我尝试连接数据库时出现jdbc.SQLServerException

时间:2019-05-23 20:07:01

标签: java sql sql-server-2012

我有用于从JavaFx应用程序连接到数据库的类

public class DBConnection {

public static Connection mcConnection() {
    Connection connection = null;
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String url = "jdbc:sqlserver://localhost; databaseName=McQueen_db;";
        connection = DriverManager.getConnection(url);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return connection;
}

}

当我尝试连接时,我有一个例外情况

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host port 1433 has failed. Error: "null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".

我检查了防火墙及其状况。我也认为那个港口。有什么问题吗?

1 个答案:

答案 0 :(得分:1)

尝试将“ localhost”更改为“ 127.0.0.1”

有时,您的系统无法正确映射localhost。

参阅official doc