JDBC ilikequery encounter some problems

From: Jian He <hejian(dot)mark(at)gmail(dot)com>
To: pgsql-jdbc(at)lists(dot)postgresql(dot)org
Subject: JDBC ilikequery encounter some problems
Date: 2021-06-21 04:18:02
Message-ID: CAMV54g2ONJv=+8VwL-ZD=NVNzSFzcTBAPcKQzCOXRAQshyyDCA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

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();

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message David G. Johnston 2021-06-21 04:27:50 Re: JDBC ilikequery encounter some problems
Previous Message Sehrope Sarkuni 2021-06-17 20:59:47 [pgjdbc/pgjdbc] 6ee9f4: fix: Correct path to docker entrypoint in postgres...