MySQL:check the manual that corresponds to your MySQL server version for the right syntax

冷不防 2022-08-05 14:20 81阅读 0赞

今天重新搞了一下JDBC,然后在写代码的时候遇到了一个蛋疼的问题,纠结了很久,抛出的异常如下:

  1. 数据库连接成功!conn=com.mysql.jdbc.JDBC4Connection@4acdd9ba
  2. 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 '?,?)' at line 1
  3. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  4. at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
  5. at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  6. at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
  7. at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
  8. at com.mysql.jdbc.Util.getInstance(Util.java:386)
  9. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
  10. at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
  11. at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
  12. at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
  13. at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
  14. at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2819)
  15. at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811)
  16. at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725)
  17. at org.phn.dao.test.DBUtil.main(DBUtil.java:98)
  18. **Error**: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

就是这一句

  1. 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 '?,?)' at line 1

下面是我的代码

  1. package org.phn.dao.test;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ResourceBundle;
  8. /**
  9. * @author phn
  10. * @date 2015-4-8
  11. * @TODO 数据库操作工具类
  12. */
  13. public class DBUtil {
  14. static ResourceBundle rbundle = ResourceBundle.getBundle("db");
  15. private static String driverName = rbundle.getString("className");
  16. private static String dbUser = rbundle.getString("user");
  17. private static String dbPass = rbundle.getString("password");
  18. private static String dbUrl = rbundle.getString("url");
  19. /**
  20. * @date 2015-4-8
  21. * @TODO 获取数据库连接
  22. * @return Connection
  23. */
  24. public static Connection getConnection() {
  25. try {
  26. // 这里使用这种方法已经指定了new出来的Driver是mysql的驱动
  27. // DriverManager.registerDriver(new Driver());
  28. Class.forName(driverName).newInstance();
  29. Connection conn = null;
  30. conn = DriverManager.getConnection(dbUrl, dbUser, dbPass);
  31. if (conn != null) {
  32. System.out.println("数据库连接成功!conn=" + conn);
  33. return conn;
  34. }
  35. } catch (InstantiationException e) {
  36. // e.printStackTrace();
  37. System.out.println("**Error**:caused at " + DBUtil.class + "."
  38. + new Throwable().getStackTrace()[0].getMethodName() + "::"
  39. + e.getMessage());
  40. } catch (IllegalAccessException e) {
  41. // e.printStackTrace();
  42. System.out.println("**Error**:caused at " + DBUtil.class + "."
  43. + new Throwable().getStackTrace()[0].getMethodName() + "::"
  44. + e.getMessage());
  45. } catch (ClassNotFoundException e) {
  46. // e.printStackTrace();
  47. System.out.println("**Error**:caused at " + DBUtil.class + "."
  48. + new Throwable().getStackTrace()[0].getMethodName() + "::"
  49. + e.getMessage());
  50. } catch (SQLException e) {
  51. // e.printStackTrace();
  52. System.out.println("**Error**:caused at " + DBUtil.class + "."
  53. + new Throwable().getStackTrace()[0].getMethodName() + "::"
  54. + e.getMessage());
  55. }
  56. System.out.println("**Error**:数据库连接失败!");
  57. return null;
  58. }
  59. public static void main(String[] args) {
  60. try {
  61. Connection conn = getConnection();
  62. String insertSql = "INSERT INTO t_user(uname,upass) VALUES(?,?)";
  63. if (conn != null) {
  64. PreparedStatement pstm = conn.prepareStatement(insertSql);
  65. pstm.setString(1, "234");
  66. pstm.setString(2, "234");
  67. pstm.executeUpdate(insertSql);
  68. }
  69. } catch (SQLException e) {
  70. e.printStackTrace();
  71. System.out.println("**Error**:" + e.getMessage());
  72. }
  73. }
  74. }

网上找了很多,然后终于在一位仁兄的博客中遇到了类似的问题,下面是这个问题的解决办法:

仔细看看我的main方法中的这一段:

  1. PreparedStatement pstm = conn.prepareStatement(insertSql);
  2. pstm.setString(1, "234");
  3. pstm.setString(2, "234");
  4. pstm.executeUpdate(insertSql);

PrepareStatement 的方法 executeUpdate 经过了重载,即有一个带String sql参数的和一个没有带String sql参数的,这里就是由于我使用了带Sql语句参数的方法才导致出现这个问题的。

实际上使用executeUpdate()的话sql语句中可以使用?作为参数传递。若使用executeUpdate(string sql)的话则不能使用sql语句中带?参数。

发表评论

表情:
评论列表 (有 0 条评论,81人围观)

还没有评论,来说两句吧...

相关阅读