Java mysql, Simple update with PreparedStatement has syntax error -
this code has sort of simple syntax error. i've fought hours , give up. can spot it? bet it's easy. thanks!
when update firstname john, no problem. when try update commented out line lastname too, syntax error.
import java.sql.*; public class updatetester { public static void main(string[] args) { try { connect connect = new connect(); connection connection = connect.getconnection(); try { string sql = "update student set firstname = ? " + " studentid = 456987"; //string sql = "update student set firstname = ? " // + " set lastname = ?, " // + " studentid = 456987"; preparedstatement pst = connection.preparestatement(sql); pst.setstring(1, "john"); //pst.setstring(2, "johnson"); pst.executeupdate(); system.out.println("updated successfully!"); connection.close(); } catch (sqlexception e) { system.out.println("exception 1!"); e.printstacktrace(); } } catch (exception e) { system.out.println("exception 2!"); e.printstacktrace(); } } }
column names correct. updating lastname on it's own works correctly too. update fails syntax error when trying both, in commented out lines.
3 issues:
- the set keyword can appear once in update statement:
- comma before second parameter missing
- unnecessary comma before clause
the corrected syntax:
string sql = "update student set firstname = ?, " + " lastname = ? " + " studentid = 456987";
Comments
Post a Comment