Question

I'm trying to connect to SQL Server 2008 R2 via Java, and I'm unable to do so using jTDS 1.2.8. The odd thing is that it works fine using the Microsoft JDBC driver. Is there some server-side setting that needs to be turned on to enable jTDS to access it? Or am I just missing something in the 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.)

If I use the following with the Microsoft driver, it works as expected, I can access the database with no problems:

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.)

However, if I use the following with the jTDS driver:

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):

"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):

"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.

Was it helpful?

Solution

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! I just added ssl=request to the URL to make it work.

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. The encrypt property only controls whether data after login is encrypted.

OTHER TIPS

Here's the format we are using, which looks very close to your:

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"
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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top