0

I am trying to run below code on netbeans but it is throwing this error "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, esalary=?, eage=?, egender=?, edept=? where eid = ?' at line 1". please help me with the error

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        try{
            //open connection
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?useSSL=false","root","BakerStreet@221b");
            
        
               //mysql query to update
               String sql = "update emp set ename=?, esalary=?, eage=?, egender=?, edept=? where eid = ?";
            PreparedStatement ptsmt = con.prepareStatement(sql);
              ptsmt.executeUpdate(sql);
              ptsmt.setString(1,empName.getText());
              ptsmt.setInt(2,Integer.parseInt(empSal.getText()));
              ptsmt.setInt(3,Integer.parseInt(empAge.getText()));
              ptsmt.setString(4,empGen.getText());
               ptsmt.setString(5,empDep.getText());
                ptsmt.setInt(6,Integer.parseInt(id.getText()));
              
          
              
              ptsmt.executeUpdate();
      
       
                JOptionPane.showMessageDialog(this, "Record updated Successfully");
        
               con.close();
        }
            catch(Exception e){
            System.out.println(e.getMessage());
        }
    }     

             
Luuk
  • 12,245
  • 5
  • 22
  • 33
peaky221b
  • 19
  • 2
  • 2
    Remove the 1st of the 2 calls to `ptsmt.executeUpdate(sql);` – forpas Jul 08 '21 at 16:29
  • see: https://stackoverflow.com/questions/39818067/ps-executeupdatesql-throws-com-mysql-jdbc-exceptions-jdbc4-mysqlsyntaxerrorexc – Luuk Jul 08 '21 at 16:41

1 Answers1

0
  1. you have called twice executeUpdate(sql) so remove one
  2. you have called executeUpdate(sql) before adding value to parameters so do this :

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here: try{ //open connection Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?useSSL=false","root","BakerStreet@221b");

           //mysql query to update
           String sql = "update emp set ename=?, esalary=?, eage=?, egender=?, edept=? where eid = ?";
        PreparedStatement ptsmt = con.prepareStatement(sql);
        
          ptsmt.setString(1,empName.getText());
          ptsmt.setInt(2,Integer.parseInt(empSal.getText()));
          ptsmt.setInt(3,Integer.parseInt(empAge.getText()));
          ptsmt.setString(4,empGen.getText());
           ptsmt.setString(5,empDep.getText());
            ptsmt.setInt(6,Integer.parseInt(id.getText()));
          
      
          //execute update ...
            ptsmt.executeUpdate();
  
   
            JOptionPane.showMessageDialog(this, "Record updated Successfully");
    
           con.close();
    }
        catch(Exception e){
        System.out.println(e.getMessage());
    }
}     
bigtheo
  • 624
  • 9
  • 16