更新记录com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorExceptio



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

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();

有人能帮我吗?

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

字符串sql="UPDATE contacts"+"WHERE firstname in'%"+data+"%'或lastname in[%"+data+"%";

更新联系人设置firstname="abc",lastname="bcdedit"WHERE firstname在"%"+data+"%"或lastname在"%n"+data="%";

相关内容

  • 没有找到相关文章

最新更新