Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

java - 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. The HelperConnection and QueryParameter are just small java beans and should be self-explanatory based on the gets you see here. I'm try to do a select * from dual using, instead a select * from ? where the QueryParameter is a STRING type and the value is dual. However, I'm getting a java.sql.SQLSyntaxErrorException: ORA-00903: invalid table name error. 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("" + StringHelper.splitBy(StringHelper.newline + "", 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I believe that PreparedStatement parameters are only for values - not for parts of the SQL query such as tables. There may be some databases which support what you're trying to achieve, but I don't believe Oracle is one of them. You'll need to include the table name directly - and cautiously, of course.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...