java -update数据库通过编辑单元格不起作用?

keg*_*ion 5 java swing jtable

我无法通过编辑/更改单元格值并单击更新按钮来更新数据库中的数据,但他无法更改我更改为单元格的数据库.它仍然与最后一个值相同.

这是我的代码:

    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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

cam*_*ckr 6

String sql ="UPDATE invoice SET description = ?,qty = ?,unitprice = ?" 
                + ",total = ? WHERE id ="+row;
Run Code Online (Sandbox Code Playgroud)

为什么要尝试在where子句的SQL中嵌入变量?

只需使用与其他值相同的参数即可.它将使SQL更简单:

String sql =
    "UPDATE invoice SET description = ?, qty = ?, unitprice = ?, total = ? WHERE id = ?";
...
pst.set???(5, row);
Run Code Online (Sandbox Code Playgroud)