jdbc connection to SQL Server

jhkqcmku  于 2023-06-21  发布在  SQL Server
关注(0)|答案(1)|浏览(135)

I have a problem connecting to SQL Server. I have already tried to check my other codes and resources on the web but it's not working.

JDBC Code

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://IP_ADD;databaseName=DB_NAME", "MY_USER", "MY_PASS");

Error Log

Error during process run: The TCP/IP connection to the host IP_ADDRESS, port
1433 has failed. Error: "connect timed out. Verify the connection properties, check that an instance of SQL Server
is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host IP_ADDRESS, port 1433 has failed.
Error: "connect timed out. Verify the connection properties, check that an instance of SQL Server is running on the host
and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".
        at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1049)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
        at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at LoadTextFile.main(LoadTextFile.java:40)
jucafojl

jucafojl1#

It's clear in the error:

The TCP/IP connection to the host IP_ADDRESS, port 1433 has failed.

It seems you are using a IP_ADDRESS constant for the server. Make sure the IP_ADDRESS has right host name, (or) replace it with actual server name/IP address in the database URL.

相关问题