简体   繁体   中英

Passing Arraylist<String> as parameters for JDBCTemplate with "in" statement in SQL query

I am receiving an ArrayList of string as parameters and I want to use it as parameters in the jdbc. I tried using it with batchUpdate but in that case I need to create the List<Object[]> and I need to hardcode the parameters definition in the SQL query. I want to pass the parameters as ArrayList and to be used in a single place in the "in" statement of the query (Like dynamically picking all the elements in the parameters>.

List<String> x = ex.getInt().getHeader("x", List.class);
String sql = "insert into tuu (userId, userGroupId) " +
                "select userId, ug.userGroupId " +
                "from general2 gt join tu on gt.v_3 = tu.userName " +
                "join tug ug " +
                "on (ug.userGroupId in (?)) " +
                "where n_1 is null";
int[] data = jdbcTemplate.batchUpdate(sql, x);

This code is throwing an error: java.lang.String cannot be cast to [Ljava.lang.Object;

It is compulsory to use ArrayList as parameters. I can't convert it into the string like "1,2,4" and just adding it to the sql query string and make use of jdbctemplate without using any parameters.

I think ug.userGroupId is Integer. and you are comparing with List<String> x which is String. First, cast the List<String> x to List<Integer> x . then run the code. That might work. thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM