简体   繁体   中英

SQL update statement in Java

I am trying to update a field in my table using Netbeans and I have two conditions. The update statement is as follows:

String sql1 = "update tbl_log set Logout_Time =? where Firstname = ? and Check = ?";
        try{
            pst = conn.prepareStatement(sql1);
            pst.setString(1, time);
            pst.setString(2, username);
            pst.setString(3, "IN");
            pst.execute();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }

but I am getting the following error:

com.mysql.jdbc.exceptions.jdbc4.MySQL SyntaxErrorException: 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 'Check = 'IN' at line 1

How can I solve it?

"Check" is a reserved word, so you need to put it in backticks

Change it to:

String sql1 = "update tbl_log set Logout_Time =? where Firstname = ? and `Check` = ?";

For a list of reserved words, see here: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Try using

pst.executeUpdate();

and also

is pst a PreparedStatement ? if not change it to that...

st.executeUpdate("update reservation set busname='"           +
                  jTextField10.getText() + "',busno='"        +
                  jTextField9.getText()  + "',cusname='"      + 
                  jTextField8.getText()  + "',noofpass='"     +
                  jTextField7.getText()  + "',amount='"       +
                  jTextField6.getText()  +"' where cusname='" +
                  jTextField8.getText()  + "' ");

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