MS SQL Server and JDBC: closed connection

2020-04-05 03:50发布

Im getting

I/O Error: DB server closed connection.

while connecting to MS SQL server 2008 from java code .


SQL server is in mixed mode and its in local machine.My connection string is jTDS

jdbc:jtds:sqlserver://machineName:1433;databaseName=DB;integratedSecurity=true


stack trace is

java.sql.SQLException: I/O Error: DB server closed connection. at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2311) at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:610) at net.sourceforge.jtds.jdbc.ConnectionJDBC2.(ConnectionJDBC2.java:345) at net.sourceforge.jtds.jdbc.ConnectionJDBC3.(ConnectionJDBC3.java:50) at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at com.app.hibernate.test.(test.java:22) at com.app.hibernate.test.main(test.java:53) Caused by: java.io.IOException: DB server closed connection. at net.sourceforge.jtds.jdbc.SharedSocket.readPacket(SharedSocket.java:848) at net.sourceforge.jtds.jdbc.SharedSocket.getNetPacket(SharedSocket.java:727) at net.sourceforge.jtds.jdbc.ResponseStream.getPacket(ResponseStream.java:466) at net.sourceforge.jtds.jdbc.ResponseStream.read(ResponseStream.java:103) at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2206) ... 8 more Exception in thread "main" java.lang.NullPointerException at com.app.hibernate.test.db(test.java:36) at com.app.hibernate.test.main(test.java:54)

JDBC Driver

String url ="jdbc:sqlserver://machine:1433;instance=SQLEXPRESS;databaseName=db";

stacktrace

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'username'. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:156) at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:240) at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:78) at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2636) at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2046) at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41) at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2034) at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4003) at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1550) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1207) at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(SQLServerConnection.java:1054) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:758) at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at com.app.hibernate.test.(test.java:22) at com.app.hibernate.test.main(test.java:53) Exception in thread "main" java.lang.NullPointerException at com.app.hibernate.test.db(test.java:36) at com.app.hibernate.test.main(test.java:54)

3条回答
Melony?
2楼-- · 2020-04-05 04:33

You have an authentication error in on the MS SQL side.

If you're not in control of how to adquire the connection (ie, you're using a Datasource or a Connection Pool), the connection URL must contain the login and password to be used, like:

jdbc:sqlserver://machine:1433;instance=SQLEXPRESS;databaseName=db;user=USERNAME;password=PASSWORD";

If the application is running on a Windows machine and you want to use the credentials of the logged user, then you can specify the domain parameter with or without useNTLMv2.

Finally, if you are on a windows machine but you want to authenticate the user against a domain, then you must supply the username, password and domain parameters. You can read all about it in the jtds FAQ, specially the URL Format section.

查看更多
可以哭但决不认输i
3楼-- · 2020-04-05 04:42

You have problems in your connection strings

For jTDS:

jdbc:jtds:sqlserver://machineName:1433;databaseName=DB;useNTLMv2=tru‌​e;domain=workgroup

You may read http://jtds.sourceforge.net/faq.html#windowsAuth for the required Single-Sign-On library for NTLM to work.

"integratedSecurity=true" that you supplied for jdts is valid when using the JDBC driver

jdbc:sqlserver://machine:1433;instance=SQLEXPRESS;databaseName=db;integratedSecurity=true

查看更多
SAY GOODBYE
4楼-- · 2020-04-05 04:46

Your Connection String and authentication have errors. if it is mix mode don't use SQL authentication

Try this

PC Name : janaka-pc SQL User Name : sa SQL Password
: 1234 Database : Janak_DB

Code for sql Conncetion in JDBC

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://janaka-PC;user=sa;password=1234;database=Janak_DB");

查看更多
登录 后发表回答