gpt4 book ai didi

java - 使用jdbc(windows身份验证模式)从java连接到sql server

转载 作者:搜寻专家 更新时间:2023-10-31 20:21:12 25 4
gpt4 key购买 nike

我需要使用 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);

但是我有这个错误:

    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 com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
...

我遵循这个答案:https://stackoverflow.com/a/12524566/1554397

我在 Libraries/Compile 添加了 jdbc4.jar

SQL Server Browser 窗口服务正在运行。

在 SQL Server 网络配置中,我选择了启用 TCP/IP 属性。

我将 TCP 地址设置为 1433。

在运行时,VM 选项我把 -Djava.library.path=my path to sqljdbc_auth.dll并复制到 JDk 中,在 bin sqljdbc_auth.dll 中。

我该怎么办?

编辑:当在 cmd telnet localhost 1433 中写入时,我得到“无法在端口 1433 上打开与主机的连接”

最佳答案

如果使用 Window 身份验证,您可以执行以下操作:

String url = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url);

然后将路径添加到 sqljdbc_auth.dll 作为 VM 参数(构建路径中需要 sqljdbc4.jar)。

看看here如果您需要更多详细信息,请参阅演示如何从 Java 连接到 SQL Server 的简短分步指南。希望对您有所帮助!

关于java - 使用jdbc(windows身份验证模式)从java连接到sql server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16676527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com