1. 程式人生 > >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

今天重新搞了一下JDBC,然後在寫程式碼的時候遇到了一個蛋疼的問題,糾結了很久,丟擲的異常如下:

資料庫連線成功![email protected]
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語句中帶?引數。

相關推薦

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

今天重新搞了一下JDBC,然後在寫程式碼的時候遇到了一個蛋疼的問題,糾結了很久,丟擲的異常如下: 資料庫連線成功![email protected] com.mysql.jdbc.e

關於check the manual that corresponds to your MySQL server version for the right syntax to use near

mysql今天在更新表的時候一直提示check the manual that corresponds to your MySQL server version for the right syntax to use near問題排查很久,數據庫版本沒有問題,語法也沒有問題,卻一直報錯最後排查發現是關鍵詞沖突

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

bat sqli jdbc call wrap base defaults org dsta 今配置Mybatis框架測試的時候報了這個錯:以下是錯誤的信息:org.apache.ibatis.exceptions.PersistenceException: ### Err

linux mysql----You have an error in your SQL syntax; check the manual that corresponds to your MySQL

Mysql 語句毛病 (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use

解決ROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use n

之前一直用的好好的,突然就出現了這個錯誤: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the rig

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server

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 MyBatis中踩到

1064 - 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 'grou

mysql8.0版本  在已存在的表裡插入一條資料 insert INTO api_user(id,username,email,groups)VALUES('1','hh','[email protected]','Boss'); 執行報錯:1064 - You have an e

check the manual that corresponds to your MySQL server version for the right syntax to use near ###

報錯如下: pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

myBatis查詢報錯 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

myBatis查詢報錯    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

mysql建立資料庫報錯You have an error in your SQL syntax; check the manual that corresponds to your MySQL se

在使用xshell遠端連線我的伺服器時想建立一個數據庫 結果執行不對,sql語句如下 mysql> create database 'wechatmall'; 結果出現mysql報錯,這個提示很明顯是sql語句有問題 You have an e

:“You have an error in your SQL syntax; check the manual that corresponds to your MySQL server versi

今天用C#連線資料庫的時候,想把欄位名改成中文,結果就報這個錯誤了,:“You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the

check the manual that corresponds to your MySQL server version for the right syntax to use near 'TYP

建立QUARTZ表時出現如下的問題 CREATE TABLE QRTZ_JOB_DETAILS( JOB_NAME VARCHAR(80) NOT NULL, JOB_GROUP VARCHAR(80) NOT NULL, DESCRIPTION VAR

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server versio

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to you

CHECK the manual that corresponds TO your MySQL SERVER VERSION FOR the RIGHT syntax TO USE near.....

使用spingdata+jpa+hibernate來對資料庫進行插入操作的時候,出現了一個報錯資訊,說sql語法有誤。報錯資訊如下: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Yo

ssh開發中插入資料時遇到 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right

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 'use

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

昨天晚上,用Hibernate做多對多對映,然後測試,結果直接報check the manual that corresponds to your MySQL server version for the right syntax to use near 'Order (m

check the manual that corresponds to your MySQL server version for the right syntax to use near '..'

[School InFormatization -->]-->ERROR{SchemaUpdate.java:212}-Unsuccessful: create table group (

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的提示。明明寫的格式也是對的但就是總報這個提示,百思不得其解

Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your My

轉自:https://blog.csdn.net/haha_66666/article/details/78444457 Query : select * from order LIMIT 0, 1000 Error Code : 1064 You have an error in your

凜冬之翼---You have an error in your SQL syntax; check the manual that corresponds to your

今天在寫網頁與資料庫互動的程式碼的時候遇到了一個問題You have an error in your SQL syntax; check the manual that corresponds to your 問題分析:從英文翻譯過來的字面上意思是你的資料庫MySQL出現問題,也就是M