简体   繁体   English

JDBC PreparedStatement忽略'? '在$$…$$查询块中

[英]JDBC PreparedStatement ignores ' ? ' in $$ … $$ query block

I have a PreparedStatement in java. 我在Java中有一个PreparedStatement。 I provide it with query containing '?', something like like 我为它提供包含'?'的查询,类似

Select .... where date >= ?

This is ok and when i try to set the argument using: 没关系,当我尝试使用以下方法设置参数时:

preparedStatement.setObject(1, dateToSet);

it works as expected. 它按预期工作。 However if the query contains $$ ... $$ block it seems the question mark (inside the block) can not be located and when i try to set the argument i receive the following error "The column index is out of range: 1, number of columns: 0" 但是,如果查询包含$$ ... $$块,则似乎无法找到问号(在块内),当我尝试设置参数时,我收到以下错误“列索引超出范围:1 ,列数:0“

SELECT * FROM crosstab( $$ Select .... where date >= ? $$ ) AS ct (...);

I also tried to put the ? 我也试着把? just after the $$ block (just to make sure), and it was able to locate that one (outside the $$ block). 就在$$块之后(只是为了确保),并且能够找到该块(在$$块之外)。 Any ideas how to make the argument placeholder discoverable in the $$ block? 有什么想法可以使参数占位符在$$块中被发现吗?

With $$ you have used Dollar-quoted String Constants and ? 使用$$您已使用美元报价的字符串常量? symbol used for declaring JDBC bind parameters won't be resolved inside a string constant. 用于声明JDBC绑定参数的符号不会在字符串常量内解析。 As per PostgreSQL docs: 根据PostgreSQL文档:

While the standard syntax for specifying string constants is usually convenient, it can be difficult to understand when the desired string contains many single quotes or backslashes, since each of those must be doubled. 虽然通常很方便地指定字符串常量的标准语法,但是当所需的字符串包含许多单引号或反斜杠时,可能很难理解,因为每个引号或反斜杠必须加倍。 To allow more readable queries in such situations, PostgreSQL provides another way, called “dollar quoting”, to write string constants. 为了在这种情况下允许更具可读性的查询,PostgreSQL提供了另一种称为“美元引号”的方式来编写字符串常量。 A dollar-quoted string constant consists of a dollar sign ($), an optional “tag” of zero or more characters, another dollar sign, an arbitrary sequence of characters that makes up the string content, a dollar sign, the same tag that began this dollar quote, and a dollar sign. 以美元报价的字符串常量包含一个美元符号($),一个零个或多个字符的可选“标记”,另一个美元符号,构成字符串内容的任意字符序列,一个美元符号以及与之相同的标记开始用这美元报价和一个美元符号。 For example, here are two different ways to specify the string “Dianne's horse” using dollar quoting: 例如,以下是两种使用美元引号指定字符串“ Dianne's horse”的方法:

 $$Dianne's horse$$ $SomeTag$Dianne's horse$SomeTag$ 

Since crosstab() uses a string constant parameter you have to prepare the nested query yourself in Java. 由于crosstab()使用字符串常量参数,因此您必须自己在Java中准备嵌套查询。 This was discussed on the mailing list . 在邮件列表中对此进行了讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM