更新记录 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException 时出错:

Error when updating record com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:

我正在尝试创建一个电话簿系统,用户在其中有一个应用程序,他们可以添加、删除、编辑和搜索。我在编辑联系人中不断收到此错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'in '%john%' or lastname in '%john%'' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
at com.mysql.jdbc.Util.getInstance(Util.java:360)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:978)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2526)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1618)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1549)
at PhoneBook.main(PhoneBook.java:112)

这是用于编辑联系人的代码:

}else if (command.equalsIgnoreCase("E")){
                            System.out.println("Type in the name you would like to edit :\n");
                            String data=s.next().trim();


                            stmt = con.createStatement();
                            String sql = "UPDATE contacts" + "WHERE firstname in '%"+data+"%' or lastname in '%"+data+"%'";

                            stmt.executeUpdate(sql);


                            sql = "SELECT firstname, lastname, email, number FROM contacts";
                            ResultSet rs = stmt.executeQuery(sql);
                            PreparedStatement pst=con.prepareStatement(sql);


                            while(rs.next()){
                               //Retrieve column 
                               String firstname = rs.getString("firstname");
                               String lastname = rs.getString("lastname");
                               String email = rs.getString("email");
                               String number = rs.getString("number");

                               //Display
                               System.out.println("First Name: " + firstname + "\n");
                               System.out.println("Last Name: " + lastname + "\n");
                               System.out.println("Email: " + email + "\n");
                               System.out.println("Number: " + number + "\n");
                            }
                            rs.close();

谁能帮帮我?

String sql = "UPDATE contacts" + "WHERE firstname in '%"+data+"%' or lastname in '%"+data+"%'";

更新联系人集 firstname="abc",lastname="bcd" WHERE firstname in '%"+data+"%' or lastname in '%"+data+"%'";

在 where 子句中,更新查询首先找到要编辑的记录,然后设置新值。