简体   繁体   English

无法使用java连接sql server 2008 r2

[英]Not able to connect sql server 2008 r2 with java

I am trying to connect sql server 2008 r2 with my java code , But it is showing me this error . 我试图连接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)

my code is: 我的代码是:

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");
    }

i am using jre7 and sqljdbc4-3.0.jar is included in library. 我正在使用jre7和sqljdbc4-3.0.jar包含在库中。

Help me with ur suggesstions. 帮助我你的suggesstions。

You have quite a lot of information from exception message. 您从异常消息中获得了大量信息。 Just follow the hints. 只需按照提示操作即可。 Check if all connection string are right. 检查所有连接字符串是否正确。 Check if DB server run. 检查DB服务器是否运行。 Check if there is no firewall blocking that port on server where DB is situated. 检查在DB所在的服务器上是否没有防火墙阻止该端口。

also must check wheather tcp/ip is enabled .. 还必须检查是否启用了tcp / ip ..

to check 去检查

  • open sql server configration manager 打开sql server configration manager
  • select sql server network configration 选择sql server network configration
  • then, select protocol for MSSQLSERVER 然后,为MSSQLSERVER选择协议
  • them check wheather tcp/ip is enabled or not. 他们检查是否启用了tcp / ip。 If not enable it. 如果没有启用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM