在使用JDBC连接时,我尝试执行2个SELECT语句。第一个返回表中的单个记录(SQL_IBGN),第二个记录设计为使用第一个查询中的列(BGNREF)从另一个表(SQL_ITXN)返回多个记录。
我为每个查询都有单独的Statement和ResultSet对象。
查询代码1:
Statement stmt = con.createStatement();
String sql = "SELECT BGNREF..... FROM SQL_IBGN WHERE BGNREF LIKE '2306009';";
ResultSet rs = stmt.executeQuery(sql);
String bgnref = null;
while (rs.next()) {
G1SQL_IBGN g1rec = new G1SQL_IBGN();
bgnref = rs.getString(1);
g1rec.setBGNREF(bgnref);
}
查询代码2:
if (bgnref != null) {
Statement stmt1 = con.createStatement();
String sql1 = "SELECT ACT_DESC...... SQL_ITXN WHERE BGNREF LIKE '"
+ bgnref + "';"; // Execute the SELECT statement ResultSet
ResultSet rs1 = stmt1.executeQuery(sql1); // Get result of first five records
while (rs1.next()) {
G1SQL_ITXN g1itxn = new G1SQL_ITXN();
g1itxn.setACT_DESC(rs1.getString(1));
}
}
第一个查询返回单个记录,但即使rs1.next()= true,第二个while循环,而(rs1.next())也不执行。