MySQLdb mySQLdb =new MySQLdb();
query="INSERT INTO user(username,password,firstname,email) VALUES (?,?,?,?)";
System.out.println(user.getUsername()+" "+user.getPassword()+" "+user.getName()+" "+user.getEmail());
connection= mySQLdb.getConnection();
try {
preparedStatement=connection.prepareStatement(query);
preparedStatement.setString(1, user.getUsername());
preparedStatement.setString(2, user.getPassword());
preparedStatement.setString(3, user.getName());
preparedStatement.setString(4, user.getEmail());
preparedStatement.executeUpdate(query);//--->throws exception here
//System.out.println("101");
return true;
} catch (Exception e) {
System.out.println(e.getMessage());
//System.out.println("102");
e.printStackTrace();
}
错误信息:
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 '?,?,?,?)' at line 1
java.sql.SQLException: 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 '?,?,?,?)' at line 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
帮我解决这个问题
最佳答案
不要在executeUpdate()
中传递查询字符串
它将解决您的问题
因为当您传递参数时,它将调用语句的executeUpdate()
函数
(即)statement.excuteUpdate(Query);
对于PreparedStatement ---> preparedStatement.executeUpdate();