java sql连接字符串,sql server的等效jdbc连接字符串

I'm currently using the following connection string to connect to a database (the database is on the same server as the ServerIP):

String constr = "Data Source=ServerIP,1433;Network Library=DBMSSOCN;Initial

Catalog=dbName;User ID=dbUserID;Password=dbUserPassword";

This connects fine when used in asp.net. (I've manually created dbUserId and assigned it dbUserPassword from sql server management studio. dbUserId is the owner of the the database "dbName")

I have a java swing application on another pc, where i need to connect to the same database. I'm using sqljdbc4.jar which resides in C:. My classpath has the entry ".;C:\sqljdbc4.jar". To accomplish the connection, i'm using the following lines of code:

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

String url = "jdbc:sqlserver://ServerIP:1433;databaseName=dbName";

String user = "dbUserID";

String pass = "dbUserPassword";

Connection connection = DriverManager.getConnection(url, user, pass);

However, i get an exception on the line "Connection connection =DriverManager.getConnection(url, user, pass);" : "The TCP/IP connection to the host "ServerIP", 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."

I've checked that the windows firewall is off (And it also has the exception added for MSSQLSERVER port 1433 tcp on both home and public networks). From sql server management studio, i've enabled TCP/IP for both sql server and sql server express.

Can anyone point me what might be wrong with the connection string or sql server connection settings?

解决方案

Finally figured out the root cause. The problem was not with java connection string, IP address or the port. It was with the network. The serverIP and the IP of the machine running the java application were on different subnets. Hence, the switching mechanism between these two subnets was blocking traffic on port 1433. Hence i was getting timeouts on the pc running the java application, while the asp.net web application worked just fine (that traffic didn't cross the switch interconneting the sub networks)

I hope this turns out to be useful for someone trying to achieve something similar in future.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值