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

Time:04-12

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.

CodePudding user response:

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.

CodePudding user response:

You can pass a list of String in a query check this tutorial.

  • Related