简体   繁体   中英

Update Statement with where clause in java:Netbeans

This was written to update the table with the new data but on executing the following code it showing a null pointer exception at prepare statement . Can anyone just help in finding where i went wrong!

String a= jTextField1.getText();
String b= jTextField2.getText();
String c= jTextField3.getText();
String d= jTextField4.getText();
String e= jTextField5.getText();
String f= jTextField6.getText();
String g= jTextField7.getText();
String i= jTextField8.getText();

try {
String s2="update loginsys set 
appointtype='"+a+"',appointname='"+b+"',appointh='"+c+"',
appointm='"+d+"',appointd='"+e+"',appointmo='"+f+"',appointy='"+g+"'where 
username='"+f+"';";

    PST=conn.prepareStatement(s2);
    PST.setString(1,a);
    PST.setString(2,b);
    PST.setString(3,c);
    PST.setString(4,d);
    PST.setString(5,e);
    PST.setString(6,f); 
    PST.setString(7,g);
    PST.setString(8,f);

RS=PST.executeQuery();
if(RS.next()){         
    JOptionPane.showMessageDialog(null, "Appointment was fixed");
    appointments.this.setVisible(false);
    new list().setVisible(true);}
   else
      JOptionPane.showMessageDialog(null, "Appointment was not fixed");

First, go back and read the tutorial on Using Prepared Statements

Second, update your query string to be more like update loginsys set appointtype=?,appointname=?,appointh=?,appointm=?,appointd=?,appointmo=?,appointy=? where username=? update loginsys set appointtype=?,appointname=?,appointh=?,appointm=?,appointd=?,appointmo=?,appointy=? where username=?

Third use executeUpdate instead of executeQuery - it will return the number of rows which were updated by the query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM