MySQL:check the manual that corresponds to your MySQL server version for the right syntax
来源:互联网 发布:算法可以申请专利吗 编辑:程序博客网 时间:2024/05/15 12:10
今天重新搞了一下JDBC,然后在写代码的时候遇到了一个蛋疼的问题,纠结了很久,抛出的异常如下:
数据库连接成功!conn=com.mysql.jdbc.JDBC4Connection@4acdd9bacom.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 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2819) at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1811) at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1725) at org.phn.dao.test.DBUtil.main(DBUtil.java:98)**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
就是这一句
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
下面是我的代码
package org.phn.dao.test;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ResourceBundle;/** * @author phn * @date 2015-4-8 * @TODO 数据库操作工具类 */public class DBUtil { static ResourceBundle rbundle = ResourceBundle.getBundle("db"); private static String driverName = rbundle.getString("className"); private static String dbUser = rbundle.getString("user"); private static String dbPass = rbundle.getString("password"); private static String dbUrl = rbundle.getString("url"); /** * @date 2015-4-8 * @TODO 获取数据库连接 * @return Connection */ public static Connection getConnection() { try { // 这里使用这种方法已经指定了new出来的Driver是mysql的驱动 // DriverManager.registerDriver(new Driver()); Class.forName(driverName).newInstance(); Connection conn = null; conn = DriverManager.getConnection(dbUrl, dbUser, dbPass); if (conn != null) { System.out.println("数据库连接成功!conn=" + conn); return conn; } } catch (InstantiationException e) { // e.printStackTrace(); System.out.println("**Error**:caused at " + DBUtil.class + "." + new Throwable().getStackTrace()[0].getMethodName() + "::" + e.getMessage()); } catch (IllegalAccessException e) { // e.printStackTrace(); System.out.println("**Error**:caused at " + DBUtil.class + "." + new Throwable().getStackTrace()[0].getMethodName() + "::" + e.getMessage()); } catch (ClassNotFoundException e) { // e.printStackTrace(); System.out.println("**Error**:caused at " + DBUtil.class + "." + new Throwable().getStackTrace()[0].getMethodName() + "::" + e.getMessage()); } catch (SQLException e) { // e.printStackTrace(); System.out.println("**Error**:caused at " + DBUtil.class + "." + new Throwable().getStackTrace()[0].getMethodName() + "::" + e.getMessage()); } System.out.println("**Error**:数据库连接失败!"); return null; } public static void main(String[] args) { try { Connection conn = getConnection(); String insertSql = "INSERT INTO t_user(uname,upass) VALUES(?,?)"; if (conn != null) { PreparedStatement pstm = conn.prepareStatement(insertSql); pstm.setString(1, "234"); pstm.setString(2, "234"); pstm.executeUpdate(insertSql); } } catch (SQLException e) { e.printStackTrace(); System.out.println("**Error**:" + e.getMessage()); } }}
网上找了很多,然后终于在一位仁兄的博客中遇到了类似的问题,下面是这个问题的解决办法:
仔细看看我的main方法中的这一段:
PreparedStatement pstm = conn.prepareStatement(insertSql);pstm.setString(1, "234");pstm.setString(2, "234");pstm.executeUpdate(insertSql);
PrepareStatement 的方法 executeUpdate 经过了重载,即有一个带String sql参数的和一个没有带String sql参数的,这里就是由于我使用了带Sql语句参数的方法才导致出现这个问题的。
实际上使用executeUpdate()的话sql语句中可以使用?作为参数传递。若使用executeUpdate(string sql)的话则不能使用sql语句中带?参数。
0 0
- MySQL check the manual that corresponds to your MySQL server version for the right syntax错误
- MySQL check the manual that corresponds to your MySQL server version for the right syntax错误
- MySQL:check the manual that corresponds to your MySQL server version for the right syntax
- MySQL check the manual that corresponds to your MySQL server version for the right syntax错误
- MySQL check the manual that corresponds to your MySQL server version for the right syntax错误
- MySQL check the manual that corresponds to your MySQL server version for the right syntax错误
- MySQL check the manual that corresponds to your MySQL server version for the right syntax错误
- MySQL check the manual that corresponds to your MySQL server version for the right syntax
- MySQL check the manual that corresponds to your MySQL server version for the right syntax错误
- MySQL check the manual that corresponds to your MySQL server version for the right syntax错误
- MySQL check the manual that corresponds to your MySQL server version for the right syntax错误
- check the manual that corresponds to your MySQL server version for the right syntax to use
- check the manual that corresponds to your MySQL server version for the right syntax to
- 01-check the manual that corresponds to your MySQL server version for the right syntax
- check the manual that corresponds to your MySQL server version for the right syntax
- check the manual that corresponds to your MySQL server version for the right syntax
- check the manual that corresponds to your MySQL server version for the right syntax的解决方法
- check the manual that corresponds to your MySQL server version for the right syntax
- CentOS 7 U盘 启动盘安装
- 搜索引擎(包括图片搜索)
- Google搜索技巧
- 项目二:对象作为数据成员
- java与matlab混合编程
- MySQL:check the manual that corresponds to your MySQL server version for the right syntax
- 推荐一款适合程序猿使用的字体
- Android如何自定义视图属性总结
- ImageView的属性android:scaleType作用
- UIButton无响应问题详细记录
- iOS开发--毛玻璃效果,图片模糊效果
- 飞机大战开发01之添加主逻辑层,显示背景的移动
- 【OpenGL编程指南】之视图和模型变换
- [XML]sax中DefaultHander解析xml过程和先后顺序