0

我创建了一个连接到 MySQL 工作台的小型 SWING 应用程序。我正在尝试为 jbutton 编写代码,当按下它时它会更新数据库中的表。我在 StackOverflow 上完成了所有问答,但查询仍然无法在数据库内执行更新。我尝试通过放置一些打印语句来调试代码,我注意到当某个单元格被修改时,准备好的语句没有收到更新。然而,没有抛出异常。我该如何解决这个问题?

JButton update_Button = new JButton("UPDATE ");
                update_Button.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        Connection connection = null;   
                        
                        int selectIndex = table_1.getSelectedRow();
                        int id = Integer.parseInt(model.getValueAt(selectIndex, 0).toString());
                                                                        
                        try {
                            
                            connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/librarydatabase", "root", "Connection");  
                            connection.setAutoCommit(false);
                            String sql = "UPDATE book SET author = ?, title = ?,subject = ?, publisher = ?, language = ? WHERE bookID = ?";
                            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);                                                                                     
                            statement.setString(1, author_TextField.getText());
                            statement.setString(2, title_TextField.getText());
                            statement.setString(3, subject_ComboBox.getSelectedItem().toString());
                            statement.setString(4, publisher_ComboBox.getSelectedItem().toString());
                            statement.setString(5, language_ComboBox.getSelectedItem().toString());
                            statement.setInt(6, id);
                                                                                                                                                        
                            statement.execute();    
                            connection.commit();
                            
                            JOptionPane.showMessageDialog(null, "The book has been updated successfully");
                            
                            statement.close();
                            connection.close();
                            
                    
                    } catch (Exception e) {
                        e.printStackTrace();
                    }catch(Throwable e) 
                    {
                        e.printStackTrace();
                    }
                    }
                });
4

2 回答 2

0

尝试使用

statement.executeUpdate();

代替

statement.execute();
于 2020-09-22T15:15:32.833 回答
0

我已经尝试过您的代码,并且我知道您最好使用executeUpdate方法。如果您可以为数据库连接创建不同的类,那么最好不要在同一个类中创建所有方法和数据库连接。

        Connection conn;
        conn = DbConnect.connect();

        int colum;
        int row = table.getSelectedRow();
        String data = table.getValueAt(row, 2).toString();
        System.out.println(data);

        try {
            String sql = "UPDATE book SET author = ?, title = ?,subject = ?, publisher = ?, language = ? WHERE id = ?";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, table.getValueAt(row, 1).toString());
            pst.setString(2, table.getValueAt(row, 2).toString());
            pst.setString(3, table.getValueAt(row, 3).toString());
            pst.setString(4, table.getValueAt(row, 4).toString());
            pst.setString(5, table.getValueAt(row, 5).toString());
            pst.setString(6, table.getValueAt(row, 0).toString());
            int a = pst.executeUpdate();
            if (a > 0) {
                System.out.println("Successfully update");
            } else {
                System.out.println("Faild to update");
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

输出:成功更新

于 2020-09-22T16:30:30.980 回答