我正在尝试一个试点项目来查询和写入 DB2 数据库。我在 StackOverFlow 和网络上看到了很多例子。但是由于某种原因,我的应用程序在连接到数据库时只是挂起。我尝试了不同的驱动程序,但仍然得到相同的结果。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.PreparedStatement;
public class DBManager {
public DBManager()
{
try
{
Class.forName("com.ibm.db2.jcc.DB2Driver");
}
catch (ClassNotFoundException e)
{
System.out.println("Please include Classpath Where your DB2 Driver is located");
e.printStackTrace();
return;
}
System.out.println("DB2 driver is loaded successfully");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset=null;
boolean found=false;
try
{
System.out.println("DB2 Database Connecting...");
conn = DriverManager.getConnection("jdbc:db2://Hostname:8471/Databasename","user","password");
if (conn != null)
{
System.out.println("DB2 Database Connected");
}
else
{
System.out.println("Db2 connection Failed ");
}
pstmt=conn.prepareStatement("SELECT * FROM P6DEVCDB00.ADCP FETCH FIRST 10 ROWS ONLY");
rset=pstmt.executeQuery();
if(rset!=null)
{
while(rset.next())
{
found=true;
System.out.println("ID: "+rset.getString("ADCONTID"));
}
}
if (found ==false)
{
System.out.println("No Information Found");
}
}
catch (SQLException e)
{
System.out.println("DB2 Database connection Failed");
e.printStackTrace();
return;
}
}
}
更新 的应用程序挂起,没有异常
conn = DriverManager.getConnection(.
我也尝试过使用不同的端口号,但有一些错误(它们确实不同)和其他只是挂起。