| From: | "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com> |
|---|---|
| To: | Jian He <hejian(dot)mark(at)gmail(dot)com> |
| Cc: | pgsql-jdbc(at)lists(dot)postgresql(dot)org |
| Subject: | Re: JDBC ilikequery encounter some problems |
| Date: | 2021-06-21 04:27:50 |
| Message-ID: | CAKFQuwZejrjD5+--oHVNrFmUAse_S3iYmQidmj8fEpkgrThzsA@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-jdbc |
On Sun, Jun 20, 2021 at 10:18 PM Jian He <hejian(dot)mark(at)gmail(dot)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.
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Dave Cramer | 2021-06-22 12:47:03 | [pgjdbc/pgjdbc] |
| Previous Message | Jian He | 2021-06-21 04:18:02 | JDBC ilikequery encounter some problems |