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

そのコードはコンパイルできないようです。代わりに、準備済みステートメントのパラメーターを使用してください。

  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 に答える