Skip to content
Advertisement

How to query for a List in JdbcTemplate?

I’m using Spring’s JdbcTemplate and running a query like this:

JavaScript

There are no named parameters being passed, however, column name, COLNAME, will be passed by the user.

Questions

  1. Is there a way to have placeholders, like ? for column names? For example SELECT ? FROM TABLEA GROUP BY ?

  2. If I want to simply run the above query and get a List<String> what is the best way?

Currently I’m doing:

JavaScript

Advertisement

Answer

Is there a way to have placeholders, like ? for column names? For example SELECT ? FROM TABLEA GROUP BY ?

Use dynamic query as below:

JavaScript

If I want to simply run the above query and get a List what is the best way?

JavaScript

EDIT: To Stop SQL Injection, check for non word characters in the colName as :

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement