4
votes

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?

2
Can you ping ServerIP from where you are running the Swing app? - maba
Ping the IP and also check if POrt is open , may be using some network sniffers - W A K A L E Y
@maba I'm able to ping the IP. Infact i'm using the server remotely from my machine running the java application. Also, netstat -aon gives the following line: TCP 0.0.0.0:1433 0.0.0.0:0 LISTENING 5584 - user720694
Confused... Is ServerIP the name of the box or is it just something you wrote instead of x.x.x.x in the question? - maba
@maba It is something i wrote instead of x.x.x.x - user720694

2 Answers

3
votes

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
votes

Have you tried with jtds.jar. I am using the following it is working for me.

  public static String jdbc_url="jdbc:jtds:sqlserver://yourServerIp:1433/dbName";
  public static String jdbc_username="sa"; 
  public static  String jdbc_password="prabhakar";
  public static String jdbc_driver="net.sourceforge.jtds.jdbc.Driver"; 
  Class.forName(jdbc_driver);
  Connection cn=null;    
  cn=DriverManager.getConnection(jdbc_url, jdbc_username,jdbc_password);