简体   繁体   English

无法使用jTDS连接到SQL Server

[英]Can't connect to SQL Server using jTDS

I'm trying to connect to SQL Server 2008 R2 via Java, and I'm unable to do so using jTDS 1.2.8. 我正在尝试通过Java连接到SQL Server 2008 R2,但无法使用jTDS 1.2.8进行连接。 The odd thing is that it works fine using the Microsoft JDBC driver. 奇怪的是,使用Microsoft JDBC驱动程序可以正常工作。 Is there some server-side setting that needs to be turned on to enable jTDS to access it? 是否需要启用某些服务器端设置才能使jTDS访问它? Or am I just missing something in the URL? 还是我只是在URL中缺少某些内容?

I'm not using Windows integrated authentication to specify credentials, nor am I attempting to connect using SSL encryption (those are issues I found that can generate the exception I'm seeing.) 我没有使用Windows集成身份验证来指定凭据,也没有尝试使用SSL加密进行连接(那些是我发现的问题,可以生成我所看到的异常。)

If I use the following with the Microsoft driver, it works as expected, I can access the database with no problems: 如果我在Microsoft驱动程序中使用以下命令,它可以按预期工作,则可以毫无问题地访问数据库:

Connection connection = DriverManager.getConnection("jdbc:sqlserver://PHSSQL792\\PHSSQL792:1433", user, password);

( user and password are variables declared earlier, so I can be sure I use the same values when connecting with either driver.) userpassword是之前声明的变量,因此可以确定在与任何一个驱动程序连接时,我使用相同的值。)

However, if I use the following with the jTDS driver: 但是,如果我在jTDS驱动程序中使用以下命令:

Connection connection = DriverManager.getConnection("jdbc:jtds:sqlserver://PHSSQL792:1433;instance=PHSSQL792", user, password);

I get the following error: 我收到以下错误:

java.sql.SQLException: I/O Error: DB server closed connection.
    at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2387)
    at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:614)
    at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:356)
    at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)
    at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:185)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at database.db_access.SqlServerDatabaseTestApp.main(SqlServerDatabaseTestApp.java:28)
Caused by: java.io.IOException: DB server closed connection.
    at net.sourceforge.jtds.jdbc.SharedSocket.readPacket(SharedSocket.java:853)
    at net.sourceforge.jtds.jdbc.SharedSocket.getNetPacket(SharedSocket.java:732)
    at net.sourceforge.jtds.jdbc.ResponseStream.getPacket(ResponseStream.java:477)
    at net.sourceforge.jtds.jdbc.ResponseStream.read(ResponseStream.java:114)
    at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2281)
    at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:614)
    at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:356)
    at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)
    at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:185)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at database.db_access.SqlServerDatabaseTestApp.main(SqlServerDatabaseTestApp.java:28)

I've tried connecting with and without the database name, and/or with/without the instance name, and got the same results. 我尝试使用和/或不使用数据库名称和/或使用/不使用实例名称进行连接,并得到相同的结果。 Any suggestions? 有什么建议么?

Edit: 编辑:

Other jTDS connection URLs I've tried (which all gave me the same error as above): 我尝试过的其他jTDS连接URL(都给了我与上面相同的错误):

"jdbc:jtds:sqlserver://PHSSQL792:1433"
"jdbc:jtds:sqlserver://PHSSQL792:1433/pacsdb"
"jdbc:jtds:sqlserver://PHSSQL792:1433/pacsdb;instance=PHSSQL792"

The corresponding Microsoft URLS (which all worked): 相应的Microsoft URLS(均有效):

"jdbc:sqlserver://PHSSQL792:1433"
"jdbc:sqlserver://PHSSQL792:1433;databasename=pacsdb"
"jdbc:sqlserver://PHSSQL792\\PHSSQL792:1433;databasename=pacsdb"

Also, I can successfully connect to a different SQL Server 2008 R2 database (on a different server) using the jTDS driver, so it's not the jar. 另外,我可以使用jTDS驱动程序成功连接到其他SQL Server 2008 R2数据库(在其他服务器上),因此它不是jar。

这是我们使用的格式,看起来非常接近您:

jdbc:jtds:sqlserver://localhost:1433/Dev_DB;tds=8.0;lastupdatecount=true

It's strange, yours connection strings looks correct. 很奇怪,您的连接字符串看起来正确。 Try to set user and password directly: 尝试直接设置用户和密码:

"jdbc:jtds:sqlserver://PHSSQL792:1433/pacsdb;instance=PHSSQL792;user=sa;password=pass"

In case anyone ever runs into this, I came back to this much later and finally figured out the problem. 万一有人碰到这个问题,我很久以后又回到了这个问题上,终于找到了问题所在。 The SQL Server instance in question was configured to require SSL connections! 有问题的SQL Server实例已配置为要求SSL连接! I just added ssl=request to the URL to make it work. 我刚刚在URL中添加了ssl=request来使其工作。

I think the reason the Microsoft driver works without explicitly setting SSL encryption is that it ALWAYS initially connects with SSL to encrypt username/password for login. 我认为Microsoft驱动程序在未显式设置SSL加密的情况下工作的原因是,它始终最初与SSL连接以对登录的用户名/密码进行加密。 The encrypt property only controls whether data after login is encrypted. encrypt属性仅控制登录后的数据是否被加密。

String driver="net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();
//First way
String connString="jdbc:jtds:sqlserver://192.168.1.123:1433/database_name;encrypt=false;user=sa;password=mypass;";
String username="sa";
String password="mypass";
Connection con=DriverManager.getConnection(connString,username,password);   

//Second way
String connString="jdbc:jtds:sqlserver://127.0.0.1:1433/database_name;encrypt=false;user=sa;password=mypass;integratedSecurity=true;instance=SQLEXPRESS;";
String username="sa";
String password="mypass";
Connection con=DriverManager.getConnection(connString,username,password);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM