无法使用java连接sql server 2008 r2

时间:2015-02-19 05:14:07

标签: java sql-server sql-server-2008 tcp-ip

我正在尝试将sql server 2008 r2与我的java代码连接,但是它向我显示了这个错误。

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包含在库中。

帮助我解决你的问题。

1 个答案:

答案 0 :(得分:0)

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

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

检查

  • 打开sql server configration manager
  • 选择sql server network configration
  • 然后,选择MSSQLSERVER协议
  • 他们检查是否启用了tcp / ip。如果没有启用它。