我需要使用jdbc 4.0从java连接到Sql Server 2008 .我有一个非常简单的代码:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost;" +
"integratedSecurity=true;";
Connection con = DriverManager.getConnection(connectionUrl);
Run Code Online (Sandbox Code Playgroud)
但我有这个错误:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. 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:190)
at …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过 Java Web 应用程序连接 MS SQL 服务器。
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection= DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=XXX;integratedSecurity=true");
Run Code Online (Sandbox Code Playgroud)
我已将“sqljdbc_auth.dll”复制到 $Tomcat_home/bin 并将 jar 复制到 $Tomcat_home/lib 文件夹中。
我在同一个 tomcat 实例中有多个 Web 应用程序。
第一个 webApp 加载并成功建立与 MS SQL 的连接。
但是其余的应用程序无法连接到 MS SQL 提示:
com.microsoft.sqlserver.jdbc.SQLServerException: This driver is not configured for integrated authentication. ClientConnectionId:41d72756-1383-427e-8c4f-c3075ae1559a
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:2400)
at com.microsoft.sqlserver.jdbc.AuthenticationJNI.<init>(AuthenticationJNI.java:68)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3132)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$100(SQLServerConnection.java:43)
Run Code Online (Sandbox Code Playgroud)
注意: Tomcat 作为 Windows 服务运行。并且 MSSQL 配置为 Windows 身份验证。
编辑:我知道本机库 (DLL) 只能加载到 JVM 中一次,因此出现错误,但我环顾网络后仍然没有解决方案。
我在用
连接到数据库时出现以下错误:
com.microsoft.sqlserver.jdbc.SQLServerException:与主机的TCP/IP连接失败.java.net.ConnectException:连接被拒绝:连接
我的连接字符串是:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;databaseName=dbcm;" +
"user=sa;password=sa");
Run Code Online (Sandbox Code Playgroud)
我检查了下面的事情: