2017-01-06 10 views
0

管理者は、これはJTableのが示されているクラスですID列で選択した行を更新するには?

//For Update 
public AdminCreateQnsPanel(JFrame mf, QuizDetails q, int set, String topic) { 
    super(mf); 

    System.out.println("**** admin create qns panel *"+ q.getQuestionNo()); 
    System.out.println("**** admin create qns panel *"+ q.getQuestionDesc()); 
    System.out.println("**** admin create qns panel *"+ q.getOption1()); 
    System.out.println("**** admin create qns panel *"+ q.getOption2());  

    JLabel lblSet = new JLabel("Set 1"); 
    lblSet.setFont(new Font("Tahoma", Font.BOLD, 30)); 
    lblSet.setBounds(29, 160, 141, 28) ; 
    add(lblSet); 

    JLabel lblQnsDesc = new JLabel("Question Description :"); 
    lblQnsDesc.setFont(new Font("Tahoma", Font.BOLD, 16)); 
    lblQnsDesc.setBounds(76, 270, 181, 20); 
    add(lblQnsDesc); 

    txtfQnsDesc = new JTextField(); 
    txtfQnsDesc.setColumns(10); 
    txtfQnsDesc.setBounds(76, 306, 639, 26); 
    add(txtfQnsDesc); 
    txtfQnsDesc.setText(q.getQuestionDesc()); 

    JLabel lblOp1 = new JLabel("Option 1 :"); 
    lblOp1.setFont(new Font("Tahoma", Font.BOLD, 16)); 
    lblOp1.setBounds(76, 365, 103, 20); 
    add(lblOp1); 

    txtfOp1 = new JTextField(); 
    txtfOp1.setColumns(10); 
    txtfOp1.setBounds(218, 362, 146, 26); 
    add(txtfOp1); 
    txtfOp1.setText(q.getOption1()); 

    JLabel lblOp2 = new JLabel("Option 2 :"); 
    lblOp2.setFont(new Font("Tahoma", Font.BOLD, 16)); 
    lblOp2.setBounds(76, 418, 103, 20); 
    add(lblOp2); 

    txtfOp2 = new JTextField(); 
    txtfOp2.setColumns(10); 
    txtfOp2.setBounds(218, 415, 146, 26); 
    add(txtfOp2); 
    txtfOp2.setText(q.getOption2()); 

    JLabel lblOp3 = new JLabel("Option 3 :"); 
    lblOp3.setFont(new Font("Tahoma", Font.BOLD, 16)); 
    lblOp3.setBounds(76, 468, 103, 20); 
    add(lblOp3); 

    txtfOp3 = new JTextField(); 
    txtfOp3.setColumns(10); 
    txtfOp3.setBounds(218, 465, 146, 26); 
    add(txtfOp3); 
    txtfOp3.setText(q.getOption3()); 

    JLabel lblOp4 = new JLabel("Option 4 :"); 
    lblOp4.setFont(new Font("Tahoma", Font.BOLD, 16)); 
    lblOp4.setBounds(76, 515, 103, 20); 
    add(lblOp4); 

    txtfOp4 = new JTextField(); 
    txtfOp4.setColumns(10); 
    txtfOp4.setBounds(218, 512, 146, 26); 
    add(txtfOp4); 
    txtfOp4.setText(q.getOption4()); 

    JLabel lblCorrAns = new JLabel("Correct Answer :"); 
    lblCorrAns.setFont(new Font("Tahoma", Font.BOLD, 16)); 
    lblCorrAns.setBounds(76, 581, 151, 20); 
    add(lblCorrAns); 

    txtfCorrAns = new JTextField(); 
    txtfCorrAns.setColumns(10); 
    txtfCorrAns.setBounds(218, 578, 146, 26); 
    add(txtfCorrAns); 
    txtfCorrAns.setText(q.getCorrectAnswer()); 

    JButton button = new JButton("Add"); 
    button.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      actionPerformedAdd(); 
      //actionPerformedOk(); 

     } 
    }); 

    button.setFont(new Font("Tahoma", Font.BOLD, 16)); 
    button.setBounds(428, 622, 115, 29); 
    add(button); 

    JButton btnCancel = new JButton("Cancel"); 
    btnCancel.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      JPanel contentPane = new AdminQuizOverallPanel(myFrame, set, topic); 
      myFrame.setContentPane(contentPane); 
      myFrame.setVisible(true); 
     } 
    }); 
    btnCancel.setFont(new Font("Tahoma", Font.BOLD, 16)); 
    btnCancel.setBounds(712, 622, 115, 29); 
    add(btnCancel); 

    JLabel lblQnsNo = new JLabel("Question No. : "); 
    lblQnsNo.setFont(new Font("Tahoma", Font.BOLD, 16)); 
    lblQnsNo.setBounds(76, 234, 151, 20); 
    add(lblQnsNo); 

    txtfQnsNo = new JTextField(); 
    txtfQnsNo.setBounds(218, 228, 146, 26); 
    add(txtfQnsNo); 
    txtfQnsNo.setColumns(10); 
    txtfQnsNo.setText(new Integer(q.getQuestionNo()).toString()); 


    JButton btnUpdate = new JButton("Update"); 
    btnUpdate.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      actionPerformedUpdate(); 


    } }); 
    btnUpdate.setFont(new Font("Tahoma", Font.BOLD, 16)); 
    btnUpdate.setBounds(572, 622, 115, 29); 
    add(btnUpdate); 

} 




//Create 
private void actionPerformedAdd() { 
    // retrieve the user input from the text box/area provided 
    if (validateInput()) { 
     //amtSpend = Double.parseDouble(txtAmount.getText()); 
     // create an object of expenses based on the input values 
     //Debugging? -> System.out.println(topic); 

     //***Refer to QuizDetails Entity Class for its constructor 
     QuizDetails e1 = new QuizDetails(Integer.parseInt(txtfQnsNo.getText()), txtfQnsDesc.getText(), txtfOp1.getText(), 
     txtfOp2.getText(), txtfOp3.getText(), txtfOp4.getText(), txtfCorrAns.getText(), topic, set); 
     // insert to database and check return value 
     if (QuizDetailsDA.createQuizDetails(e1)) { //Call Create method from QuizDetailsDA 
      System.out.print("Ok"); 
      JOptionPane.showMessageDialog(myFrame, 
        "Record created successfully", "Alert", 
        JOptionPane.INFORMATION_MESSAGE); 
      // reset text field for next record. 
      txtfQnsNo.setText(""); 
      txtfQnsDesc.setText(""); 
      txtfOp1.setText(""); 
      txtfOp2.setText(""); 
      txtfOp3.setText(""); 
      txtfOp4.setText(""); 
      txtfCorrAns.setText(""); 
      JPanel contentPane = new AdminQuizOverallPanel(myFrame, set,topic); 
      myFrame.setContentPane(contentPane); 
      myFrame.setVisible(true); 
     } else 
     { 
      System.out.print("Error"); 
      JOptionPane.showMessageDialog(myFrame, 
        "Database Error. Record not created.", "Alert", 
        JOptionPane.ERROR_MESSAGE); 
     } 


    } 
} 

private boolean validateInput() { 
    boolean result = false; 
    String msg = ""; 
    result = true; 
    /*int msgType = JOptionPane.ERROR_MESSAGE; 

    // retrieve the user input from the text box/area provided 
    String dateSpend = txtDate.getText(); 
    String cat = txtCategory.getText(); 
    String amt = txtAmount.getText(); 
    String cont = txtContent.getText(); 

    if (dateSpend.length() != 10) 
     msg += "Please enter date in DD-MM-YYYY format.\n"; 
    if (cat.length() == 0) 
     msg += "Please enter category.\n"; 
    try { 
     Double.parseDouble(amt); // convert to double for amount 
    } catch (NumberFormatException e) { 
     msg += "Plese enter amount in decimal numbers.\n"; 
    } 
    if (cont.length() == 0) 
     msg += "Please enter content.\n"; 

    if (msg.length() == 0) 
     result = true; 
    else 
     JOptionPane.showMessageDialog(myFrame, msg, "Alert", msgType); 
     */ 
    return result; 
} 


//Update 
public AdminCreateQnsPanel(JFrame mf,String action, QuizDetails e1){ 
    this(mf, action); 
    txtfQnsNo.setText(new Integer(e1.getQuestionNo()).toString()); 
    txtfQnsDesc.setText(e1.getQuestionDesc()); 
    txtfOp1.setText(e1.getOption1()); 
    txtfOp2.setText(e1.getOption2()); 
    txtfOp3.setText(e1.getOption3()); 
    txtfOp4.setText(e1.getOption4()); 
    txtfCorrAns.setText(e1.getCorrectAnswer()); 
    quizdetails = e1; 
} 




public AdminCreateQnsPanel(JFrame mf, String action) { 
    super(mf); 
} 



public void actionPerformedUpdate(){ 
    int qnsNo = Integer.parseInt(txtfQnsNo.getText()); 
    String qnsDesc = txtfQnsDesc.getText(); 
    String op1 = txtfOp1.getText(); 
    String op2 = txtfOp2.getText(); 
    String op3 = txtfOp3.getText(); 
    String op4 = txtfOp4.getText(); 
    String corrAns = txtfCorrAns.getText(); 

    //***Refer to QuizDetails Entity Class for its constructor 
    QuizDetails e1 = new QuizDetails(id1,qnsNo, qnsDesc, op1, op2, op3, op4, corrAns); 

    //Testing -> System.out.println("action performed update " + e1.getQuestionNo()); 

    if(QuizDetailsDA.updateQuizDetails(e1)){ //Call Update method from QuizDetailsDA 
     JOptionPane.showMessageDialog(myFrame, "Record updated successfully", "Alert", JOptionPane.INFORMATION_MESSAGE); 
     txtfQnsNo.setEditable(false); 
     txtfQnsDesc.setEditable(false); 
     txtfOp1.setEditable(false); 
     txtfOp2.setEditable(false); 
     txtfOp3.setEditable(false); 
     txtfOp4.setEditable(false); 
     txtfCorrAns.setEditable(false); 


    } 
    else{ 
     JOptionPane.showMessageDialog(myFrame, "Database Error. Record not updated.", "Alert", JOptionPane.ERROR_MESSAGE); 
    } 
} 

更新できるテキストフィールドform'withこのA」。

package studyHelperApp.ui; 


import javax.swing.JLabel; 



import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import model.QuizDetailsTableModel; 
import studyHelperApp.dataAccess.QuizDetailsDA; 
import studyHelperApp.entity.QuizDetails; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import studyHelpersApp.ui.MasterPanel; 
import java.awt.Font; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 
import java.awt.event.ActionEvent; 
import javax.swing.JTextField; 
import javax.swing.table.DefaultTableModel; 

import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 
import javax.swing.ListSelectionModel; 
import java.awt.Color; 




public class AdminQuizOverallPanel extends MasterPanel { 
    private JTextField txtfSearch; 
    private JTable table; 
    private int set; 
    private String topic; 
    public static int id1; 
    private int id; 

    private void setTableModelFromDB(){ 
     ArrayList <QuizDetails> result = QuizDetailsDA.retrieveAllQuizDetails(set,topic); //Call method from QuizDetailsDA 
     QuizDetailsTableModel model = new QuizDetailsTableModel(result); 
     table.setModel(model); 
    } 

    public void loadDataTable(){ 
      setTableModelFromDB(); 

     } 

    /** 
    * Create the panel. 
    * @wbp.parser.constructor 
    */ 

    public AdminQuizOverallPanel(JFrame mf) { 
     super(mf); 
     initComponents(); 

     setBounds(100, 100, 900, 750); 
     setLayout(null); 

     JLabel lblDisplay = new JLabel(""); 
     lblDisplay.setFont(new Font("Tahoma", Font.PLAIN, 17)); 
     lblDisplay.setBounds(550, 200, 287, 20); 
     add(lblDisplay); 

     JLabel lblSet = new JLabel("Set "); 
     lblSet.setBounds(49, 189, 58, 31); 
     lblSet.setFont(new Font("Tahoma", Font.BOLD, 30)); 
     add(lblSet); 


     JScrollPane scrollPane = new JScrollPane(); 
     scrollPane.setBounds(15, 246, 847, 225); 
     add(scrollPane); 

     table = new JTable(); 
     loadDataTable(); 
     scrollPane.setViewportView(table); 


     JButton btnDelete = new JButton("Delete"); 
     btnDelete.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       actionPerformedDelete(); 



     } 
     }); 

     btnDelete.setFont(new Font("Tahoma", Font.BOLD, 16)); 
     btnDelete.setBounds(637, 502, 115, 29); 
     add(btnDelete); 


     JButton btnSearch = new JButton("Search"); 
     btnSearch.setFont(new Font("Tahoma", Font.BOLD, 16)); 
     btnSearch.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0){ 
       lblDisplay.setText("You searched for: "+txtfSearch.getText()); 


      } 


     }); 

     btnSearch.setBounds(744, 160, 93, 29); 
     add(btnSearch); 

     txtfSearch = new JTextField(); 
     txtfSearch.addKeyListener(new KeyAdapter() { 
      @Override 
      public void keyPressed(KeyEvent e) { 
       if(e.getKeyCode()== KeyEvent.VK_ENTER) 
       lblDisplay.setText("You searched for: "+txtfSearch.getText()); 
      } 
     }); 
     txtfSearch.setBounds(550, 161, 194, 26); 
     add(txtfSearch); 
     txtfSearch.setColumns(10); 

     JButton btnUpdate = new JButton("Update"); 
     btnUpdate.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       actionPerformedUpdate(); 
      } 
     }); 
     btnUpdate.setFont(new Font("Tahoma", Font.BOLD, 16)); 
     btnUpdate.setBounds(372, 502, 115, 29); 
     add(btnUpdate); 

     JButton btnAdd = new JButton("Add"); 
     btnAdd.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       JPanel contentPane = new AdminCreateQnsPanel(myFrame, set ,topic); 
       myFrame.setContentPane(contentPane); 
       myFrame.setVisible(true); 

      } 
     }); 
     btnAdd.setFont(new Font("Tahoma", Font.BOLD, 16)); 
     btnAdd.setBounds(135, 502, 115, 29); 
     add(btnAdd); 

     JButton btnBack = new JButton("<Back"); 
     btnBack.setFont(new Font("Tahoma", Font.BOLD, 16)); 
     btnBack.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       JPanel contentPane = new AdminQuizSetNoPanel(myFrame, topic); 
       myFrame.setContentPane(contentPane); 
       myFrame.setVisible(true); 
      } 
     }); 
     btnBack.setBounds(70, 555, 115, 29); 
     add(btnBack); 

     JLabel lblQuizID = new JLabel(topic); 
     lblQuizID.setFont(new Font("Tahoma", Font.BOLD, 30)); 
     lblQuizID.setBounds(156, 189, 147, 31); 
     add(lblQuizID); 

     JLabel lblSetNo = new JLabel(""); 
     lblSetNo.setFont(new Font("Tahoma", Font.BOLD, 30)); 
     lblSetNo.setBounds(107, 189, 31, 31); 
     add(lblSetNo); 


     } 


    public AdminQuizOverallPanel(JFrame mf, int set, String topic) { 
     super(mf); 
     this.set = set; 
     this.topic = topic; 
     initComponents(); 

     setBounds(100, 100, 900, 750); 
     setLayout(null); 

     JLabel lblDisplay = new JLabel(""); 
     lblDisplay.setFont(new Font("Tahoma", Font.PLAIN, 17)); 
     lblDisplay.setBounds(550, 200, 287, 20); 
     add(lblDisplay); 

     JLabel lblSet = new JLabel("Set"); 
     lblSet.setBounds(49, 189, 93, 31); 
     lblSet.setFont(new Font("Tahoma", Font.BOLD, 30)); 
     add(lblSet); 


     JLabel lblSetNo = new JLabel(Integer.toString(set)); 
     lblSetNo.setBounds(107, 189, 31, 31); 
     lblSetNo.setFont(new Font("Tahoma", Font.BOLD, 30)); 
     add(lblSetNo); 


     JScrollPane scrollPane = new JScrollPane(); 
     scrollPane.setBounds(15, 246, 847, 225); 
     add(scrollPane); 

     table = new JTable(); 
     loadDataTable(); 
     scrollPane.setViewportView(table); 


     JButton btnDelete = new JButton("Delete"); 
     btnDelete.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       actionPerformedDelete(); 



     } 
     }); 

     btnDelete.setFont(new Font("Tahoma", Font.BOLD, 16)); 
     btnDelete.setBounds(611, 502, 115, 29); 
     add(btnDelete); 


     JButton btnSearch = new JButton("Search"); 
     btnSearch.setFont(new Font("Tahoma", Font.BOLD, 16)); 
     btnSearch.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0){ 
       lblDisplay.setText("You searched for: "+txtfSearch.getText()); 
       //findQuizDetails(); 

      } 


     }); 

     btnSearch.setBounds(744, 160, 93, 29); 
     add(btnSearch); 

     txtfSearch = new JTextField(); 
     txtfSearch.addKeyListener(new KeyAdapter() { 
      @Override 
      public void keyPressed(KeyEvent e) { 
       if(e.getKeyCode()== KeyEvent.VK_ENTER) 
       lblDisplay.setText("You searched for: "+txtfSearch.getText()); 
      } 
     }); 
     txtfSearch.setBounds(550, 161, 194, 26); 
     add(txtfSearch); 
     txtfSearch.setColumns(10); 

     JButton btnUpdate = new JButton("Update"); 
     btnUpdate.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       actionPerformedUpdate(); 
      } 
     }); 
     btnUpdate.setFont(new Font("Tahoma", Font.BOLD, 16)); 
     btnUpdate.setBounds(365, 502, 115, 29); 
     add(btnUpdate); 

     JButton btnAdd = new JButton("Add"); 
     btnAdd.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       JPanel contentPane = new AdminCreateQnsPanel(myFrame, set ,topic); 
       myFrame.setContentPane(contentPane); 
       myFrame.setVisible(true); 

      } 
     }); 
     btnAdd.setFont(new Font("Tahoma", Font.BOLD, 16)); 
     btnAdd.setBounds(123, 502, 115, 29); 
     add(btnAdd); 


     JButton btnBack = new JButton("<Back"); 
     btnBack.setFont(new Font("Tahoma", Font.BOLD, 16)); 
     btnBack.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
       JPanel contentPane = new AdminQuizSetNoPanel(myFrame, topic); 
       myFrame.setContentPane(contentPane); 
       myFrame.setVisible(true); 
      } 
     }); 
     btnBack.setBounds(70, 555, 115, 29); 
     add(btnBack); 

     JLabel lblQuizID = new JLabel(topic); 
     lblQuizID.setFont(new Font("Tahoma", Font.BOLD, 30)); 
     lblQuizID.setBounds(156, 189, 147, 31); 
     add(lblQuizID); 

     } 





    private void initComponents() { 
     // TODO Auto-generated method stub 

    } 

    //Delete 
    public void actionPerformedDelete(){ 

     System.out.println("performed delete 1****"); 

     int rowSelected = table.getSelectedRow(); 

     System.out.println("performed delete 2 *****"+ rowSelected); 

     if(rowSelected >= 0){ 
      int resp = JOptionPane.showConfirmDialog(myFrame, "Confirm Delete?", "Confirmation", JOptionPane.YES_NO_CANCEL_OPTION); 
      if(resp == JOptionPane.YES_OPTION){ 
       int selRowDel = (Integer)table.getModel().getValueAt(rowSelected, 0); 
       QuizDetailsDA.deleteQuizDetails(selRowDel); //Call Delete method from QuizDetailsDA 
       //QuizDetailsDA.retrieveAllQuizDetails(); 
       setTableModelFromDB(); 
      } 

      else { 
       JOptionPane.showMessageDialog(myFrame, "No record selected", "Alert", JOptionPane.ERROR_MESSAGE); 
      } 

     } 
    } 



    //Update 
    public void actionPerformedUpdate(){ 
     int rowSelected = table.getSelectedRow(); 
     if(rowSelected >= 0){ 
      int id = (Integer)table.getModel().getValueAt(rowSelected, 0); 
      id1 = id; 

      QuizDetails quizdt = QuizDetailsDA.retrieveQuizDetailsById(id); 
      //Testing -> System.out.println("**** action performed update **: " + quizdt.getQuestionNo()); 



      //Call method from AdminCreateQnsPanel(JFrame mf, QuizDetails q, int set, String topic) constructor 
      JPanel contentPane = new AdminCreateQnsPanel(myFrame,quizdt, set, topic); 
      //After adding set and topic to this above constructor, cancel button is working in AdminCreateQnsPanel 
      myFrame.getContentPane().removeAll(); 
      myFrame.setContentPane(contentPane); 
      myFrame.setVisible(true); 



     } 

     else { 
      JOptionPane.showMessageDialog(myFrame, "No record selected", "Alert", JOptionPane.ERROR_MESSAGE); 
     } 

    } 


} 

