我正在尝试在db2中执行查询。虽然语句“SELECT PacientID FROM Spital.Pacient”在CLP中没有问题,但它在我编写的java程序中不起作用。
public static void main(String[] args) {
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
Connection myConn = DB2Conn1.openConnection("admin", "admin", "jdbc:db2://localhost:50000/MYDB");
if(!myConn.isClosed()) {
System.out.println("Connectat!");
}
String nume;
Statement stmt;
ResultSet rs;
stmt = myConn.createStatement();
rs = stmt.executeQuery("SELECT PacientID FROM Spital.Pacient");
while(rs.next()) {
nume = rs.getString(1);
}
rs.close();
stmt.close();
DB2Conn1.closeConnection(myConn);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
控制台输出:
Connectat!
com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=SPITAL.PACIENT, DRIVER=3.69.49
at com.ibm.db2.jcc.am.gd.a(Unknown Source)
at com.ibm.db2.jcc.am.gd.a(Unknown Source)
at com.ibm.db2.jcc.am.gd.a(Unknown Source)
at com.ibm.db2.jcc.am.yo.c(Unknown Source)
at com.ibm.db2.jcc.am.yo.d(Unknown Source)
at com.ibm.db2.jcc.am.yo.a(Unknown Source)
at com.ibm.db2.jcc.t4.bb.h(Unknown Source)
at com.ibm.db2.jcc.t4.bb.b(Unknown Source)
at com.ibm.db2.jcc.t4.p.a(Unknown Source)
at com.ibm.db2.jcc.t4.vb.i(Unknown Source)
at com.ibm.db2.jcc.am.yo.ib(Unknown Source)
at com.ibm.db2.jcc.am.yo.a(Unknown Source)
at com.ibm.db2.jcc.am.yo.a(Unknown Source)
at com.ibm.db2.jcc.am.yo.executeQuery(Unknown Source)
at mydbprog.tester.main(tester.java:19)