我不确定这是不是这个地方,但我没有想法,下面是我的程序的更新代码,它很简单,但由于某种原因不起作用。
private void txt_updateActionPerformed(java.awt.event.ActionEvent evt) {
try{
String value1= txt_id.getText();
String value2= txt_title.getText();
String value3= txt_firstname.getText();
String value4= txt_lastname.getText();
String sql="update publishers set publisher_id='"value1"', publisher_title='"value2"', publisher_name='"value3"', publisher_lastname='"value4"' WHERE publisher_id='"value1"'";
pst=conn.prepareStatement(sql);
pst.execute();
JOptionPane.showMessageDialog(null, "Updated");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
Update_table();
}
我不确定出了什么问题。我试着改变周围的东西,但没有帮助。它在程序中工作,但实际上并不更新数据库。所以当我重新启动程序时没有任何改变
好的,我也尝试了以下操作,但没有成功。
private void txt_updateActionPerformed(java.awt.event.ActionEvent evt) {
try{
String value1= txt_id.getText();
String value2= txt_title.getText();
String value3= txt_firstname.getText();
String value4= txt_lastname.getText();
String sql="update publishers set publisher_id='" + value1 + "', publisher_title='" + value2 + "',publisher_name='" + value3 + "', publisher_lastname='" + value4 + "'WHERE publisher_id='" + value1 + "'";
pst=conn.prepareStatement(sql);
pst.execute();
conn.setAutoCommit(true);
JOptionPane.showMessageDialog(null, "Updated");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
Update_table();
}
和这个
private void txt_updateActionPerformed(java.awt.event.ActionEvent evt) {
try{
String sql="update publishers set publisher_id=?, publisher_title=?, publisher_name=?, publisher_lastname=? WHERE publisher_id=?";
pst=conn.prepareStatement(sql);
pst.setString(1, txt_id.getText());
pst.setString(2, txt_title.getText());
pst.setString(3, txt_firstname.getText());
pst.setString(4, txt_lastname.getText());
pst.setString(5, txt_id.getText());
pst.execute();
conn.setAutoCommit(true);
JOptionPane.showMessageDialog(null, "Updated");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
Update_table();
}
update_table 如下
private void Update_table(){
try{
String sql="select publisher_id, publisher_title, publisher_name, publisher_lastname from publishers";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
Table_Publishers.setModel(DbUtils.resultSetToTableModel(rs));
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}