Java 如何使用 JDBC 连接到 SQL Server
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21612385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect to SQL Server using JDBC
提问by TestRaptor
I am using Microsoft SQL Server 2008 and am trying to connect to the database using JDBC. Below is my code. The username and password use Windows authentication.
我正在使用 Microsoft SQL Server 2008 并尝试使用 JDBC 连接到数据库。下面是我的代码。用户名和密码使用 Windows 身份验证。
String url1 = "jdbc:sqlserver://ServerName;databaseName=v14testvp;user=USERNAME;password='';";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
DriverManager.getConnection(url1);
I am getting the blow error when trying to connect
尝试连接时出现打击错误
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'USERNAME'. ClientConnectionId:befb617f-8382-4388-ad98-a210ed0c3105
com.microsoft.sqlserver.jdbc.SQLServerException:用户“USERNAME”登录失败。ClientConnectionId:befb617f-8382-4388-ad98-a210ed0c3105
Can someone help me out on what to check to resolve the error. I have tried filling out both the user name and password in my url1 string, but I get the same error. I have also tried including the domain in the user name (domain\USERNAME). I'm relatively new to sql and java, so hopefully I'm just missing something simple.
有人可以帮助我了解要检查什么来解决错误。我尝试在 url1 字符串中同时填写用户名和密码,但出现相同的错误。我也尝试在用户名中包含域(域\用户名)。我对 sql 和 java 比较陌生,所以希望我只是错过了一些简单的东西。
EDIT:
编辑:
I changed my code to below
我将代码更改为以下
String url1 = "jdbc:sqlserver://servername;databaseName=v14testvp;integratedSecurity=true;authenticationScheme=JavaKerberos";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
DriverManager.getConnection(url1);
I am now getting the below error. I have the sqljdbc_auth.dll. Can someone point me in the right direction on how to resolve this?
我现在收到以下错误。我有 sqljdbc_auth.dll。有人可以指出我如何解决这个问题的正确方向吗?
com.microsoft.sqlserver.jdbc.SQLServerException: Integrated authentication failed. ClientConnectionId:0e66f60e-958c-4c8e-85b9-484023f16ecf at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1667) at com.microsoft.sqlserver.jdbc.KerbAuthentication.intAuthInit(KerbAuthentication.java:140) at com.microsoft.sqlserver.jdbc.KerbAuthentication.GenerateClientContext(KerbAuthentication.java:268) at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2691) at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234) at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41) at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220) at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696) at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326) at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827) at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at com.ibm.atmn.tests.DumbyClass.DumbyTests(DumbyClass.java:52) Caused by: javax.security.auth.login.LoginException: Unable to obtain Princpal Name for authentication
com.microsoft.sqlserver.jdbc.SQLServerException:集成身份验证失败。ClientConnectionId:0e66f60e-958c-4c8e-85b9-484023f16ecf at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1667) at com.microsoft.sqlserver.jdbc.KerbAuthentication.intAuthInit(Kerb)java com.microsoft.sqlserver.jdbc.KerbAuthentication.GenerateClientContext(KerbAuthentication.java:268) at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2691) at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon( SQLServerConnection.java:2234) 在 com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41) 在 com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:
回答by Mark Rotteveel
When connecting to SQL Server with Windows Authentication, you cannot use a username and password (see for example this answer). Instead you need to specify integratedSecurity=true
and depending on the driver version and preference you need to use Kerberos authentication (and include authenticationScheme=JavaKerberos
in the connection string) or load the right sqljdbc_auth.dll
.
使用 Windows 身份验证连接到 SQL Server 时,不能使用用户名和密码(例如参见此答案)。相反,您需要integratedSecurity=true
根据驱动程序版本和首选项指定使用 Kerberos 身份验证(并包含authenticationScheme=JavaKerberos
在连接字符串中)或加载正确的sqljdbc_auth.dll
.
See Using Kerberos Integrated Authentication to Connect to SQL Serverand Building the Connection URL
回答by Jeff Johnston
You do not use authenticationScheme=JavaKerberos
together with sqljdbc_auth.dll
. If you have the DLL then leave out the authentication scheme and you will connect as the user logged into Windows.
您不要authenticationScheme=JavaKerberos
与 一起使用sqljdbc_auth.dll
。如果您有 DLL,则省略身份验证方案,您将以登录 Windows 的用户身份进行连接。
If you want to connect as a different user or you are not on Windows then you can specify authenticationScheme=JavaKerberos
. For this to work you need to supply these system properties:
如果您想以其他用户身份连接或者您不在 Windows 上,则可以指定authenticationScheme=JavaKerberos
. 为此,您需要提供以下系统属性:
-Djava.security.auth.login.config=???
-Djava.security.krb5.conf=???
-Djava.security.auth.login.config=???
-Djava.security.krb5.conf=???
回答by Tim
you have to setup kerberos related java properties
您必须设置与 kerberos 相关的 java 属性
1) loginContext configuration file By default ,microsoft configured for you and this is optional.But if you want to control more about the behavior, you have to dive into the details. https://docs.oracle.com/javase/8/docs/jre/api/security/jaas/spec/com/sun/security/auth/module/Krb5LoginModule.html
1) loginContext 配置文件默认情况下,微软为你配置,这是可选的。但如果你想控制更多的行为,你必须深入了解细节。 https://docs.oracle.com/javase/8/docs/jre/api/security/jaas/spec/com/sun/security/auth/module/Krb5LoginModule.html
2) kerberos configuration a) use external configuation then passed to java b) directly use java properties to set like System.setProperty("java.security.krb5.realm", ""); System.setProperty("java.security.krb5.kdc",)
2)kerberos配置a)使用外部配置然后传递给java b)直接使用java属性设置像System.setProperty(“java.security.krb5.realm”,“ ”); System.setProperty("java.security.krb5.kdc",)