我连接到 SQL Server 时遇到问题。我已经尝试检查网络上的其他代码和资源,但它不起作用。
JDBC 代码
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://IP_ADD;databaseName=DB_NAME", "MY_USER", "MY_PASS");
错误日志
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)
最佳答案
错误中很清楚:
The TCP/IP connection to the host IP_ADDRESS, port 1433 has failed.
您似乎正在为服务器使用 IP_ADDRESS 常量。确保 IP_ADDRESS 具有正确的主机名,(或)将其替换为数据库 URL 中的实际服务器名称/IP 地址。
关于java - jdbc 连接到 SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30486017/