java.sql.SQLexception:没有为参数1指定值

时间:2017-02-08 09:38:51

标签: java mysql jdbc

我使用java和MySQL开发了员工薪资管理。

我的问题是,当我更新性别时,会发生此错误。

  

java.sql.SQLexception:没有为参数1指定值。

那我该怎么办?

以下是我的更新按钮代码:

 String value4 = txt_id.getText();
    int x = JOptionPane.showConfirmDialog(null, "Are you sure you want to update?","Update Record",JOptionPane.YES_NO_OPTION); 
if(x==0){       
     try{
         String value1 = txt_firstname.getText();
     String value2 = txt_surname.getText();

     String value3 = txt_dob.getText();  
     String value5 = txt_email.getText(); 
     String value6 = txt_tel.getText(); 
     String value7 = txt_address.getText();
     String value8 = txt_dep.getText(); 
     String value9 = txt_add2.getText(); 
     String value10 = txt_apt.getText(); 
     String value11 = txt_pc.getText(); 
     String value12 = txt_design.getText();
     String value13 = txt_status.getText(); 
     String value14 = txt_salary.getText(); 
     String value15 = txt_job.getText(); 
     String value16 = txt_doj.getText(); 
     String sql= "update Staff_information set id='"
             +value4+"',first_name='"
             +value1+"', surname='"
             +value2+"', " + "Dob='"
             +value3+"',Email='"
             +value5+"',Telephone='"
             +value6+"'," + "Address='"
             +value7+"',Department='"
             +value8+"', Address2 = '"
             +value9+"', " + "Apartment = '"
             +value10+"', Post_code ='"
             +value11+"', " + "Designation ='"
             +value12+"', Status ='"
             +value13+"', Salary ='"
             +value14+"', job_title ='"
             +value15+"', Date_Hired ='"
             +value16+"',Gender='"+gender+"', Image=? where id='"+value4+"' ";
     pst=conn.prepareStatement(sql);
     pst.execute(); 
     JOptionPane.showMessageDialog(null, "Record Updated");
     }catch(Exception e)
     { JOptionPane.showMessageDialog(null, e);
     }
     try{

         File file = new File(filename);
         FileInputStream fis = new FileInputStream(file);
         byte[] image = new byte[(int) file.length()]; 
         fis.read(image); 

         String sql = "update Staff_information SET Image =? where id ='"+value4+"'";
         pst = conn.prepareStatement(sql); 
         pst.setBytes(1, image); 
         pst.executeUpdate(); 
         pst.close(); 

     }catch(Exception e){
         JOptionPane.showMessageDialog(null, e);
     }

     finally { 
         try{ rs.close(); 
         pst.close(); 
         } catch(Exception e){
         }
     }

}

1 个答案:

答案 0 :(得分:0)

您需要为第一个sql设置参数。

String sql= "update Staff_information set id='"
             +value4+"',first_name='"
             +value1+"', surname='"
             +value2+"', " + "Dob='"
             +value3+"',Email='"
             +value5+"',Telephone='"
             +value6+"'," + "Address='"
             +value7+"',Department='"
             +value8+"', Address2 = '"
             +value9+"', " + "Apartment = '"
             +value10+"', Post_code ='"
             +value11+"', " + "Designation ='"
             +value12+"', Status ='"
             +value13+"', Salary ='"
             +value14+"', job_title ='"
             +value15+"', Date_Hired ='"
             +value16+"',Gender='"+gender+"', Image=? where id='"+value4+"' ";
     pst=conn.prepareStatement(sql);
pst.setBytes(1, somevaluehere);** 

     pst.execute();