Java PreparedStatement complaining about SQL syntax on execute()

前端 未结 6 1825
梦毁少年i
梦毁少年i 2020-12-21 06:20

This is driving me nuts... What am I doing wrong here?

ArrayList toAdd = new ArrayList();
toAdd.add(\"password\");
try{
    Prepa         


        
相关标签:
6条回答
  • 2020-12-21 06:45

    The MySQL manual clearly says that ? (parameter markers) are for binding data values only, not for column names.

    Parameter markers can be used only where data values should appear, not for SQL keywords, identifiers, and so forth.

    So you will have to use your second approach.

    0 讨论(0)
  • 2020-12-21 06:46

    Try using the following

    pStmt.executeUpdate();

    pStmt.close();

    0 讨论(0)
  • 2020-12-21 06:49

    You cannot submit an ALTER TABLE statement using parameters like this.

    I guess it is not permissible to execute DDL statements in Java PreparedStatement.

    0 讨论(0)
  • 2020-12-21 06:59

    When using a prepared statement, your parameter is treated similarily to a string literal. As a result, your statement is equivalent to "ALTER TABLE testTable ADD \'"+s+"\' varchar(100)". Notice the single quotations around the field name in the error message.

    I would suggest building a literal statement in this case, and not attempting to used a prepared statement.

    0 讨论(0)
  • 2020-12-21 07:00

    Prepared statements need to define a fixed structure so they can be precompiled. That means you can have variable values, but never variable table names, column names, function names etc.

    0 讨论(0)
  • 2020-12-21 07:05

    Placeholders in JDBC are for data, not for table, column, view or function names. And for good reason. The DB schema of an application is static most of the time and only changes rarely. There are no benefits making them dynamic.

    0 讨论(0)
提交回复
热议问题