使用来自 jtable 单元格的 where 参数更新 mysql table
updating mysql table using where parameter from jtable cell
我有一个 MySQL Table 列(itemCode,itemCount),
我有一个 jTable 和 Columns (itemCode, itemCount, AddItemCount)
我想用 jTable 上的数据更新 MySQL table itemCount,
但我不知道如何使用可以根据每个 jTable 行中的 itemCode 值更改的 where 参数 ( itemCode )。
换句话说,我想将数据库 table 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 循环之外,它不会获得需要作为参数的 "row",
而如果我将 SQL 命令放在 for 循环中,它只会更新最后一行,因为命令会在每个循环中不断重复。
如果 where 参数只取一个值(例如来自 jtextfield ),它可以正常工作。
I dont know how to use a where Parameter ( itemCode ) that can change based on the value of itemCode in each jTable row.
我不明白混乱。您可以像为 "ItemCount" 指定参数一样指定参数:
String SQLupdate = "UPDATE tableItem SET ItemCount= ? WHERE ItemCode = ?";
ps = con.prepareStatement(SQLupdate);
for(int row=0; row<rows; row++)
{
String itemCode = (String)tabelDetailDO.getValueAt(row, 0);
itemCount = (int) tabelDetailDO.getValueAt(row, 2);
addItemCount = (int) tabelDetailDO.getValueAt(row, 3);
totalItemCount = itemCount + addItemCount;
ps.setInt(1, totalItemCount);
ps.setString(2, itemCode);
ps.addBatch();
}
请注意,我从未使用过批处理更新,所以首先尝试让逻辑在没有批处理的情况下工作,因此您需要在循环中每次都执行更新。是的,我知道它效率不高,但您只是在测试 SQL。
然后,如果可行,您可以尝试使用批量更新。
我有一个 MySQL Table 列(itemCode,itemCount), 我有一个 jTable 和 Columns (itemCode, itemCount, AddItemCount)
我想用 jTable 上的数据更新 MySQL table itemCount, 但我不知道如何使用可以根据每个 jTable 行中的 itemCode 值更改的 where 参数 ( itemCode )。
换句话说,我想将数据库 table 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 循环之外,它不会获得需要作为参数的 "row",
而如果我将 SQL 命令放在 for 循环中,它只会更新最后一行,因为命令会在每个循环中不断重复。
如果 where 参数只取一个值(例如来自 jtextfield ),它可以正常工作。
I dont know how to use a where Parameter ( itemCode ) that can change based on the value of itemCode in each jTable row.
我不明白混乱。您可以像为 "ItemCount" 指定参数一样指定参数:
String SQLupdate = "UPDATE tableItem SET ItemCount= ? WHERE ItemCode = ?";
ps = con.prepareStatement(SQLupdate);
for(int row=0; row<rows; row++)
{
String itemCode = (String)tabelDetailDO.getValueAt(row, 0);
itemCount = (int) tabelDetailDO.getValueAt(row, 2);
addItemCount = (int) tabelDetailDO.getValueAt(row, 3);
totalItemCount = itemCount + addItemCount;
ps.setInt(1, totalItemCount);
ps.setString(2, itemCode);
ps.addBatch();
}
请注意,我从未使用过批处理更新,所以首先尝试让逻辑在没有批处理的情况下工作,因此您需要在循环中每次都执行更新。是的,我知道它效率不高,但您只是在测试 SQL。
然后,如果可行,您可以尝试使用批量更新。