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

解决JDBC中PreparedStatement错误:SQL语法错误
本文解决了一位开发者在使用JDBC进行数据库操作时遇到的MySQLSyntaxErrorException问题,通过分析代码发现错误在于使用了不当的PreparedStatement方法。文章详细解释了原因并提供了修正方法。

今天重新搞了一下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语句中带?参数。

引用\[1\]:MySQL中,当出现"check the manual that corresponds to your MySQL server version for the right syntax"的错误提示时,通常是由于SQL语法错误导致的。这个错误提示是MySQL在解析SQL语句时发现了不正确的语法。可能的原因包括使用了不支持的语法、使用了关键字作为字段名、使用了特殊字符等。解决这个问题的方法有几种:1、在冲突的字段名前后加上`符号,以避免与关键字冲突;2、修改冲突的字段名,将其改为与SQL语句关键字不冲突的其他名称;3、检查SQL语句是否有问题,确保语法正确;4、检查表名和字段名是否有问题,包括与SQL关键字冲突或与同一数据库中其他表冲突。\[1\] 问题: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"的错误提示时,意味着在MySQL中发生了语法错误。这个错误提示通常是由于SQL语句中存在不正确的语法导致的。解决这个问题的方法包括在冲突的字段名前后加上`符号,修改冲突的字段名,确保SQL语句的语法正确,以及检查表名和字段名是否有问题,包括与SQL关键字冲突或与同一数据库中其他表冲突。\[1\] #### 引用[.reference_title] - *1* *2* [一篇文章让你解决sql报错check the manual that corresponds to your MySQL server version for the right ...](https://blog.youkuaiyun.com/qq_44050737/article/details/117084682)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [check the manual that corresponds to your MySQL server version for the right syntax to use完美解决](https://blog.youkuaiyun.com/yetaodiao/article/details/126656737)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值