无法将 sql 服务器 2008 r2 连接到 java

Not able to connect sql server 2008 r2 with java

我正在尝试使用我的 java 代码连接 sql 服务器 2008 r2,但它向我显示此错误。

Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host DINESH-PC, port 1433 has failed. Error: "Connection refused: connect. 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.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1033)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Main.main(Main.java:13)

我的代码是:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    String dbURL = "jdbc:sqlserver://localhost:1433;user=sa;password=123456";
    Connection conn = DriverManager.getConnection(dbURL);
    if (conn != null) {
        System.out.println("Connected");
    }

我正在使用 jre7 和 sqljdbc4-3。0.jar 包含在库中。

请帮助我提出你的建议。

您从异常消息中获得了很多信息。只需按照提示操作即可。检查所有连接字符串是否正确。检查数据库服务器 运行。检查数据库所在服务器上是否有防火墙阻止该端口。

还必须检查 tcp/ip 是否已启用 ..

检查

  • 打开sql 服务器配置管理器
  • select sql 服务器网络配置
  • 然后,select MSSQLSERVER 协议
  • 他们检查 tcp/ip 是否启用。如果不启用它。