Hello! The setString () method of preparedStatement does not work. This is not how it works:

prStmt = conn.prepareStatement("SELECT * FROM ?"); prStmt.setString(1, "cases"); rs = prStmt.executeQuery(); 

And this is how it works:

 prStmt = conn.prepareStatement("SELECT * FROM cases"); rs = prStmt.executeQuery(); 

Although the documentation says:

void setString (int parameterIndex, String x), where x is set instead of the "?"

Tell me what could be the problem?

  • 2
    And for the name of the table, it seems impossible to do so, only for the fields - Alexey Shimansky

1 answer 1

For the name of the table can not use parameterization?

answer from @Aleksey-Shimansky

instead, you can use a variable when creating a query

 String tableName = "cases"; String query = "SELECT * FROM " + tableName; prStmt = conn.prepareStatement(query); rs = prStmt.execute(); 

request alternate

 String query = String.format("SELECT * FROM $1%s WHERE .... ? ", tableName); 

also correct the command

from the docs.oracle.com documentation - Using Prepared Statements

With Statement objects , to run a PreparedStatement object , you can:

  • run executeQuery if the result is ONE ResultSet value
  • run executeUpdate if the query DOES NOT HAVE the result of the values
  • run execute if the result has MUCH values
  • 2
    Want to say what you can apply ? to the name of the table? - Alexey Shimansky
  • @ Alexey Shimansky, of course not. Table name can not be used as? You are right here. - Saidolim
  • But it seems to me that the question is precisely this ... yes there is no execute ... But the error is not in this with the author and he asks about it .. I think it is worth explaining this in the answer. - Alexey Shimansky
  • one
    Now everything is clear, thank you for your help - Igor Gorbunov