Java JDBC连接错误:通信链接失败

时间:2016-01-02 09:50:02

标签: java sql-server jdbc

我现在正在使用JDBC两天。但仍未成功与数据库建立成功连接。 这是建立数据库连接的代码:

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

public class DBConnector {

Connection conn = null;
public static String SERVER_NAME = "localhost";
public static String PORT_NUMBER = "3306";
public static String DATABASE_NAME = "TemporaryDatabase";
public String uri = "jdbc:mysql://" + SERVER_NAME + ":" + PORT_NUMBER + "/" + DATABASE_NAME;

public String username = "root";
public String password = "";

public DBConnector() {
}

public boolean Connect() {

    try {

        if (init()) {
            conn = DriverManager.getConnection(uri, username, password);
            return true;

        } else {
            System.out.println("Can not load driver.");
            return false;
        }
    } catch (SQLException se) {
        se.printStackTrace();
        return false;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

}

public static boolean init() {
    String driverName = "com.mysql.jdbc.Driver"; // for MySql

    try {
        Class.forName(driverName);
        return true;

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        return false;
    }

}

}

以下是我运行这段代码时遇到的错误:

  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:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:981)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2286)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2085)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at DBConnector.Connect(DBConnector.java:24)
    at WebCrawler.main(WebCrawler.java:37)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298)
    ... 16 more
CONNECTION: false

我正在使用MS SQL Server 2012.任何人都可以让我知道我做错了什么。我猜它的用户名和密码,但我也不知道正确的。我正在使用默认的SQL服务器实例。

1 个答案:

答案 0 :(得分:0)

您使用的是SQL Server,但您的JDBC网址是针对MySQL的。 SQL Server的正确JDBC url表单如下所示:

jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]

例如:

jdbc:sqlserver://localhost:1433;databaseName=TemporaryDatabase

另外,你应该使用SQL Server的JDBC驱动程序而不是MySQL的。您可以找到驱动程序here以及有关SQL Server连接字符串here的信息。