你能告诉我为什么我得到“不能使用在ReadyStatement上获取查询字符串的查询方法”吗?

2022-09-03 17:28:25

在尝试调试以下代码和 SQL Select 查询时,我不断遇到错误“无法使用在 ReadyStatement 上采用查询字符串的查询方法”。(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 错误在主电话 FKey...电话 FKey: 47

SQLException:不能使用在 ReadyStatement 上采用查询字符串的查询方法。SQLState: 42809 VendorError: 0 org.postgresql.util.PSQLException: 不能使用在 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);   <-------

我已确认这些字段包含以下值:=
false,= 47,= SMS,并且foundMbrfoundPhoneIDmodeqry = "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字符串中看到它。我认为这只是preditionStatment的一个怪癖。

我还将此确切的SQL复制到pgAdmin III中,并手动成功执行它。虽然,我确实不得不添加回“;”。我在许多其他领域几乎使用相同的代码,没有问题。

难道是缺少的“;”?
也许是某种类型不匹配?(foundPhoneID 是 int.,rowid 是 serial/integer,pr_sms 是 integer FKey)
它可能是定义 qry 字符串的 if 语句块吗?

哎呀!


答案 1

尝试做:

pStmt.executeQuery()

而不是

pStmt.executeQuery(qry)

本问题所述


答案 2

推荐