您的位置:首页 > 数据库 > MySQL

MySql JDBC:You have an error in your SQL syntax;

2012-07-01 22:14 211 查看
用Statement来创建sql语句写多了,然后由于安全,性能等等的因素改用PreparedStatement来创建sql语句,但是呢,原来拷贝过来的

st.executeQuery(sql);还是用的原来的,这样就造成了,改了前面,忘记了后面的部分没有全部矫正过
就是用:

PreparedStatement ps = null;

ps = conn.prepareStatement(sql);

/* 中间省略一万字 */

ps.executeUpdate(sql);

然后在跑这条sql语句的时候,就会报

Exception in thread "main" com.infy.jdbc.dao.DaoException: 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 '?,birthday = ?, money = ? where id
= ?' at line 1
at com.infy.jdbc.dao.impl.UserDaoImpl.updateUser(UserDaoImpl.java:118)
at com.infy.jdbc.dao.UserDAOTest.main(UserDAOTest.java:24)
Caused by: com.mysql.jdbc.exceptions.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 '?,birthday = ?, money = ? where
id = ?' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1402)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1317)
at com.infy.jdbc.dao.impl.UserDaoImpl.updateUser(UserDaoImpl.java:116)
... 1 more

因为ps.executeUpdate(sql);是跑不通的,我们可以去查一下jdk的api文档,里面就根本没有executeUpdate(sql);方法,有的是executeUpdate(),所以解决的办法就是去掉参数sql就可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