com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接[重复]

Posted

技术标签:

【中文标题】com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接[重复]【英文标题】:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link [duplicate] 【发布时间】:2012-09-17 14:25:41 【问题描述】:

我使用的是 Mac OS X Lion 10.8 版。我通过 XAMPP 安装了 mysql。而且我已将 mysql 和 jdbc 驱动程序 (5.1.22) 添加到我的类路径中。以下简单代码不起作用:

package learning.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Simple 

Connection getConnection() throws SQLException
String url = "jdbc:mysql://localhost/";
return DriverManager.getConnection(url, "username", "password");


void createDatabase(String database, Connection conn) throws SQLException
Statement stmt = null;
try 
stmt = conn.createStatement();
stmt.executeUpdate("drop database if exists "+database);
stmt.executeUpdate("create database "+database);
finally 
if(null != stmt)
stmt.close();



public static void main(String args[]) throws Exception
String school = "school";
Clas-s-room cr = new Clas-s-room();
Connection conn = cr.getConnection();
cr.createDatabase(school, conn);


当我运行代码时,我收到以下错误:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:350)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2408)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2445)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2230)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at learning.database.Clas-s-room.getConnection(Clas-s-room.java:43)
at learning.database.Simple.main(Simple.java:33)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:218)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
... 16 more

有人知道如何解决这个问题吗?

【问题讨论】:

感谢您的链接!赞成票。 【参考方案1】:

我终于明白了。感谢 Max 和 Yogendra 的发帖。 问题原来是我的 XAMPP 配置。为了修复它,我做了如下操作:

-   sudo vi /Applications/XAMPP/etc/my.cnf 
⁃   scroll down to `skip-networking`
⁃   comment it out by changing it to `#skip-networking`
⁃   save the changes.
⁃   restart mysql from XAMPP Console

然后我运行我的代码,一切都很好!

【讨论】:

【参考方案2】:

MySQL 数据库请使用 mysql-connector-java-5.1.10-bin.jar 或以上版本。以下是正确的代码。从 jdbc 更改导入语句。例如。导入 com.mysql.jdbc.Connection;并导入 com.mysql.jdbc.Statement; 代替 导入java.sql.Connection;并导入 java.sql.Statement;

您需要使用 Class.forName(driver) 为 MySQL 加载类驱动程序。使用下面的代码就可以了。

            Connection conn = null;
            String url = "jdbc:mysql://localhost:3306/";
            String dbName = "databaseName";
            String driver = "com.mysql.jdbc.Driver";
            String userName = "root";
            String password = "root";
            try
            
                    Class.forName(driver).newInstance();
                    conn = (Connection) DriverManager.getConnection(url
                                    + dbName, userName, password);
                    Statement stmt = (Statement) conn.createStatement();
             catch (Exception e)
            
                    e.printStackTrace();
            

【讨论】:

这不起作用。我得到同样的错误。 Class.forName(driver).newInstance();将加载驱动程序,并且在您的代码中缺少此语句。

以上是关于com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接[重复]的主要内容,如果未能解决你的问题,请参考以下文章

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:重复条目 '' 键 'PRIMARY'

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障?

错误 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表不存在

使用 JDBC 访问远程 MySQL 数据库时出现 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障