【问题标题】:How to remove data in database from jTable?如何从jTable中删除数据库中的数据?
【发布时间】:2016-03-29 03:17:33
【问题描述】:

我想从 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);
}

【问题讨论】:

  • “是否可以在没有 jTextField 和一个按钮的情况下从 jtable 中删除数据库中的数据?” 一般来说,是的。首先创建一个代表一行数据的pojo,它会让生活更轻松,然后你只需要从表中获取数据并删除它

标签: java sql swing jtable sql-delete


【解决方案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
}

搜索记录后。您只需单击要删除的 Jtable 中的记录。就这么简单地点击删除按钮。

如果要删除选定的行,只需在此处使用刷新方法。如果您使用 Prepared Statement 而不是 Statements 来避免 SQL 注入,则可以更好地修复您的语句。

【讨论】:

    猜你喜欢
    • 2015-02-11
    • 2018-06-26
    • 2016-11-08
    • 2014-01-17
    • 2014-03-14
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多