Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

postgresql - Java Crosstab - preparedstatement query

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
         }

But it does not seem to work.

I get a PSQLException - Can't use query methods that take a query string on a PreparedStatement.

Any ideas what I am missing?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...