Question

I have query like

`Select * from Table1 where xyz in (List of String to be Supplied).

In my java code. I have a dao object in which I am calling this sql using jdbc template. The method takes in a list of String and that needs to be supplied to this SQl. I have my row-mapper.

How to write the SQl and how to pass the list of variables?

My SQL will run on a Teradata Db.

Était-ce utile?

La solution

Use a NamedParameterJdbcTemplate which, as the doc says:

It also allows for expanding a List of values to the appropriate number of placeholders.

So you just need

String sql = "select * from Table1 where xyz in :list"; 
// or String sql = "select * from Table1 where xyz in (:list)";
// I can't remember which one is right
Map parameters = new HashMap<String, Object>();
parameters.put("list", theListOfXyz);
List<Foo> result = template.query(sql, parameters, rowMapper);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top