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

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

数据库连接成功!conn=com.mysql.jdbc.JDBC4Connection@4acdd9ba
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
    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语句中带?参数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值