[英]Access SQL Server 2008 R2 via Scala/JDBC on Windows 2008 64 bit using integrated authentication
I'm trying to access a SQL server 2008 using scala without any success. 我正在尝试使用scala访问SQL Server 2008,但没有成功。 Here is my sample code: 这是我的示例代码:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
val connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=XXX;integratedSecurity=true;";
val con = DriverManager.getConnection(connectionUrl);
And here is the error I get: 这是我得到的错误:
com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1352)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1339)
at com.microsoft.sqlserver.jdbc.TDSChannel.read(IOBuffer.java:1654)
at com.microsoft.sqlserver.jdbc.TDSReader.readPacket(IOBuffer.java:3694)
at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:5026)
at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:4978)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2528)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1905)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1893)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1045)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:817)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
I have no idea what's going wrong and I'm not able to get any further debug output. 我不知道出了什么问题,也无法获得更多的调试输出。 What I have checked is the following: 我检查的是以下内容:
Obviously the integrated authentication is the problem. 显然,集成身份验证是问题所在。 Any hint how to fix that or how to debug any further? 有任何提示如何解决该问题或如何进一步调试吗?
This MSDN page suggests that you have to copy a file, or change a path: 此MSDN页面建议您必须复制文件或更改路径:
To use integrated authentication, copy the sqljdbc_auth.dll file to a directory on the Windows system path on the computer where the JDBC driver is installed. 要使用集成身份验证,请将sqljdbc_auth.dll文件复制到安装了JDBC驱动程序的计算机上Windows系统路径上的目录中。
Under the "Connecting with Integrated Authentication" section. 在“连接集成身份验证”部分下。
I've had more success using the jTDS open source driver to access SQL Server databases through JDBC ( http://jtds.sourceforge.net/ ). 使用jTDS开源驱动程序通过JDBC( http://jtds.sourceforge.net/ )访问SQL Server数据库时,我获得了更大的成功。
I used this to connect to a SQL Server 2008 instance using this connection URL: 我使用此连接使用以下连接URL连接到SQL Server 2008实例:
jdbc:jtds:sqlserver://localhost/DatabaseName;instance=InstanceName
My connection still seems to work when I add the property integratedSecurity=True
, but it doesn't seem necessary. 当我添加属性integratedSecurity=True
,我的连接似乎仍然有效,但是似乎没有必要。 I believe the jTDS docs say that you're not supposed to end the connection URL with a semi-colon. 我相信jTDS文档说您不应以分号结束连接URL。
You'll have to add the ntmlauth.dll
file to your JRE/JDK /bin
directory. 您必须将ntmlauth.dll
文件添加到JRE / JDK /bin
目录。 You also must ensure that the SQL Server Browser service is running. 您还必须确保SQL Server Browser服务正在运行。 My understanding is that this is true both for the jTDS driver and the Microsoft driver (run the SQL Server Configuration Manager and star the SQL Server Browser service. You may want to set it to automatically start on startup). 我的理解是,这对于jTDS驱动程序和Microsoft驱动程序都是正确的(运行SQL Server配置管理器并启动SQL Server浏览器服务。您可能希望将其设置为在启动时自动启动)。
Another nice advantage of jTDS is that you can get it through a Maven repository if you're using Maven: jTDS的另一个不错的优点是,如果您使用的是Maven,则可以通过Maven存储库获取它:
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.2.4</version>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.