I have a typical crosstab query with static parameters. It works fine with createStatement. I want to use preparestatement to query instead.
String
Please try:
String query = "...";
PreparedStatement stat = conn.prepareStatement(query);
ResultSet rs = stat.executeQuery();
while (rs.next()) {
// TODO
}
You have fallen for the confusing type hierarchy of PreparedStatement extends Statement
:
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()
.