2017-06-05 71 views
0
public class RDF2Connection { 

    static Connection connection=null; 
    final static String connectionUrl = "jdbc:sqlserver://DESKTOP-Q5K9FE6:1433;" + 
      "databaseName=RDFDB;"; 
    public static Connection getRdf2Connected(){ 

      try { 
       Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
       connection = DriverManager.getConnection(connectionUrl, "sa", "root"); 
      } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
      return connection; 
     } 

} 

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host DESKTOP-Q5K9FE6, port 1433 has failed. Error: "Connection refused: no further information. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.". at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:206) at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:257) at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2385) at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:567) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1955) at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1616) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1447) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:788) at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1187) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at com.rdf2.databaseconnection.RDF2Connection.getRdf2Connected(RDF2Connection.java:22) at MainClass.main(MainClass.java:53) java.lang.NullPointerException at MainClass.main(MainClass.java:54)错误,同时连接的Java与SQL Server 2017年

Process finished with exit code 0

+0

可能与[this]重复(https://stackoverflow.com/questions/18981279/the-tcp-ip-connection-to-the-host-localhost-port-1433-has-failed) –

+0

检查看看如果netbios名称解析为服务器。尝试使用ipadress?或者说Mssqlserver正在运行。 –

+0

看看[this](https://stackoverflow.com/questions/18841744/jdbc-connection-failed-error-tcp-ip-connection-to-host-failed)。你有没有添加DLL? –

回答

0

只要确保这个正确的端口

这才是真正的答案 感谢。

0

你可以看到,如果SQL Server仅一个IPv6端口上侦听?如果是这样你可以使用以下内容

System.setProperty("java.net.preferIPv6Addresses", "true"); 

这将是我唯一的建议,只要代码更改。可能尝试使用IP而不是名称。如果这不起作用,你需要去你的SQL Server并验证它是否接受TCP/IP连接,或者检查你的本地防火墙设置。那时问题会更适合于服务器交换。

+0

我怎么知道它在IPv6端口上工作? –

+0

我在一段时间里还没有用过SQL Server。只需用该行建立一个新程序,看看是否仍然出现相同的错误 –