Обсуждение: JDBC ilikequery encounter some problems

Поиск
Список
Период
Сортировка

JDBC ilikequery encounter some problems

От
Jian He
Дата:

https://jdbc.postgresql.org/documentation/head/query.html only a few examples. But I encounter some problems  when  I using ilike operator. I asked this question on Stackoverflow (https://stackoverflow.com/questions/68061908/jdbc-ilike-query-java) Now I also type in here> Hope you guys can help me. I do googled, seems don't have much good post about it.
_______

JDBC successfully connected to PostgreSQL. But some ilike query still have problems. only 1 code is working. I want the first and the third one to working properly.

--------------- not working

String ilikequery = "SELECT * FROM emp where ? iLIKE '%C%' ";
PreparedStatement ilikestatement = Main.connection.prepareStatement(ilikequery);
ilikestatement.setString(1,"name");
ResultSet resultSet = ilikestatement.executeQuery();

-------------- this one working,

String queryname = "Cowen"; String query = "select * from emp where name = ?";
PreparedStatement  statement = Main.connection.prepareStatement(query); statement.setString(1,queryname);
ResultSet resultSet = statement.executeQuery();

------------this one not working.

String ilikequerywithparameter = "SELECT * FROM emp" + " where name iLIKE '%"+"?"+"%' ";
PreparedStatement ilikestatementpara = Main.connection.prepareStatement(ilikequerywithparameter); ilikestatementpara.setString(1,"c"); ResultSet resultSet = ilikestatementpara.executeQuery();

The last code snippet have Exception error.Exception in thread "main" org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns:

-------- this one is working.

String simpleilikequery = "SELECT * FROM emp" + " WHERE name iLIKE '%C%'";
PreparedStatement simpleilikestatement = Main.connection.prepareStatement(simpleilikequery);
ResultSet resultSet = simpleilikestatement.executeQuery();

Re: JDBC ilikequery encounter some problems

От
"David G. Johnston"
Дата:
On Sun, Jun 20, 2021 at 10:18 PM Jian He <hejian.mark@gmail.com> wrote:
--------------- not working
String ilikequery = "SELECT * FROM emp where ? iLIKE '%C%' ";
PreparedStatement ilikestatement = Main.connection.prepareStatement(ilikequery);
ilikestatement.setString(1,"name");
ResultSet resultSet = ilikestatement.executeQuery();

Has nothing to do with the iLIKE operator - identifiers cannot be parameterized.  You need to decide on a different way to get the column "name" into the query string safely.

------------this one not working.

String ilikequerywithparameter = "SELECT * FROM emp" + " where name iLIKE '%"+"?"+"%' ";
PreparedStatement ilikestatementpara = Main.connection.prepareStatement(ilikequerywithparameter); ilikestatementpara.setString(1,"c"); ResultSet resultSet = ilikestatementpara.executeQuery();

The last code snippet have Exception error.Exception in thread "main" org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns:

You put the question mark inside single quotes in the submitted query and thus as far as the system is concerned it is just a string containing a question mark, not a parameter symbol.  The direct way to make it work is to add string concatenation operators between the literal parts (which are quoted) and the parameter (which is not), having the server build the final string from the three individual parts.

David J.