错误 SQL:您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册以了解正确的语法
Error SQL: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
我一直在尝试使用 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: 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 '?)INSERT INTO Clientes(nome) VALUES(?)' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:768)
at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:653)
at uni.jdbc.preparedStatement.Main.main(Main.java:46)
会不会是版本问题?连接器版本为 8.0.27.
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,没有这样的方法。
我一直在尝试使用 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: 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 '?)INSERT INTO Clientes(nome) VALUES(?)' at line 1 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:768) at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:653) at uni.jdbc.preparedStatement.Main.main(Main.java:46)
会不会是版本问题?连接器版本为 8.0.27.
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,没有这样的方法。