你能告诉我为什么我得到 "Can't use query methods that take a query string on a PreparedStatement." 吗?

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

我在尝试调试以下代码和 SQL Select 查询时一直遇到错误 "Can't use query methods that take a query string on a PreparedStatement."。 (Postgres 9.4,jdk 1.8)也许我是盲人,它是一个简单的类型,但我需要一些帮助。

我的控制台输出:

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)`

部分代码:

            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);   <-------

我已确认字段包含以下值:
foundMbr = false,foundPhoneID = 47,mode = 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;";

我得到的错误是:ResultSet rs = pStmt.executeQuery(qry);

如您在控制台中所见,我什至确认 pStmt 持有正确的绑定,因为我将其打印出来。 - 也就是说,它似乎缺少结尾“;”。不知道为什么会这样,因为我可以在 qry 字符串中看到它。我认为这只是 preparedStatment 的一个怪癖。

我也将这个 SQL 完全复制到 pgAdmin III 中并成功地手动执行了它。虽然,我确实必须加回“;”。我在许多其他领域使用几乎相同的代码没有问题。

难道是漏掉了';'?
也许某种类型不匹配? (foundPhoneID是一个i​​nt,rowid是一个serial/integer,pr_sms是一个整数FKey)
难道是定义qry字符串的if语句块?

TIA!

尝试做:

pStmt.executeQuery()

而不是

pStmt.executeQuery(qry)

如本文所述question