[英]throws an exception in prepared statement in executeUpdate
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();
}
Errormessage: 错误信息:
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)
help me to solve this issue 帮我解决这个问题
不带参数调用executeUpdate
:
preparedStatement.executeUpdate();
Already you have set a query parameter at 您已经在以下位置设置了查询参数
preparedStatement=connection.prepareStatement(query);
^Here
So instead calling this 所以代替这个
preparedStatement.executeUpdate(query);
Remove the query parameter in executeUpdate();
删除
executeUpdate();
的查询参数executeUpdate();
And make it as 并使其成为
preparedStatement.executeUpdate();
do not pass the query string in executeUpdate()
It will solve your issue because when you pass the parameter it calls the Statement's executeUpdate()
function (ie) statement.excuteUpdate(Query);
不要在
executeUpdate()
传递查询字符串,这将解决您的问题,因为当您传递参数时,它将调用语句的executeUpdate()
函数(即) statement.excuteUpdate(Query);
For PreparedStatement---> preparedStatement.executeUpdate();
对于PreparedStatement --->
preparedStatement.executeUpdate();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.