我创建了一个连接到 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();
}
}
});