简体   繁体   中英

JDBC connection doesn't work with Sql Server 2012 or higher

The code below works with Sql Server 2008 but not with Sql Server 2012 and 2014 (v 11 or 12)

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://myhost\\myinstance:1433;database=mydb";
connection = DriverManager.getConnection(url,"username", "pwd");

I'm using Java Runtime 1.7

I'm using the Microsoft Drivers as you can see in the Shell line below.

I've tried putting sqljdbc_auth.dll (I've tried both 32 and 64 bit) in the jar drivers folder,
in JRE bin folder or even passed as java.library.path

I've checked in the configuration manager (on the Sql Server) that TCP/IP is enabled and responds at port 1433.

The firewall is off.

Here is the error I get:

c:\\test\\TestConn\\Test2\\bin>"C:\\Program Files\\Java\\jre7\\bin\\java" -classpath C:\\libsqlsvr\\sqljdbc_6.0\\ita\\sqljdbc4.1.jar;. -Djava.library.path=C:\\libsqlsvr\\sqljdbc_6.0\\ita\\auth\\x64 Test com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the hot myhost , port 1433 has failed. Error: "connect timed out. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.". at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(S QLServerException.java:190) at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExcepti onToSQLServerException(SQLServerException.java:241) at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:22 43) at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLSer verConnection.java:1309) at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConne ction.java:991) at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerCon nection.java:827) at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver. java:1012) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at Test.testsqlsvr(Test.java:135) at Test.main(Test.java:24)

Do you know what could cause the problem?

Since you're using HOST:PORT identification of the SQL Server in JDBC, you should provide a valid IP or hostname in the HOST part, not the instance name (ie no "myhost\\myinstance", but the actual network hostname / IP).

The JDBC connection goes through normal TCP, so the HOST:PORT is an ordinary network address, no Microsoft magic. HOSTNAME\\INSTANCE uses a dispatcher service ( SQL Server Browser ) that resides on a known port (UDP 1434) and forwards the connection according to the \\INSTANCE part, and is not combined with port.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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