データアクセスパッケージ

//retrieve quiz by id 
    public static QuizDetails retrieveQuizDetailsById(int id) { 
     // declare local variables 
     QuizDetails quizdetails = null; 
     ResultSet rs = null; 
     DBController db = new DBController(); 
     String dbQuery; 
     PreparedStatement pstmt; 
     // step 1 - connect to database 
     db.getConnection(); 

     // step 2 - declare the SQL statement 
     dbQuery = "SELECT * FROM QuizDetails WHERE id=?"; 
     pstmt = db.getPreparedStatement(dbQuery); 

     // step 3 - execute query 
     try { 
      pstmt.setInt(1,id); 
      rs = pstmt.executeQuery(); 
      if (rs.next()) { // first record found 
       quizdetails = convertToQuizDetails(rs); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 

     } 

     // step 4 - close connection 
     db.terminate(); 
     return quizdetails; 
     } 


    private static QuizDetails convertToQuizDetails(ResultSet rs) throws SQLException { 
     QuizDetails quizdetails; 
     int id = rs.getInt("id"); 
     //String quizID = rs.getString("quizID"); 
     //int setNo = rs.getInt("setNo"); 
     int questionNo = rs.getInt("questionNo"); 
     String questionDesc = rs.getString("questionDesc"); 
     String option1 = rs.getString("option1"); 
     String option2 = rs.getString("option2"); 
     String option3 = rs.getString("option3"); 
     String option4 = rs.getString("option4"); 
     String correctAnswer = rs.getString("correctAnswer"); 



     quizdetails = new QuizDetails(id, questionNo, questionDesc, option1, option2, option3, option4, correctAnswer); 

     return quizdetails; 
    } 


//Update 
     public static boolean updateQuizDetails(QuizDetails quizdt) { 
      //declare local variables 
      boolean success = false; 
      DBController db = new DBController(); 
      String dbQuery; 
      PreparedStatement pstmt; 

      System.out.println("quiz details da ** 1a " + quizdt.getQuestionNo()); 
      System.out.println("quiz details da ** 1b " + quizdt.getQuestionDesc()); 

      //step 1 - establish connection to database 
      db.getConnection();  

      //step 2 - declare the SQL statement 
      //dbQuery = "UPDATE QuizDetails SET quizID = ?, setNo = ?, questionNo = ?, questionDesc = ?, option1 = ?, option2 = ?, option3 = ?, option4 =?, correctAnswer = ? WHERE id = ?"; 

      dbQuery = "UPDATE QuizDetails SET questionNo = ?, questionDesc = ?, option1 = ?, option2 = ?, option3 = ?, option4 =?, correctAnswer = ? WHERE id= ?"; 
      pstmt = db.getPreparedStatement(dbQuery); 

      System.out.println("quiz details da 2 ** " + quizdt.getQuestionNo()); 

      //step 3 - to update record using executeUpdate method 
      try { 
       //pstmt.setInt(1, quizdt.getId()); 
       //pstmt.setString(1, quizdt.getQuizID()); 
       //pstmt.setInt(2, quizdt.getSetNo()); 
       pstmt.setInt(1, quizdt.getQuestionNo()); 
       pstmt.setString(2, quizdt.getQuestionDesc()); 
       pstmt.setString(3, quizdt.getOption1()); 
       pstmt.setString(4, quizdt.getOption2()); 
       pstmt.setString(5, quizdt.getOption3()); 
       pstmt.setString(6, quizdt.getOption4()); 
       pstmt.setString(7,quizdt.getCorrectAnswer()); 
       pstmt.setInt(8, quizdt.getId()); //Cannot be hard coded 

       System.out.println(quizdt.getId()); 
       System.out.println("quiz details da 3 ** " + quizdt.getQuestionNo()); 

       if (pstmt.executeUpdate() == 1) 
        success = true; 
       pstmt.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      //step 4 - close connection 

      db.terminate(); 

      return success;  

     } 

例えば、私は、ID 1を更新したいと思うし、それが正常に更新されます。しかし、id 5を更新したい場合、id 5の更新された項目はid 1に更新されます。データベースを更新しようとすると誰もがこの問題に遭遇しましたか?

+0

上記の問題を解決するための推奨されるコードはありますか? :) –

+0

通常、更新するSQLコードは です。テーブルセットを更新してください。COLUMN =値ここでCONDITION はおそらくwhere条件が正しくありません。 –

+0

WHERE id =?だからid =?私の状態ですか?フレディ・フィッシャー –

答えて

0

このような質問については、おそらく詳しい情報を提供する必要があります。あなたが更新しているコンテキストが何であるかを理解することは難しいです。たとえば、MySQLを使用している場合は、クエリを修正することでこれを解決できます。 Idは、設定された部分ではなく部分で使用されるべきです。このようなものはうまくいくはずです:

UPDATE TABLE `users` SET `name` = 'John' WHERE `id` = 1; 
+0

テーブルを残すと、 ユーザを更新します。set name = 'John' id = 1 –

関連する問題