简体   繁体   English

ORA-00903:PreparedStatement上的表名无效

[英]ORA-00903: invalid table name on PreparedStatement

I have a method that will execute a query with a list of QueryParameters for the prepared statement. 我有一个方法将执行一个查询,其中包含准备语句的QueryParameters列表。 The HelperConnection and QueryParameter are just small java beans and should be self-explanatory based on the get s you see here. HelperConnectionQueryParameter只是小的java bean,应该根据你在这里看到的get解释。 I'm try to do a select * from dual using, instead a select * from ? 我尝试select * from dual使用中select * from ? ,而不是select * from ? where the QueryParameter is a STRING type and the value is dual . QueryParameter是STRING类型,值是dual However, I'm getting a java.sql.SQLSyntaxErrorException: ORA-00903: invalid table name error. 但是,我得到一个java.sql.SQLSyntaxErrorException: ORA-00903: invalid table name错误。 My code and output are below. 我的代码和输出如下。 What's up? 这是怎么回事?

public static ResultSet executeQuery(HelperConnection helperConnection, String query, QueryParameter... params) throws SQLException {
  System.out.println("The connection is: " + helperConnection.getJdbcURL());
  System.out.println("The query is: " + query);
  if (params.length > 0) {
    System.out.println("The QueryParameters are:");
    System.out.println("\t" + StringHelper.splitBy(StringHelper.newline + "\t", params));
  }
  Connection conn = helperConnection.createOracleConnection();
  PreparedStatement pstmt = conn.prepareStatement(query);
  for (int i = 1; i <= params.length; i++) {
    QueryParameter param = params[i - 1];
    switch (param.getType()) {
      //Other cases here
      case QueryParameter.STRING:
        pstmt.setString(i, param.getValue());
        break;
    }
  }

  ResultSet rs = pstmt.executeQuery();
  conn.commit();
  return rs;
}

Output: 输出:

The connection is: //.....My connection
The query is: select * from ?
The QueryParameters are:
    String - dual

I believe that PreparedStatement parameters are only for values - not for parts of the SQL query such as tables. 我相信PreparedStatement参数仅适用于 - 不适用于SQL查询的某些部分,例如表。 There may be some databases which support what you're trying to achieve, but I don't believe Oracle is one of them. 可能有一些数据库支持您要实现的目标,但我不相信Oracle就是其中之一。 You'll need to include the table name directly - and cautiously, of course. 您需要直接包含表名 - 当然还要谨慎。

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

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