【发布时间】:2015-03-30 07:05:33
【问题描述】:
我的代码中有一个循环。它遍历结果集。每当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;
}
【问题讨论】:
-
我建议您更改执行 SQL 的方式以使用
PreparedStatements。这是为了防止 SQL 注入 -
您有 SQL 注入漏洞。您可以使用
PreparedStatements和占位符?将其删除,稍后您将其设置为要在查询中使用的数据,例如stmt.setInt(1, pdno); -
"然后它刚刚从 while 循环中出来。它不会进行下一次迭代" 是否有可能在更新您的代码时引发异常,从而破坏您的循环?我们可以看看你是如何处理
SQLException的吗? -
不要仅以 cmets 的形式发布重要信息(尤其是无法在注释中正确格式化的代码示例)。而是使用 edit 选项将其添加到您的问题中。
-
是否有任何错误代码可以通过 Eclipse 或其他方式显示给我们?
标签: java database loops jdbc resultset