Equivalent jdbc connection string for sql server

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

String constr = "Data Source=ServerIP,1433;Network Library=DBMSSOCN;Initial Catalog=dbName;User ID=dbUserID;Password=dbUserPassword"; 

This connection works great when used with asp.net. (I manually created dbUserId and assigned it dbUserPassword from sql server management studio. DbUserId is the owner of the dbName database)

I have a java swing application on another PC where I need to connect to one database. I am using sqljdbc4.jar, which is located in C :. My class has an entry ".; C: \ sqljdbc4.jar". To make the connection, I use 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 in the line "Connection connection = DriverManager.getConnection (url, user, pass);": "TCP / IP connection to host" ServerIP ", port 1433 failed. Error:" Connection timed out. Check connection properties. Verify that the instance of SQL Server is running on the host and accepts TCP / IP connections on the port. Ensure that TCP connections to the port are not blocked by the firewall. "

I checked that the Windows Firewall is turned off (And he also added an exception for the MSSQLSERVER 1433 tcp port on both home and public networks). From sql server management studios, I have enabled TCP / IP for SQL Server and SQL Server Express.

Can someone tell me what might be wrong in the connection settings or connection settings to sql server?

+4
source share
2 answers

Finally, the reason became clear. The problem was not the Java connection string, IP address or port. That was with the network. The IP server and the IP address of the machine running the Java application were on different subnets. Consequently, the switching mechanism between these two subnets blocked traffic on port 1433. Therefore, I received timeouts on a PC running a java application, while the asp.net web application worked very well (this traffic did not cross the switch connecting the subnets )

I hope this will be useful for those who are trying to achieve something similar in the future.

+2
source

Have you tried using jtds.jar . I use the following, this works 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); 
0
source

Source: https://habr.com/ru/post/1483729/


All Articles