【问题标题】:Connect to sql server from java with jdbc(windows authentication mode)使用 jdbc(windows 身份验证模式)从 java 连接到 sql server
【发布时间】:2013-05-21 18:00:31
【问题描述】:

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

我在库/编译中添加了 jdbc4.jar

SQL Server Browser windows 服务正在运行。

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

我将 TCP 地址设置为 1433。

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

我该怎么办?

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

【问题讨论】:

  • 你不需要 forName() 和 jdbc 4。
  • 好的,所以?我得到了同样的错误......
  • 否。必须与第一个字符串一起使用。“使用集成身份验证连接到本地计算机上的默认数据库:jdbc:sqlserver://localhost;integratedSecurity=true;”来自msdn.microsoft.com/en-us/library/ms378428.aspx
  • 我唯一能建议的是重新启动 SQL 服务器并重试。该错误清楚地表明没有服务器正在侦听您放置的端口。
  • 如何重新启动 sql server?我关闭 Sql Server Management 并再次打开?我已经这样做了。

标签: java sql-server-2008 netbeans jdbc netbeans-7


【解决方案1】:

如果使用 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。希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 2013-09-07
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多