java - jdbc 连接到 SQL Server

标签 java sql-server jdbc

我连接到 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/

相关文章:

java - 如何使用java8来采集这张图

Java - 接口(interface),实例化一个接口(interface)?

sql-server - SQL Server 中指定值的累计计数

c# - 使用来自两个不同表的两个 SELECT 语句

java - Google Visualization API 中的 JDBC 结果集到数据表?

java - 为什么我的 JDBC 连接中出现 ArrayIndexOutOfBoundsException?

Java - 可以删除除 '.' 和 double / float 以外的所有字符吗?

java - 从滚动 Pane 中的文本 Pane 获取文本

sql - 如何排除sql server insert语句中的行?

java derby 连接到端口 1,527 上的服务器本地主机,并显示消息“连接被拒绝”