select 在 while 循环中查询
select query inside a while loop
我在 While 循环中有一个 SELECT 查询。我期待单行。得到那一行后,我想退出 while 循环。问题是,在我执行 select 查询并检查结果集时的第一次迭代之后,出现 "Invalid Cursor" 错误。不确定我在这里做错了什么。
//looping thru hashmap
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
int bind_index=1;
Map.Entry pair = (Map.Entry)it.next();
log.debug(pair.getKey() + " = " + pair.getValue());
String sql_main = null;
sql_main = getSql(sb2.toString());
ResultSet resultSet = null;
PreparedStatement p_stmt1 = null;
p_stmt1 = db.getPreparedStatement(sql_main);
p_stmt1.setString(bind_index++, (String)pair.getKey());
p_stmt1.setString(bind_index++, (String)pair.getValue());
p_stmt1.setString(bind_index++,strGMTDate+" "+strGMTTime);
p_stmt1.setString(bind_index++,strGMTDate+" "+strGMTTime);
resultSet = db.executeQuery(p_stmt1);
CachedRowSetImpl rsCopy2 = new CachedRowSetImpl();
rsCopy2.populate(resultSet);
if(rsCopy2.next()){
rsCopy2.beforeFirst();
grid.bind(rsCopy2);
break;
}
resultSet = null;
p_stmt1=null;
resultSet.close();
p_stmt1.close();
}
首先,由于您的 SQL 没有改变,您应该在循环外准备语句。此外,您应该使用 try-with-resources 来确保 JDBC 资源被清理。
还有,你的executeQuery
错了。
String sql_main = getSql(sb2.toString());
try (PreparedStatement p_stmt1 = db.getPreparedStatement(sql_main)) {
for (Map.Entry pair : map.entrySet()) {
log.debug(pair.getKey() + " = " + pair.getValue());
int bind_index = 0;
p_stmt1.setString(++bind_index, (String)pair.getKey());
p_stmt1.setString(++bind_index, (String)pair.getValue());
p_stmt1.setString(++bind_index, strGMTDate + " " + strGMTTime);
p_stmt1.setString(++bind_index, strGMTDate + " " + strGMTTime);
try (ResultSet resultSet = p_stmt1.executeQuery()) {
if (resultSet.next()) {
// Capture/use values from resultSet here
break;
}
}
}
}
我在 While 循环中有一个 SELECT 查询。我期待单行。得到那一行后,我想退出 while 循环。问题是,在我执行 select 查询并检查结果集时的第一次迭代之后,出现 "Invalid Cursor" 错误。不确定我在这里做错了什么。
//looping thru hashmap
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
int bind_index=1;
Map.Entry pair = (Map.Entry)it.next();
log.debug(pair.getKey() + " = " + pair.getValue());
String sql_main = null;
sql_main = getSql(sb2.toString());
ResultSet resultSet = null;
PreparedStatement p_stmt1 = null;
p_stmt1 = db.getPreparedStatement(sql_main);
p_stmt1.setString(bind_index++, (String)pair.getKey());
p_stmt1.setString(bind_index++, (String)pair.getValue());
p_stmt1.setString(bind_index++,strGMTDate+" "+strGMTTime);
p_stmt1.setString(bind_index++,strGMTDate+" "+strGMTTime);
resultSet = db.executeQuery(p_stmt1);
CachedRowSetImpl rsCopy2 = new CachedRowSetImpl();
rsCopy2.populate(resultSet);
if(rsCopy2.next()){
rsCopy2.beforeFirst();
grid.bind(rsCopy2);
break;
}
resultSet = null;
p_stmt1=null;
resultSet.close();
p_stmt1.close();
}
首先,由于您的 SQL 没有改变,您应该在循环外准备语句。此外,您应该使用 try-with-resources 来确保 JDBC 资源被清理。
还有,你的executeQuery
错了。
String sql_main = getSql(sb2.toString());
try (PreparedStatement p_stmt1 = db.getPreparedStatement(sql_main)) {
for (Map.Entry pair : map.entrySet()) {
log.debug(pair.getKey() + " = " + pair.getValue());
int bind_index = 0;
p_stmt1.setString(++bind_index, (String)pair.getKey());
p_stmt1.setString(++bind_index, (String)pair.getValue());
p_stmt1.setString(++bind_index, strGMTDate + " " + strGMTTime);
p_stmt1.setString(++bind_index, strGMTDate + " " + strGMTTime);
try (ResultSet resultSet = p_stmt1.executeQuery()) {
if (resultSet.next()) {
// Capture/use values from resultSet here
break;
}
}
}
}