【问题标题】:Error SQL: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax [duplicate]错误 SQL:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法 [重复]
【发布时间】:2021-12-19 05:04:03
【问题描述】:

我一直在尝试使用 preparedStatement 与 MySQL 建立 JDBC 连接。

到目前为止,这是我的代码:

package uni.jdbc.preparedStatement;
import java.sql.*;

public class Main {

    public static void main(String[] args) {

        try {

            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/banco_Teste","root","password");

            //ADD
            String resultSet1 = "INSERT INTO Clientes(nome) VALUES(?)";
            String resultSet2 = "INSERT INTO Clientes(nome) VALUES(?)";

            //UPDATE
            String resultSet3 = "UPDATE Clientes SET nome = ? WHERE id = ?";
            String resultSet4 = "UPDATE Clientes SET nome = ? WHERE id = ?";

            //DELETE
            String resultSet5 = "DELETE FROM Clientes WHERE id = ?";
            String resultSet6 = "DELETE FROM Clientes WHERE id = ?";

            //SELECT
            String select = "Select * FROM Clientes";

            try (PreparedStatement pS = conn.prepareStatement(resultSet1 + resultSet2 + resultSet3 + resultSet4 + resultSet5 + resultSet6 + select)){

                //INSERT
                pS.setString(1, "João");
                pS.setString(2, "Maria");
                pS.execute(resultSet1 + resultSet2);

                //UPDATE
                pS.setString(1, "John");
                pS.setString(2, "Marya");
                pS.execute(resultSet3 + resultSet4);

                //DELETE
                pS.setString(1, "John");
                pS.setString(2, "Marya");
                pS.execute(resultSet5 + resultSet6 );

                //SELECT
                pS.execute(select);

            }catch (SQLException e){

                e.printStackTrace();

            }

        }catch(SQLException | ClassNotFoundException e){

            e.printStackTrace();

            System.out.println("Conexão falhou!");

        }
    }
}

这是查询:

create database banco_Teste

use banco_Teste

CREATE TABLE Clientes(

    id_cliente INTEGER PRIMARY KEY AUTO_INCREMENT,
    nome varchar (100)

)

问题是:当我运行时,返回以下错误:

java.sql.SQLSyntaxErrorException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '?)INSERT INTO Clientes(nome) VALUES(?)' 附近使用正确的语法 在 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) 在 com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) 在 com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:768) 在 com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:653) 在 uni.jdbc.preparedStatement.Main.main(Main.java:46)

会不会是版本问题?连接器版本为 8.0.27。

【问题讨论】:

  • 您需要使用pS.execute()。此外,您需要确保您实际上正在执行/准备一个有效的语句,因为您当前的语句无效。

标签: java mysql jdbc prepared-statement


【解决方案1】:
resultSet1 + resultSet2 + resultSet3 + resultSet4 + resultSet5 + resultSet6 + select

会给你一个错误,因为最后,你的查询结果看起来像这样:

INSERT INTO Clientes(nome) VALUES(?)INSERT INTO Clientes(nome) VALUES(?)UPDATE Clientes SET nome = ? WHERE id = ?UPDATE Clientes SET nome = ? WHERE id = ?...

什么是 syntaxically 不正确,如您的例外中所述。您需要做的是在查询末尾添加;

您的查询应如下所示:

    //ADD
    String resultSet1 = "INSERT INTO Clientes(nome) VALUES(?);";
    String resultSet2 = "INSERT INTO Clientes(nome) VALUES(?);";

    //UPDATE
    String resultSet3 = "UPDATE Clientes SET nome = ? WHERE id = ?;";
    String resultSet4 = "UPDATE Clientes SET nome = ? WHERE id = ?;";

    //DELETE
    String resultSet5 = "DELETE FROM Clientes WHERE id = ?;";
    String resultSet6 = "DELETE FROM Clientes WHERE id = ?;";

    //SELECT
    String select = "Select * FROM Clientes;";

这个link可以帮助你了解更多详情。

我对这个电话有点困惑:

pS.execute(resultSet1 + resultSet2);

根据Java Doc,没有这种方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 2015-10-23
    相关资源
    最近更新 更多