gpt4 book ai didi

java - DB2 连接 - 从 IBM DataStudio 工作但从 java 程序失败

转载 作者:行者123 更新时间:2023-12-02 02:54:52 24 4
gpt4 key购买 nike

我正在尝试使用 JDBC 连接从我的 Windows 计算机连接到 DB2 数据库。

我能够使用 IBM Data Studio 客户端中的相同凭证成功连接,但是当我从 Java 程序中尝试相同的凭据时,出现以下异常。

com.ibm.db2.jcc.am.io: [jcc][t4][2043][11550][4.7.89] Exception java.net.ConnectException: Error opening socket to server lnx1iitmon05s/XXXXXX on port YYYY with message: Connection refused: connect. ERRORCODE=-4499, SQLSTATE=08001
at com.ibm.db2.jcc.am.dd.a(dd.java:319)
at com.ibm.db2.jcc.am.dd.a(dd.java:337)
at com.ibm.db2.jcc.t4.xb.a(xb.java:378)
at com.ibm.db2.jcc.t4.xb.<init>(xb.java:76)
at com.ibm.db2.jcc.t4.a.y(a.java:263)
at com.ibm.db2.jcc.t4.b.a(b.java:1786)
at com.ibm.db2.jcc.am.lb.a(lb.java:538)
at com.ibm.db2.jcc.am.lb.<init>(lb.java:494)
at com.ibm.db2.jcc.t4.b.<init>(b.java:309)
at com.ibm.db2.jcc.DB2SimpleDataSource.getConnection(DB2SimpleDataSource.java:214)
at com.ibm.db2.jcc.DB2Driver.connect(DB2Driver.java:224)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at com.genericframework.db.RRDBConnection.getConnection(RRDBConnection.java:63)
at com.genericframework.db.RRDBConnection.main(RRDBConnection.java:77)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at com.ibm.db2.jcc.t4.y.run(y.java:34)
at java.security.AccessController.doPrivileged(Native Method)
at com.ibm.db2.jcc.t4.xb.a(xb.java:372)
... 12 more

这是我的代码..

 try {
String filename = "com/genericframework/db/RRDBConnection.properties";
//input = SPARKApp.class.getClassLoader().getResourceAsStream(filename);
input = getClass().getClassLoader().getResourceAsStream(filename);

// load a properties file
prop.load(input);
} catch (FileNotFoundException ex) {
Logger.getLogger(RRDBConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(RRDBConnection.class.getName()).log(Level.SEVERE, null, ex);
}

dbHostName = prop.getProperty("DBHost");
dbPort = prop.getProperty("DBPort");
dbDatabasename = prop.getProperty("DBname");
dbUsername = prop.getProperty("DBUserName");
dbPassword = prop.getProperty("DBPassword");



String connectionString = "jdbc:db2://"+dbHostName+":"+dbPort+"/"+dbDatabasename;

System.out.println(connectionString);
Connection connection=null;
try {
//Class.forName( "COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriver");
Class.forName( "com.ibm.db2.jcc.DB2Driver");
connection = DriverManager.getConnection(connectionString,dbUsername,dbPassword);

} catch (ClassNotFoundException ex) {
Logger.getLogger(RRDBConnection.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(RRDBConnection.class.getName()).log(Level.SEVERE, null, ex);
}

最佳答案

请尝试以下代码:

        try{  
Class.forName(className);
//System.out.println("class not found");
con=DriverManager.getConnection(
dbURL,dbName,dbPassword);
Statement stmt=con.createStatement();

//your operations like select,insert or update

catch(Exception e) {
System.out.println(e.getMessage());
}
finally {
con.close();
}

并添加maven依赖项:

<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>licences</artifactId>
<version>1.0</version>
<!-- Adjust this properly -->
<scope>system</scope>
<systemPath>${basedir}/lib/db2jcc_license_cu.jar</systemPath>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc4</artifactId>
<version>1.0</version>
<!-- Adjust this properly -->
<scope>system</scope>
<systemPath>${basedir}/lib/db2jcc4.jar</systemPath>
</dependency>

或使用下面的 jar :

1. db2jcc.jar 
2. db2jcc_license_cu.jar

关于java - DB2 连接 - 从 IBM DataStudio 工作但从 java 程序失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43272806/

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