在 windows 中连接到 db2 服务器
Connect to db2 server in windows
我正在使用这段 java 代码连接到 db2 数据库服务器。
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DatabaseConnect {
Connection co=null;
Statement st=null;
PreparedStatement pstmt;
ResultSet rs;
int status;
void makeConnection() throws ClassNotFoundException, SQLException
{
Class.forName("com.ibm.db2.jcc.DB2Driver");
co=DriverManager.getConnection("jdbc:db2://localhost:50000/databasename","username","password");
//st=co.createStatement();
}
Connection getConnection()
{
return co;
}
void endConnection() throws SQLException
{
co.close();
}
public static void main(String args[]) throws ClassNotFoundException, SQLException
{
DatabaseConnect db=new DatabaseConnect();
db.makeConnection();
new MaintainHash().createHashMap(db.getConnection());
new Login(db.getConnection()).setVisible(true);
}
}
如果主机名是 localhost,它工作正常,但是当我提供远程服务器的主机名时,程序挂起并显示此错误。
Exception in thread "main" com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][3.68.61] Exception java.net.ConnectException: Error opening socket to server /192.168.80.1 on port 50,000 with message: Connection timed out: connect. ERRORCODE=-4499, SQLSTATE=08001
at com.ibm.db2.jcc.am.gd.a(gd.java:329)
at com.ibm.db2.jcc.am.gd.a(gd.java:410)
at com.ibm.db2.jcc.t4.ac.a(ac.java:439)
at com.ibm.db2.jcc.t4.ac.<init>(ac.java:96)
at com.ibm.db2.jcc.t4.a.b(a.java:358)
at com.ibm.db2.jcc.t4.b.newAgent_(b.java:2066)
at com.ibm.db2.jcc.am.Connection.initConnection(Connection.java:780)
at com.ibm.db2.jcc.am.Connection.<init>(Connection.java:725)
at com.ibm.db2.jcc.t4.b.<init>(b.java:333)
at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:233)
at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:199)
at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:474)
at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:115)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at DatabaseConnect.makeConnection(DatabaseConnect.java:31)
at DatabaseConnect.main(DatabaseConnect.java:58)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.ibm.db2.jcc.t4.w.run(w.java:49)
at java.security.AccessController.doPrivileged(Native Method)
at com.ibm.db2.jcc.t4.ac.a(ac.java:425)
... 14 more
Java Result: 1
BUILD SUCCESSFUL (total time: 25 seconds)
我可以远程登录并 ping 到主机的 IP 地址。
知道了。
该解决方案实际上非常简单,也有点偏离主题。感谢伊恩指出。我必须将db2 的端口号添加到防火墙的入站和出站规则中。
如果有人遇到同样的问题,您可以按照以下步骤操作:
- 转到 Windows 控制面板中的防火墙。
- 单击“高级设置”。
- Select 入站规则,然后单击新建规则。
- Select 端口选项,然后键入要添加的端口号。
- 对出站规则重复上述步骤。
我正在使用这段 java 代码连接到 db2 数据库服务器。
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DatabaseConnect {
Connection co=null;
Statement st=null;
PreparedStatement pstmt;
ResultSet rs;
int status;
void makeConnection() throws ClassNotFoundException, SQLException
{
Class.forName("com.ibm.db2.jcc.DB2Driver");
co=DriverManager.getConnection("jdbc:db2://localhost:50000/databasename","username","password");
//st=co.createStatement();
}
Connection getConnection()
{
return co;
}
void endConnection() throws SQLException
{
co.close();
}
public static void main(String args[]) throws ClassNotFoundException, SQLException
{
DatabaseConnect db=new DatabaseConnect();
db.makeConnection();
new MaintainHash().createHashMap(db.getConnection());
new Login(db.getConnection()).setVisible(true);
}
}
如果主机名是 localhost,它工作正常,但是当我提供远程服务器的主机名时,程序挂起并显示此错误。
Exception in thread "main" com.ibm.db2.jcc.am.DisconnectNonTransientConnectionException: [jcc][t4][2043][11550][3.68.61] Exception java.net.ConnectException: Error opening socket to server /192.168.80.1 on port 50,000 with message: Connection timed out: connect. ERRORCODE=-4499, SQLSTATE=08001
at com.ibm.db2.jcc.am.gd.a(gd.java:329)
at com.ibm.db2.jcc.am.gd.a(gd.java:410)
at com.ibm.db2.jcc.t4.ac.a(ac.java:439)
at com.ibm.db2.jcc.t4.ac.<init>(ac.java:96)
at com.ibm.db2.jcc.t4.a.b(a.java:358)
at com.ibm.db2.jcc.t4.b.newAgent_(b.java:2066)
at com.ibm.db2.jcc.am.Connection.initConnection(Connection.java:780)
at com.ibm.db2.jcc.am.Connection.<init>(Connection.java:725)
at com.ibm.db2.jcc.t4.b.<init>(b.java:333)
at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:233)
at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:199)
at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:474)
at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:115)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at DatabaseConnect.makeConnection(DatabaseConnect.java:31)
at DatabaseConnect.main(DatabaseConnect.java:58)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.ibm.db2.jcc.t4.w.run(w.java:49)
at java.security.AccessController.doPrivileged(Native Method)
at com.ibm.db2.jcc.t4.ac.a(ac.java:425)
... 14 more
Java Result: 1
BUILD SUCCESSFUL (total time: 25 seconds)
我可以远程登录并 ping 到主机的 IP 地址。
知道了。 该解决方案实际上非常简单,也有点偏离主题。感谢伊恩指出。我必须将db2 的端口号添加到防火墙的入站和出站规则中。 如果有人遇到同样的问题,您可以按照以下步骤操作:
- 转到 Windows 控制面板中的防火墙。
- 单击“高级设置”。
- Select 入站规则,然后单击新建规则。
- Select 端口选项,然后键入要添加的端口号。
- 对出站规则重复上述步骤。