使用jdbc从java连接到sql server(Windows身份验证模式)
发布时间:2020-12-14 19:34:35 所属栏目:Java 来源:网络整理
导读:我需要使用 jdbc 4.0从java连接到Sql Server 2008. 我有一个非常简单的代码: Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String connectionUrl = "jdbc:sqlserver://localhost;" + "integratedSecurity=true;"; Connection con = Dri
我需要使用
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 Windows服务正在运行. 在SQL Server网络配置中,我选择了Enebled on TCP / IP属性. 我将TCP地址设置为1433. 在运行,VM选项我把-Djava.library.path =我的路径sqljdbc_auth.dll 我该怎么办? 编辑: 解决方法
如果使用Window身份验证,您可以执行以下操作:
String url = "jdbc:sqlserver://MYPCSQLEXPRESS;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.希望能帮助到你! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |