如何从jTable中删除数据库中的数据?

时间:2016-03-29 03:17:34

标签: java sql swing jtable sql-delete

我想从jtable中删除数据库中的数据。我有1个jTextField和1个jButton。因此,当我在表中单击此选定行时,主键不会在我的jTextField中设置。

是否可以在没有jTextField的情况下从jtable中删除数据库中的数据而只是一个按钮?

继承我的代码

try {
    int row = table.getSelectedRow();

    String id_ = (table.getModel().getValueAt(row, 1)).toString();

    String sql ="SELECT id FROM 'mycredentials.sales' WHERE id= "+id_+"'";
    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mycredentials?autoReconnect=true&useSSL=false", username, password);
    PreparedStatement pst = (PreparedStatement) connection.prepareStatement(sql);
    ResultSet rs = pst.executeQuery();

    while (rs.next()) {
        jFrame_removeP.setText(rs.getString("id"));
    }
} catch (SQLException e1) {
    System.err.println(e1);
}

随机ID号出现在我的jTextField中。我的表格代码是:

 String name = jFrame_pName.getText().trim();
 String price = jFrame_pPrice.getText().trim();
 String quantity = jFrame_quantity.getText().trim();
 String total = jFrame_total.getText().trim();
 String st[] = {name, price, quantity, total};
 model.addRow(st);
 jFrame_gTotal.setText(Integer.toString(getSum())); 

 try {
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mycredentials?autoReconnect=true&useSSL=false", username, password);
        Statement s = (Statement) connection.createStatement();
        String sql = "INSERT INTO mycredentials.sales (name, price, quantity, total) VALUES ('"+jFrame_pName.getText()+"', '" + jFrame_pPrice.getText() + "','"+jFrame_quantity.getText()+"','"+jFrame_total.getText()+"')";

        s.executeUpdate(sql);

    } catch (SQLException e1) {
        System.err.println(e1);
    }

我的删除按钮是:

DefaultTableModel model1 = (DefaultTableModel) table_1.getModel();

int selectedRowIndex = table_1.getSelectedRow();
model1.removeRow(selectedRowIndex);

try {
    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mycredentials?autoReconnect=true&useSSL=false", username, password);
    Statement ps = (Statement) connection.createStatement();

    String sql = "DELETE from mycredentials.sales where id ='" + jFrame_removeP.getText() + "'";
    ps.executeUpdate(sql);

} catch (Exception ex) {
    System.err.println(ex);
}

1 个答案:

答案 0 :(得分:1)

你是说这个意思吗?我没有得到你所需要的。希望这会有所帮助。

private void deleteBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          

    String deleteQuery = "DELETE FROM SAMPLE WHERE USER_ID = ?";

    try (Connection myCon = DBUtilities.getConnection(DBType.JDBC);
        PreparedStatement myPs = myCon.prepareStatement(deleteQuery);){

            myPs.setInt(1,userID);

            myPs.executeUpdate();

            JOptionPane.showMessageDialog(null,"Records deleted");
}//end of try 
    catch (SQLException ex) {
        DBUtilities.processException(ex);
}//end of catch
}

enter image description here

搜索记录后。您只需单击要删除的Jtable中的记录。只需点击删除按钮即可。

enter image description here

如果要删除所选行,请在此处使用刷新方法。如果使用Prepared Statement而不是语句来避免SQL注入,请更好地修复语句。