我的代码中有一个循环。它遍历结果集。每当 qc = 0
的值时,它应该更新数据库中的一个表。
但它只是在第一次遇到 qc = 0
时才更新数据库表。然后它就从 while 循环中出来了。它不会在下一次迭代中检查“qc”的另一个值
String sql5 = "select t$pdno ,t$opno, t$qcmp , t$qtbf from ttisfc010100 where t$pdno = "+pdno+" and t$opno <= "+opno ;
ResultSet getData3 = db.getStmt().executeQuery(sql5);
while(getData3.next()) {
int pd , op , qc , qt ;
pd = getData3.getInt(1);
op = getData3.getInt(2);
qc = getData3.getInt(3);
qt = getData3.getInt(4);
String sql7="";
if(qc==0)
{
sql7 = "update ttisfc010100 set t$qcmp = "+quantity+" , t$qtbf = "+quantity+" where t$pdno = "+pd+" and t$opno = "+op;
db.getStmt().executeUpdate(sql7);
}
}
catch (SQLException e) {
e.printStackTrace(); }
getStmt()
方法包含创建普通语句的逻辑。见下文:
public class DatabaseConnection
{
Statement stmt;
public void openConnection() {
Class.forName(classForName);
conn = DriverManager.getConnection(driver+dsn, user, password);
stmt = conn.createStatement();
}
public Statement getStmt()
{
if(stmt==null)
{
stmt = this.getNewStmt();
}
return stmt;
}
public Statement getNewStmt() {
Statement newStmt;
try {
newStmt = conn.createStatement();
}
catch(Exception e){
newStmt = stmt;
}
return newStmt;
}
最佳答案
我已经删除了 Normal 语句的用法,并在我的代码中使用了准备好的语句。现在这解决了我的问题。
非常感谢。你们真棒。
关于java - 遍历结果集,我需要在满足条件时更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28242103/