Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you tell me why I get "Can't use query methods that take a query string on a PreparedStatement."? [duplicate]

I keep hitting an error "Can't use query methods that take a query string on a PreparedStatement." when trying to debug the following code & SQL Select query. (Postgres 9.4, jdk 1.8) Maybe I'm blind and it's a simple type, but I could use some help.

My Console Ouput:

SELECT rowid, firstname, lastname, prefname, email1, email2, email3, type, status, preflang, mbrappid, deviceid, mbrstatus, mbrtype, mbrcat, pr_phonevoice FROM qbirt.person WHERE pr_sms = 47 ORDER BY lastupdt DESC

E R R O R JDBC Prep'd Stmt error on Primary Phone FKey... Phone FKey: 47

SQLException: Can't use query methods that take a query string on a PreparedStatement. SQLState: 42809 VendorError: 0 org.postgresql.util.PSQLException: Can't use query methods that take a query string on a PreparedStatement. at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:102) at solutions.demand.qbirt.Person.findMember(Person.java:762)`

Portion of code:

            if (!foundMbr && foundPhoneID > 0) {
                if (QbirtUtils.verbose) {
                    System.out.println("Querying Person by FK ID for phones: " + foundPhoneID + "\n");
                }

                if (mode.equals(pMode.SMS)) {
                    qry = "SELECT rowid, firstname, lastname, prefname, email1, email2, email3, type, "
                            + "status, preflang, mbrappid, deviceid, mbrstatus, mbrtype, mbrcat, pr_phonevoice "
                            + "FROM qbirt.person "
                            + "WHERE pr_sms = ? "
                            + "ORDER BY lastupdt DESC;";
                } else {
                    if (mode.equals(pMode.VOICE)) {
                        qry = "SELECT rowid, firstname, lastname, prefname, email1, email2, email3, type, "
                                + "status, preflang, mbrappid, deviceid, mbrstatus, mbrtype, mbrcat, pr_phonevoice "
                                + "FROM qbirt.person "
                                + "WHERE pr_phonevoice = ? "
                                + "ORDER BY lastupdt DESC;";
                    } else { 
                        if (mode.equals(pMode.PHONE)) {
                            qry = "SELECT DISTINCT ON (rowid) rowid, firstname, lastname, prefname, email1, email2, email3, type, "
                                    + "status, preflang, mbrappid, deviceid, mbrstatus, mbrtype, mbrcat, pr_phonevoice "
                                    + "FROM qbirt.person "
                                    + "WHERE (pr_sms = ? OR pr_phonevoice = ?) "
                                    + "ORDER BY lastupdt DESC, rowid DESC;";
                        }
                    }
                }



                try {
                    PreparedStatement pStmt = conn.prepareStatement(qry);
                    pStmt.setInt(1, foundPhoneID);
                    if (mode.equals(pMode.PHONE)) {
                        pStmt.setInt(2, foundPhoneID);
                    }
                    System.out.println(pStmt.toString());
                    ResultSet rs = pStmt.executeQuery(qry);   <-------

I have confirmed that the fields contain the following values:
foundMbr = false, foundPhoneID = 47, mode = SMS, and that qry = "SELECT rowid, firstname, lastname, prefname, email1, email2, email3, type, status, preflang, mbrappid, deviceid, mbrstatus, mbrtype, mbrcat, pr_phonevoice FROM qbirt.person WHERE pr_sms = ? ORDER BY lastupdt DESC;";

I get the error on the line: ResultSet rs = pStmt.executeQuery(qry);

As you can see in the console, I have even confirmed that the pStmt is holding the correct binding because I print it out. - That said, it seems to be missing the ending ';'. Not sure why that is because I can see it in the qry string. I assume that is just a quirk of the preparedStatment.

I have also copied this exact SQL into pgAdmin III and successfully executed it manually. Although, I did have to add back the ';'. I use virtually this same code in many other areas without problem.

Could it be that the missing ';'?
Maybe some sort of type mismatch? (foundPhoneID is an int., rowid is a serial/integer, pr_sms is an integer FKey)
Could it be the block of if statements that defines the qry string?

TIA!

like image 838
MaybeWeAreAllRobots Avatar asked Mar 14 '16 04:03

MaybeWeAreAllRobots


1 Answers

Try doing:

pStmt.executeQuery()

instead of

pStmt.executeQuery(qry)

as discussed in this question

like image 125
Monica Granbois Avatar answered Oct 25 '22 04:10

Monica Granbois