Where col like 'abc%' with PreparedStatement

From: Andy Fan <zhihui(dot)fan1213(at)gmail(dot)com>
To: pgsql-jdbc(at)lists(dot)postgresql(dot)org
Subject: Where col like 'abc%' with PreparedStatement
Date: 2021-03-22 15:52:35
Message-ID: CAKU4AWqQ_KCbyJ2aUcdn2qixKuy7NavqzXorW8Soa9C=X3Fh5A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

The real SQL I want to execute is SELECT * FROM t WHERE col like 'abc%';
This query
probably can go with IndexScan since it is prefixed with 'abc'. However I
am confused when I want
to use PreparedStatement.

For example:
List<Map<String, Object>> imap = jdbcTemplate.queryForList("select *
from tm where a like ?",
new Object[] {"a" + "%"}
);

The parse tree is sent to postgreSQL is "a like ?", server knows nothing
about if it is prefixed with
something or not, so probably Index Scan can't be selected. So what the
user can use the prepared
Statement and use the IndexScan for the above case?

Thanks

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Tom Lane 2021-03-22 15:56:06 Re: Where col like 'abc%' with PreparedStatement
Previous Message Feng zhihao 2021-03-22 14:34:14 [pgjdbc/pgjdbc] 66b1e0: fix: make sure the table have defined primary keys...