我有一个带列的 MySQL 表(itemCode、itemCount),我有一个带列的 jTable(itemCode、itemCount、AddItemCount)
我想用 jTable 上的数据更新 MySQL 表 itemCount,但我不知道如何使用可以根据每个 jTable 行中 itemCode 的值更改的 where 参数 ( itemCode )。
换句话说,我想将数据库表 itemCode 与每行中的 jTable itemCode 匹配,然后更新匹配 itemCode 的 itemCount。
我尝试过的(绝对不起作用):
int itemCount, addItemCount, totalItemCount;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con.setAutoCommit(false);
int rows = tabelDetailDO.getRowCount();
for(int row=0; row<rows; row++) {
String SQLupdate = "UPDATE tableItem SET ItemCount=? WHERE ItemCode = '"+(String) tabelDetailDO.getValueAt(row, 0)+"' ";
ps = con.prepareStatement(SQLupdate);
itemCount = (int) tabelDetailDO.getValueAt(row, 2);
addItemCount = (int) tabelDetailDO.getValueAt(row, 3);
totalItemCount = itemCount + addItemCount;
ps.setInt(1, totalItemCount);
ps.addBatch();
}
ps.executeBatch();
con.commit();
}
catch (Exception e) {
JOptionPane.showMessageDialog(rootPane, e);
}
如果我把 SQL 命令放在 for 循环之外,它不会得到需要作为参数的“行”,
而如果我将 SQL 命令放在 for 循环中,它只会更新最后一行,因为命令只会在每个循环中不断重复。
如果 where 参数只取一个值(例如来自 jtextfield ),它会正常工作。
DIEA
相关分类