【发布时间】:2012-06-01 06:08:59
【问题描述】:
我有一个方法可以执行查询,其中包含准备好的语句的 QueryParameters 列表。 HelperConnection 和 QueryParameter 只是小型 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
【问题讨论】: