2

我不确定这是不是这个地方,但我没有想法,下面是我的程序的更新代码,它很简单,但由于某种原因不起作用。

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);
        }

   }
4

3 回答 3

3

该代码看起来无法编译。改用preparedstatement的参数。

  String sql="update publishers set publisher_id=?, publisher_title=?, publisher_name=?, publisher_lastname=? WHERE publisher_id=?";
    pst=conn.prepareStatement(sql);
    pst.setString(1, value1);
    pst.setString(2, value2);
    pst.setString(3, value3);
    pst.setString(4, value4);
    pst.setString(5, value1);
    pst.execute();
于 2013-01-03T00:58:18.943 回答
1

该代码无法编译,因为您没有在查询中正确连接字符串,它应该是:

String sql="update publishers set publisher_id='"+value1+"', publisher_title='"+value2+"', publisher_name='"+value3+"', publisher_lastname='"+value4+"' WHERE publisher_id='"+value1+"'";
于 2013-01-03T01:01:52.340 回答
1

修改您的程序,以便您使用:

Statement st = con.createStatement();
int updateCount = st.executeUpdate(sql);
if(updateCount <= 0) {
  System.out.println("Nothing update in database");
} else {
  System.out.println("Number of records updated: " + updateCount);
}
st.close();

还要确保您没有禁用自动提交。

if(!con.getAutoCommit()) {
  con.commit();
}
于 2013-01-03T01:30:31.823 回答