While working with Postgres JDBC, I have encountered a problem about query with strings. Following query is working with tools like Auqadata Studio but not working in code.
Code is as following;
// strSqlStatement is passed from a framework as String like this and executed untouched
strSqlStatement = "SELECT columnA FROM abc.table1 WHERE columnB= '%KLMN%' limit 1;";
...
strDBUrl = "jdbc:postgresql://x.x.x.x:1234/instance?useUnicode=yes&characterEncoding=UTF-8";
...
rs = st.executeQuery(strSqlStatement);
In Postgres database records exists for columnB as;
... ColumnA ... ColumnB ... ColumnC
------- ------- -------
value1 | TEST_AA_KLMN_BB_S | 25
value2 | TEST2_AA_KLMN_BB_S | 79
value3 | TEST3_AA_KLMN_BB_S | 124
Problem is like this, normally querying value2 with WHERE clause as ColumnB = 'TEST2_AA_KLMN_BB_S' should work but this simple query is not working. Also LIKE clause as ColumnB LIKE '%KLMN%' is not working at all.
There is no problem when other columns as ColumnC is used for WHERE clause.
I have checked that our Postgres database have no locale setting(lc_ctype=C) and encoding of database is UTF-8(server_encoding=UTF8).
Background;
- This code piece exists in a standalone executable jar file.
- Datatype of ColumnB is varchar(64).
- Java 1.7 (Also tried with 1.8 and 1.6).
- JDBC is postgresql-9.4.1211.jre6 (also tried with 7 and 8).
Edit:
@Francisco Puga, I mistakenly write WHERE columnB= '%KLMN%' in question, it is correct in real code as WHERE columnB LIKE '%KLMN%', nevertheless thank you for your quick reply.
I have narrowed the problem. I find out = and LIKE are actually working but there is a limit issue about string.
If compared string is shorter than 25 characters everything is fine but when compared string is longer than 25 characters then result is empty. Actually 25 character rings no bell for me.
ResultSetuseUnicodeandcharacterEncodingare MySQL's JDBC parameters (any maybe other RDBMS). PostreSQL uses thecharSetand that is only relevant for versions before (the pretty ancient)7.3