java sql update / delete什么都不做

时间:2013-01-03 00:52:43

标签: java sql

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

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

   }

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分:1)

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

String sql="update publishers set publisher_id='"+value1+"', publisher_title='"+value2+"', publisher_name='"+value3+"', publisher_lastname='"+value4+"' WHERE publisher_id='"+value1+"'";

答案 2 :(得分: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();
}