gpt4 book ai didi

java - jdbc:jtds 连接到 SQL 服务器

转载 作者:行者123 更新时间:2023-11-29 09:30:47 25 4
gpt4 key购买 nike

<分区>

我正在尝试使用 JDBC 驱动程序在某些 Java 和 SQL Server 之间创建连接。我已经创建了一个 Connection 类,它应该形成与我计算机上服务器的连接,示例 IP 地址为“BHX”

这是连接类

public class Connection {

public static void main(String[] args) throws Exception {

try {
String databaseDriver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(databaseDriver);
} catch (Exception e) {
e.printStackTrace();
}
try {
String url = "jdbc:jtds:sqlserver://BHX:1433/Forecast;instance=SQLEXPRESS";
java.sql.Connection con = DriverManager.getConnection(url);
System.out.println("Connection created");
con.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}

我想知道 url 字符串是否有任何问题,因为当我运行这段代码时出现以下错误:

java.sql.SQLException: Network error IOException: Connection refused: connect
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:417)
at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:185)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Connection.main(Connection.java:56)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:311)
at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:261)
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:318)
... 5 more

我看过类似的问题(like this here)但看起来我已经遵循了所有可能的解决方案。我检查了配置管理器中是否启用了 TCP/IP,并且端口号是 1433。

我试过在禁用防火墙的情况下运行它,但仍然遇到同样的错误。

telnet BHX 1433,我收到以下消息“...无法在端口 1433 上打开到主机的连接:连接失败”

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com