这个问题在这里已有答案:
-
MySQLSyntaxErrorException when trying to execute PreparedStatement [duplicate] 2个答案
-
Java MYSQL Prepared Statement Error: Check syntax to use near '?' at line 1 2个答案
我已经尝试了两种方式的queryUpdate,它显示了相同的异常 .
String queryUpdate =“update student set name ='?'其中id ='?' “;
String queryUpdate =“update student set name =?where id =?”;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class UpdateRecord
{
private Connection connection;
Statement statement;
String queryUpdate = "update student set name = ? where id = ? ";
Scanner sc = new Scanner(System.in);
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
public UpdateRecord()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
connection = CreateConnection.getDatabase(); // creating connection from the class we created in the file CreateConnection.java
statement = connection.prepareStatement(query);
System.out.println("Enter id...");
int id = Integer.parseInt(buffer.readLine());
System.out.println("Enter name...");
String name = buffer.readLine();
statement.executeUpdate(query);
System.out.println("\n\n\t\t!!!.........UPDATED..........!!!");
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException s)
{
s.printStackTrace();
}
catch(IOException i)
{
i.printStackTrace();
}
}
public static void main(String[] args)
{
new UpdateRecord();
}
}
MySql异常:
创建的类加载连接输入id ... 1输入名称... Hamid 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 '? where id = ?' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at sun.reflect.NativeConstructorAccessorImpl.newInstance( NativeConstructorAccessorImpl.java:62)位于com.mysql.jdbc.Util.handleNewInstance(Util)的java.lang.reflect.Constructor.newInstance(Constructor.java:423)中的sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) .java:406)com.mysql.jdbc.Util.getInstance(Util.java:381)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1031)at com.mysql.jdbc.SQLError.createSQLException( SQLError.java:957)com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)at com.mysql.jdbc.MysqlIO.sendCommand (MysqlIO.java:1837)com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961)at com.mysql.jdbc.ConnectionImpl.execSQL(Conne ctionImpl.java:2537)位于com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1564),位于UpdateRecord的com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1485) . (UpdateRecord.java:34)在UpdateRecord.main(UpdateRecord.java:56)
1 回答
您似乎想要使用带有位置参数的预准备语句 . 一个不错的选择,但你的语法有一些问题 .
首先,以下行需要更改:
PreparedStatement#executeUpdate()
不带任何参数 . 请务必检查Javadoc是否存在您可能犯的错误 .此外,您从未分配位置参数 . 请尝试以下完整代码段: