【问题标题】:ORA-00903: invalid table name on PreparedStatementORA-00903: PreparedStatement 上的表名无效
【发布时间】:2012-06-01 06:08:59
【问题描述】:

我有一个方法可以执行查询,其中包含准备好的语句的 QueryParameters 列表。 HelperConnectionQueryParameter 只是小型 java bean,根据您在此处看到的 gets 应该是不言自明的。我尝试使用select * from dual 代替select * from ?,其中QueryParameter 是STRING 类型,值为dual。但是,我收到了 java.sql.SQLSyntaxErrorException: ORA-00903: invalid table name 错误。我的代码和输出如下。怎么了?

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;
}

输出:

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

【问题讨论】:

    标签: java sql oracle jdbc


    【解决方案1】:

    我相信PreparedStatement 参数仅适用于 - 不适用于部分SQL 查询,例如表。可能有一些数据库支持您想要实现的目标,但我不相信 Oracle 是其中之一。您需要直接包含表名 - 当然要谨慎。

    【讨论】:

    • 这很有道理!谢谢:)
    猜你喜欢
    • 2016-12-19
    • 2016-07-31
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 2019-12-01
    相关资源
    最近更新 更多