无法使用java连接到sql server

2020年7月21日 187点热度 0条评论 来源: Ghimpu Lucian Eduard

我试图使用Java和Microsoft SQL Server连接到本地sql服务器。 我正在使用sql server auth。无法使用java连接到sql server

下面的代码:

import java.sql.*; 


public class Main { 
    public static void main(String[] args) { 

     String userName ="testlogin2"; 
     String password ="pass"; 

     String url ="jdbc:sqlserver://localhost/LabNoteMap;integratedSecurity=true;"; //LabNoteMap beeing target db 
     try { 
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
      Connection conn = DriverManager.getConnection(url, userName, password); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 

     System.out.println("end"); 
    } 
} 

和堆栈跟踪:

"C:\Program Files\Java\jdk-9\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.5\lib\idea_rt.jar=63510:C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.5\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Deus\IdeaProjects\testMYSQL\out\production\testMYSQL;C:\Users\Deus\Desktop\jdbc\sqljdbc_6.0\enu\jre8\sqljdbc42.jar Main 
end 
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost/LabNoteMap, port 1433 has failed. Error: "localhost/LabNoteMap. 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:191) 
    at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:242) 
    at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2369) 
    at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:551) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1963) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1628) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1459) 
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:773) 
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1168) 
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:678) 
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229) 
    at Main.main(Main.java:13) 

Process finished with exit code 0 

也有些SS:

enter image description here

enter image description here

另请注意,在Sql服务器配置中,tcp/ip已启用且端口设置为1433.

我猜可能是防火墙?但不知道如何检查它,或者别的什么?


===========解决方案如下:

您的连接字符串错误。它

String url ="jdbc:sqlserver://localhost/LabNoteMap;integratedSecurity=true;"; 

String url ="jdbc:sqlserver://localhost;databaseName=LabNoteMap;integratedSecurity=true;"; 

您目前并未指定要建立连接的数据库,你应该改变。

    原文作者:Ghimpu Lucian Eduard
    原文地址: https://stackoverflow.com/q/47993670
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。