简体   繁体   English

当查询使用 MySQL 控制台工作时,JDBC 在执行 UPDATE 查询时抛出 SQLSyntaxErrorException

[英]JDBC throws SQLSyntaxErrorException while executing UPDATE query while the query works using MySQL console

I am writing a student management app and have created a function to update student data-我正在编写一个学生管理应用程序并创建了一个更新学生数据的功能-

 public static void updateStudent(int id, int input) throws SQLException {
    Scanner sc = new Scanner(System.in);   //Scanner object
    Connection connection = ConnectionSetup.CreateConnection();  //Setting up connection
    String updateStatement = "UPDATE student_details SET ? = ? WHERE 's_id' = ?;"; //Initializing query
    PreparedStatement pstmt = connection.prepareStatement(updateStatement);

    System.out.println("Enter new name: ");
    String newName = sc.nextLine();
    pstmt.setString(1,"s_name"); //sets first ? to the columnname
    pstmt.setString(2,newName); //sets the second ? to new name
    pstmt.setString(3, String.valueOf(id));  //sets the third ? to the student ID
    pstmt.execute(); //executes the query

All the other CRUD functions work fine, but this one throws the following error after inputting all the info-所有其他 CRUD 功能都可以正常工作,但是在输入所有信息后,此功能会引发以下错误-

Exception in thread "main" 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 ''s_name' = 'Prateek' WHERE 's_id' = '6'' 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.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:371)
at com.Student.manage.StudentFunc.updateStudent(StudentFunc.java:76)
at Start.main(Start.java:58)

I tried printing the finalized query and it has correct syntax and works in the MySQL Console-我尝试打印最终的查询,它具有正确的语法并且可以在 MySQL 控制台中运行 -

SQL Query is: UPDATE student_details SET 's_name' = 'new name' WHERE 's_id' = '6';

What is the bug here?这里的错误是什么? Please help me understand.请帮我理解。

You can't use query parameters for column names (or any other identifer, or SQL keywords, etc.).您不能对列名(或任何其他标识符或 SQL 关键字等)使用查询参数。 When you use a query parameter, it is interpreted as a constant value.当您使用查询参数时,它被解释为一个常量值。 So your UPDATE statement is executed as if you had written it this way:所以你的 UPDATE 语句就像你这样写一样执行:

UPDATE student_details SET 's_name' = 'new name' WHERE 's_id' = '6';

This does not work.这不起作用。 You can't use a string constant value as the left hand side of an assignment * .您不能使用字符串常量值作为赋值*的左侧。 When I test it in my local MySQL client, I get this error:当我在本地 MySQL 客户端中对其进行测试时,出现此错误:

ERROR 1064 (42000): You have an error in your SQL syntax; ERROR 1064 (42000):您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near ''s_name' = 'new name' WHERE 's_id' = '6'' at line 1检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 ''s_name' = 'new name' WHERE 's_id' = '6'' 附近使用正确的语法

The error reports it got confused at 's_name' , because a quoted string literal is not valid at that place in an UPDATE statement.错误报告它在's_name'处被混淆了,因为在 UPDATE 语句中的那个位置引用的字符串文字无效。

The WHERE clause is also a problem. WHERE 子句也是一个问题。 It's not a syntax error, but it doesn't do what you probably intended.这不是语法错误,但它没有按照您的意图执行。

WHERE 's_id' = '6';

This compares the string value 's_id' to the string value '6', it does not compare the column s_id to a value.这将字符串值's_id'与字符串值 '6' 进行比较,它不会将列s_id与值进行比较。 Obviously the string 's_id' is not equal to '6', so the condition will always evaluate as false, and no rows will be updated.显然字符串's_id'不等于 '6',因此条件将始终评估为 false,并且不会更新任何行。


* You can't put a constant value on the left hand of an assignment in most other programming languages, either. *在大多数其他编程语言中,您也不能在赋值的左侧放置常量值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM