问题描述
我有一个典型的带有静态参数的交叉表查询。它适用于createStatement。我想使用preparestatement来查询。
I have a typical crosstab query with static parameters. It works fine with createStatement. I want to use preparestatement to query instead.
String query = "SELECT * FROM crosstab(
'SELECT rowid, a_name, value
FROM test WHERE a_name = ''att2''
OR a_name = ''att3''
ORDER BY 1,2'
) AS ct(row_name text, category_1 text, category_2 text, category_3 text);";
PreparedStatement stat = conn.prepareStatement(query);
ResultSet rs = stat.getResultSet();
stat.executeQuery(query);
rs = stat.getResultSet();
while (rs.next()) {
//TODO
}
但它似乎不起作用。
我得到一个PSQLException -
不能使用带查询的查询方法PreparedStatement上的字符串。
I get a PSQLException -Can't use query methods that take a query string on a PreparedStatement.
我缺少什么想法?
推荐答案
你已经陷入混乱的类型层次结构 PreparedStatement extends Statement
:
You have fallen for the confusing type hierarchy of PreparedStatement extends Statement
:
PreparedStatement
具有相同的执行*(字符串)
方法,例如 Statement
,但它们不是应该使用,只需使用无参数 执行*()
PreparedStatement
的方法 - - 您已经使用 conn.prepareStatement()
给出了实际的查询字符串。
PreparedStatement
has the same execute*(String)
methods like Statement
, but they're not supposed to be used, just use the parameterless execute*()
methods of PreparedStatement
--- you already have given the actual query string to execute using conn.prepareStatement()
.
这篇关于Java Crosstab - preparedstatement查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!