5

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?

6
  • Can you ping ServerIP from where you are running the Swing app? Commented May 31, 2013 at 7:30
  • Ping the IP and also check if POrt is open , may be using some network sniffers Commented May 31, 2013 at 7:31
  • @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 Commented May 31, 2013 at 7:33
  • Confused... Is ServerIP the name of the box or is it just something you wrote instead of x.x.x.x in the question? Commented May 31, 2013 at 7:36
  • @maba It is something i wrote instead of x.x.x.x Commented May 31, 2013 at 7:38

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

what did u do for above problem? ie. what changes where?
@DPM I just ensured that the java application and the sql database server resided on machines which were in the same subnet as mentioned above. May i know what other information do you require?
@Karan I think I am also facing the same issue. My sql server is having the hostname *.uss.net, and linux server is having *.privatecloud.local. Are these in different subnets? Can you help me?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.