Java 通过JTable删除MySQL数据库中的选定行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20585028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:56:38  来源:igfitidea点击:

Deleting selected row in MySQL database through JTable

javamysqlsqljtablesql-delete

提问by general_bearbrand

  • tb_records = jtable name
  • records = table name inside my database
  • Date = my first column
  • hey = substitute for my real password
  • mydatabase = name of my database
  • tb_records = jtable 名称
  • 记录 = 我的数据库中的表名
  • 日期 = 我的第一列
  • 嘿 = 代替我的真实密码
  • mydatabase = 我的数据库的名称

My problem is that, when I highlight a row in my JTable and delete it, it deletes all the rows. I want to delete the selected row only. Here's my code:

我的问题是,当我突出显示 JTable 中的一行并删除它时,它会删除所有行。我只想删除选定的行。这是我的代码:

int row = tb_records.getSelectedRow();
DefaultTableModel model= (DefaultTableModel)tb_records.getModel();

String selected = model.getValueAt(row, 0).toString();

            if (row >= 0) {

                model.removeRow(row);

                try {
                    Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "hey");
                    PreparedStatement ps = conn.prepareStatement("delete from records where Date='"+selected+"' ");
                    ps.executeUpdate();
                }
                catch (Exception w) {
                    JOptionPane.showMessageDialog(this, "Connection Error!");
                }           
            }

What could be the problem here? How can I delete a selected row in my database and not all the rows?

这里可能有什么问题?如何删除数据库中的选定行而不是所有行?

回答by camickr

1) Don't display your own message. Display the error message from the Exception as it will give a better explanation what the problem is.

1) 不要显示您自己的消息。显示来自异常的错误消息,因为它将更好地解释问题所在。

2) Use a proper PreparedStatement for the SQL. You are less likely to make syntax errors. Something like:

2) 为 SQL 使用适当的 PreparedStatement。您不太可能犯语法错误。就像是:

String sql = "delete from records where Date= ?";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString( 1, selected );
stmt.executeUpdate();

I don't know much about SQL but maybe you need to pass a Date object not a String object since your where clause is using a Date?

我对 SQL 了解不多,但也许您需要传递 Date 对象而不是 String 对象,因为您的 where 子句使用的是日期?

回答by u4370109

The OP wrote:

OP写道:

SOLUTION: Pick a column with unique values. My Date column has the same values that's why it's deleting all my rows even though I set my row as getSelectedRow. Time_in = my 4th column with unique values.

change

String selected = model.getValueAt(row, 0).toString();

to

String selected = model.getValueAt(row, 3).toString();

and

PreparedStatement ps = conn.prepareStatement("delete from records where Date='"+selected+"' ");

to

PreparedStatement ps = conn.prepareStatement("delete from records where Time_in='"+selected+"' ");

解决方案:选择具有唯一值的列。我的日期列具有相同的值,这就是为什么即使我将行设置为 getSelectedRow 也会删除我的所有行。Time_in = 我的第 4 列具有唯一值。

改变

String selected = model.getValueAt(row, 0).toString();

String selected = model.getValueAt(row, 3).toString();

PreparedStatement ps = conn.prepareStatement("delete from records where Date='"+selected+"' ");

PreparedStatement ps = conn.prepareStatement("delete from records where Time_in='"+selected+"' ");

回答by Messou

DefaultTableModel model = (DefaultTableModel) jTable.getModel();
        int row = jTable.getSelectedRow();
        String eve = jTable.getModel().getValueAt(row, 0).

        String delRow = "delete from user where id="+eve;
        try {
            ps = myCon.getConnection().prepareStatement(delRow);
            ps.execute();
            JOptionPane.showMessageDialog(null, "Congratulation !!");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,  e.getMessage());
        }