Java com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接

Java com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接,java,mysql,jdbc,xampp,Java,Mysql,Jdbc,Xampp,我使用的是MacOSXLion版本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 Simpl

我使用的是MacOSXLion版本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";
Classroom cr = new Classroom();
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.Classroom.getConnection(Classroom.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

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

请对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.forNamedriver加载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();
            }

对于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.forNamedriver加载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();
            }

我终于明白了。感谢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

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

我终于明白了。感谢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

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

这是行不通的。我得到了相同的错误。Class.forNamedriver.newInstance;将加载驱动程序,并且在代码中缺少此语句。这不起作用。我得到了相同的错误。Class.forNamedriver.newInstance;将加载驱动程序,并且在您的代码中缺少此语句。感谢链接!向上投票。感谢链接!投票表决。