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

Java 如何从jTable中删除数据库中的数据?,java,sql,swing,jtable,sql-delete,Java,Sql,Swing,Jtable,Sql Delete,我想从jtable中删除数据库中的数据。我有一个jTextField和一个jButton。所以,当我在表中单击这个选中的行时,主键不会在我的jTextField中设置 不需要jTextField,只需一个按钮,就可以从jtable中删除数据库中的数据吗 这是我的密码 try { int row = table.getSelectedRow(); String id_ = (table.getModel().getValueAt(row, 1)).toString();

我想从jtable中删除数据库中的数据。我有一个jTextField和一个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);
}

你是说像这样吗?我没有得到你需要的东西。希望这有帮助

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
}

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

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

“是否可以在不使用jTextField而只需一个按钮的情况下从jtable中删除数据库中的数据?”通常是这样的。首先创建一个表示一行数据的pojo,这将使工作更轻松,然后只需从表中获取数据并将其删除即可