简体   繁体   English

转义双引号Oracle使用Java准备了Statement

[英]Escaping double quotes Oracle prepared Statement in Java

I checked similar asked questions but did not have an acceptable solution. 我检查了类似的问题,但没有可接受的解决方案。 A table with a list of quoted values in a column: 一个表,其中在列中列出了带引号的值:

ROW  COLOR_ARRAY
===  ============
1    "RED", "BLUE", "GREEN", "LIGHT-BLUE"
2    "RED", "GREEN", "LIGHT-BLUE"

I want to pull all rows who contains exact value "BLUE" so, row 1 should only be returned. 我想拉出所有包含精确值“ BLUE”的行,因此,仅应返回第1行。 my prepared statement: 我准备的声明:

        // must look for color wrapped in double quotes "BLUE"
        String sql = "Select * from myTable where COLOR_ARRAY LIKE '%\"?\"%'";
        selectColor = connection.prepareStatement(sql);
        String myColor = "BLUE";
        selectTags.setString(1, myColor);
        rs = selectTags.executeQuery();

I'm getting a Invalid column index error. 我收到无效的列索引错误。 I suspect the ? 我怀疑? mark in my prepared statement is getting lost in trying to esacape the double quotes in my LIKE clause. 我准备好的语句中的标记在尝试保留我的LIKE子句中的双引号时迷路了。

I've tried escaping the double quotes with \\\\" and " but no luck. 我尝试使用\\\\“和\\ u0022来转义双引号,但是没有运气。

In JDBC, you cannot place a placeholder ? 在JDBC中,不能放置占位符? within an existing SQL string; 在现有的SQL字符串中; the value must be standalone. 该值必须是独立的。 JDBC will treat a ? JDBC会对待? within a SQL string as a literal ? 在SQL字符串中作为文字? character; 字符; it's just part of the string. 它只是字符串的一部分。 Escaping the double-quotes characters within the Java string is correct, however. 但是,在Java字符串中转义双引号字符是正确的。

Concatenate the ? 串联? placeholder value with the % SQL wildcard strings in the query string. 查询字符串中带有% SQL通配符字符串的占位符值。

String sql = "Select * from myTable where COLOR_ARRAY LIKE '%\"' || ? || '\"%'";

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

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