2014-02-06 142 views
1

我正在使用Microsoft SQL Server 2008,并试图使用JDBC连接到数据库。以下是我的代码。用户名和密码使用Windows身份验证。如何使用JDBC连接到SQL Server

String url1 = "jdbc:sqlserver://ServerName;databaseName=v14testvp;user=USERNAME;password='';"; 
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
    DriverManager.getConnection(url1); 

试图连接

com.microsoft.sqlserver.jdbc.SQLServerException当我收到打击错误:用户登录失败 'USERNAME'。 ClientConnectionId:befb617f-8382-4388-ad98-a210ed0c3105

有人可以帮我解决什么问题来解决错误。我已经尝试填写我的url1字符串中的用户名和密码,但我得到了同样的错误。我也尝试在用户名(domain \ USERNAME)中包含域。我相对较新的SQL和Java,所以希望我只是想念一些简单的东西。

编辑:

我改变了我的代码下面

String url1 = "jdbc:sqlserver://servername;databaseName=v14testvp;integratedSecurity=true;authenticationScheme=JavaKerberos"; 
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
     DriverManager.getConnection(url1); 

我现在得到以下错误。我有sqljdbc_auth.dll。有人能指出我如何解决这个问题的正确方向吗?

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(KerbAuthentication.java:140 ) 在com.microsoft.sqlserver.jdbc.KerbAuthentication.GenerateClientContext(KerbAuthentication.java:268) 在com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2691) 在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.micros oft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696) 在com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715) 在com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper( SQLServerConnection.java:1326) 在com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991) 在com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827) 在COM。 microsql.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) 引起者:jav ax.security.auth.login.LoginException:无法获得Princpal名认证

+0

com.microsoft.sqlserver.jdbc.SQLServerException:登录失败,用户 'USERNAME'。 1)确保用户名/密码正确。 2)如果您正在从远程机器访问,请确保授予远程访问 – kosa

+0

@Nambari - 用户名/密码正确,远程访问被授予 – TestRaptor

+0

@TestRaptor您是否找到解决该问题的方法? –

回答

0

当连接到SQL Server使用Windows身份验证,则无法使用用户名和密码(见例如this answer)。相反,您需要指定integratedSecurity=true,并根据驱动程序版本和首选项,您需要使用Kerberos身份验证(并在连接字符串中包含authenticationScheme=JavaKerberos)或加载正确的sqljdbc_auth.dll

Using Kerberos Integrated Authentication to Connect to SQL ServerBuilding the Connection URL

+0

谢谢。我编辑了我原来的问题。你能看看我的新错误吗? – TestRaptor

+0

如果你想使用sqlauth,那么你应该删除authenticationScheme属性或给它适当的值(请参阅我的答案中的第二和第三个链接)。 –

1

你不一起使用authenticationScheme = JavaKerberos与sqljdbc_auth.dll。如果你有DLL,那么就省略了认证方案,并且你将在用户登录到Windows时进行连接。

如果您想以不同用户身份连接,或者您不在Windows上,则可以指定authenticationScheme = JavaKerberos。为此,您需要提供以下系统属性:

  • -Djava.security.auth.login.config = ???
  • -Djava.security.krb5.conf = ???

https://docs.oracle.com/javase/8/docs/technotes/guides/security/jgss/tutorials/KerberosReq.html

1

你必须设置的Kerberos相关的Java属性

1)LoginContext的配置文件 默认情况下,微软为您配置,这是optional.But,如果你想控制更多关于行为,你必须深入细节。 https://docs.oracle.com/javase/8/docs/jre/api/security/jaas/spec/com/sun/security/auth/module/Krb5LoginModule.html

2)Kerberos配置 一)使用外部configuation然后传递到Java b)中直接使用Java属性来设置 像System.setProperty( “java.security.krb5.realm”, “”) ; System.setProperty( “java.security.krb5.kdc”,