更新后SELECT查询的java意外行为
以下是我的java代码中的逻辑:
con.setAutoCommit(false);
int starIndex = 0;
List Details = new ArrayList();
while (true) {
isRsEmpty = true;
String my_Query = "select * from x,y,z where"
+ " x.a = y.b"
+ " x.a = z.c"
+ "y.b = z.e"
+ " ORDER BY a ASC"
+ " limit 5"
+ " offset " + starIndex;
preparedStatement = con.prepareStatement(my_Query);
rs = preparedStatement.executeQuery();
while (rs.next()) {
isRsEmpty = false;
//Other processing steps
starIndex++;
Details.add(rs.getInt("id"));
}
if(isRsEmpty){
starIndex = 0;
}
Iterator itr = Details.iterator();
String updateTable = "UPDATE x SET status = ? where i = ? and j = ? and k = ?";
updatePreparedStatement = con.prepareStatement(updateTable);
while (itr.hasNext()) {
updatePreparedStatement.setInt(1, 1);
updatePreparedStatement.setString(2, "zzz");
updatePreparedStatement.setInt(3, 9);
updatePreparedStatement.setInt(4, 10);
updatePreparedStatement.addBatch();
}
updatePreparedStatement.executeBatch();
con.commit();
Details.clear();
}
问题:
我的表中有13个符合select查询的条目
当我第一次运行查询时,我的限制是5,偏移量是0,我得到 从表中正确选择5条记录,并更新所选的5条记录
当select查询再次运行时,对表进行更新后,它会给我3条记录这是出乎意料的,因为我的表中还有8条记录。再次更新3条记录
同样,当select查询运行时,它会给我0条记录
然后再次运行一个select查询,它会给我5条记录并更新这5条记录
我无法理解这种行为。如果我在没有更新的情况下运行select查询,那么它将正确运行,这将为我提供5,5,3条记录,但按照上述逻辑,它将为我提供5,3,0,5条记录
这里有什么问题
注:在上面的程序中,所有变量,如PreparedStatement和其他变量都是正确声明的
谢谢!
# 1 楼答案
在select查询中,您的条件之间是否有可能缺少“and”关键字
我不知道这是否能解决问题,但我觉得这样做很奇怪