我无法通过编辑/更改单元格值并单击更新按钮来更新数据库中的数据,但他无法更改我更改为单元格的数据库。它与最后一个值仍然相同。
这是我的代码:
public void directlyup() {
int col=tablesample.getSelectedColumn();
int row=tablesample.getSelectedRow();
int index;
index = Integer.parseInt(tablesample.getModel().getValueAt(row, 0).toString());
try {
String sql ="UPDATE invoice SET description = ?,qty = ?,unitprice = ?"
+ ",total = ? WHERE id ="+row;
pst = conn.prepareStatement(sql);
pst.setString(1, (String) tablesample.getValueAt(row, 1));
pst.setString(2, (String) tablesample.getValueAt(row, 2));
pst.setString(3, (String) tablesample.getValueAt(row, 3));
pst.setString(4, (String) tablesample.getValueAt(row, 4));
pst.execute();
JOptionPane.showMessageDialog(null, "Successfully Updated");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
答案 0 :(得分:6)
String sql ="UPDATE invoice SET description = ?,qty = ?,unitprice = ?"
+ ",total = ? WHERE id ="+row;
为什么要尝试在where子句的SQL中嵌入变量?
只需使用与其他值相同的参数即可。它将使SQL更简单:
String sql =
"UPDATE invoice SET description = ?, qty = ?, unitprice = ?, total = ? WHERE id = ?";
...
pst.set???(5, row);
答案 1 :(得分:2)
对于数据集上的CRUD操作,最好使用中间表。这避免了大量数据集上的大量查询。
在提出解决问题的建议之前,我想指出一些关于数据库结构的评论:
total
字段显然是计算字段。这样的信息不适合放入数据库。它是根据要求计算的。另外,我想说这样的决定是针对特定的数据库做出的。在这种情况下,我的解决方案涉及mysql 。
这是底部代码段运行的表格的DDL
CREATE TABLE `invoice` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`invoice_id` int(10) unsigned NOT NULL,
`description` varchar(255) DEFAULT NULL,
`qty` double DEFAULT NULL,
`unitprice` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB
这是使用文档ID密钥(invoce_id
)对数据集进行CRUD操作的代码。
public boolean save(long invoice_id, List<Invoice> list) throws SQLException {
try(Connection connection = getConnection()) {
try {
connection.setAutoCommit(false);
String query =
"create temporary table if not exists `invoice_tmp` (" +
"`id` int(10) unsigned NOT NULL," +
"`description` varchar(255) DEFAULT NULL," +
"`qty` double DEFAULT NULL," +
"`unitprice` double DEFAULT NULL)";
connection.createStatement().executeUpdate(query);
query = "insert into `invoice_tmp` values (?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(query);
for(Invoice invoice: list) {
statement.setLong(1, invoice.getId());
statement.setString(2, invoice.getDescription());
statement.setDouble(3, invoice.getQty());
statement.setDouble(4, invoice.getUnitPrice());
statement.addBatch();
}
statement.executeBatch();
statement.close();
query =
"delete invoice from invoice " +
"left join invoice_tmp on (invoice.id = invoice_tmp.id) " +
"where invoice_id = ? and invoice_tmp.id is null";
statement = connection.prepareStatement(query);
statement.setLong(1, invoice_id);
statement.executeUpdate();
statement.close();
query =
"update `invoice` " +
"join `invoice_tmp` using (`id`) " +
"set " +
"`invoice`.description = `invoice_tmp`.description, " +
"`invoice`.qty = `invoice_tmp`.qty, " +
"`invoice`.unitprice = `invoice_tmp`.unitprice";
connection.createStatement().executeUpdate(query);
query =
"insert into `invoice` (`invoice_id`, `description`, `qty`, `unitprice`) " +
"select ? as `invoice_id`, `description`, `qty`, `unitprice` from `invoice_tmp` where `id` = 0";
statement = connection.prepareStatement(query);
statement.setLong(1, invoice_id);
statement.executeUpdate();
statement.close();
connection.createStatement().executeUpdate("drop table if exists `invoice_tmp`");
connection.commit();
return true;
}
catch (Exception e) {
connection.rollback();
throw e;
}
}
}
this是一个测试项目,用于演示上述代码的工作原理。
答案 2 :(得分:1)
您可能打算在WHERE子句中使用索引而不是行?
String sql ="UPDATE invoice SET description = ?,qty = ?,unitprice = ?"
+ ",total = ? WHERE id ="+index;
答案 3 :(得分:1)
假设您选择了多行,如果您的id为int:
int[]rows=tablesample.getSelectedRows();
String sql="UPDATE invoice SET description = ?,qty = ?,unitprice = ?"
+ ",total = ? WHERE id =?";
try{
PreparedStatement pst = conn.prepareStatement(sql);
for(int currentRow:rows){
pst.setString(1, (String) tablesample.getValueAt(currentRow, 1));
pst.setString(2, (String) tablesample.getValueAt(currentRow, 2));
pst.setString(3, (String) tablesample.getValueAt(currentRow, 3));
pst.setString(4, (String) tablesample.getValueAt(currentRow, 4));
pst.setInt(5, currentRow);
pst.executeUpdate();
}
JOptionPane.showMessageDialog(null, "Successfully Updated");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
} finally{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
为了获得最佳性能,您可以像example
一样使用executeBatch