ORA-00903:PreparedStatement上的表名无效 [英] ORA-00903: invalid table name on PreparedStatement
问题描述
我有一个方法,它将使用一系列QueryParameters为预准备语句执行查询。 HelperConnection
和 QueryParameter
只是小型的java bean,基于应该是不言自明的得到
你在这里看到的。我试着用选择*来自双
使用,而不是 select * from?
其中 QueryParameter
是STRING类型,值为 dual
。但是,我得到一个 java.sql.SQLSyntaxErrorException:ORA-00903:无效的表名
错误。我的代码和输出如下。怎么了?
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 get
s 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("\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
推荐答案
我认为 PreparedStatement
参数仅适用于值 - 不适用于SQL查询的部分内容,例如表格。可能有一些数据库支持您尝试实现的目标,但我不相信Oracle就是其中之一。您需要直接包含表名 - 当然还要谨慎。
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.
这篇关于ORA-00903:PreparedStatement上的表名无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!