I want to know how to bind list of objects into sql with more than 1 parameter in 'where' clause.
Select * from order where order_id = ? and action_code = ?
Data is like
List<Order> orderList = new ArrayList<>();
orderList.add(1,"KP");
orderList.add(2,"DP");
orderList.add(3,"KP");
Here is what i tried I have a list of Orders. Each order has order id (long) and action code (string). Now i need to query the db with order id and action code as the parameters in where clause.
Select * from order where order_id = ? and action_code = ?.
I tried to pass an array of objects in args. [ this dint work ].
Object[] args = new Object[ size of orderList ]
int i = 0
for (Order order : orderList)
{
args[i] = new Object[]{order.getOrderId(), order.getActionCode()};
i++;
}
I am firing the query as below
jdbcTemplate.query(sql, args, rowMapper);
When i run the app, i get below error.
org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [SELECT * where ORD_I = ? AND ACT_C = ?]; SQL state [99999]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type
However if i pass data as below it works
jdbcTemplate.query(sql, new Object[] {orderList.get(0).getOrderId, orderList.get(0).getActionCode()}, rowMapper);
What i have tried so far I have provided the Type as below [ in DB, order_id is number(38), action_code is Char(2))
int[] types = new int[] { java.sql.Types.NUMERIC, java.sql.Types.CHAR
};
jdbcTemplate.query(sql, args, types, rowMapper);
i got an exception saying . [Same error when i replace java.sql.Types.CHAR with java.sql.Types.VARCHAR ]
org.springframework.dao.InvalidDataAccessApiUsageException: args and argTypes parameters must match
The below set up works
int[] types = new int[] { java.sql.Types.NUMERIC, java.sql.Types.CHAR
};
jdbcTemplate.query(sql, new Object[] {orderList.get(0).getOrderId, orderList.get(0).getActionCode()}, types, rowMapper);
i have referred below issues How set Array/List in Spring jdbcTemplate as parameter? - talks about passing one parameter from the list
how to bind a list of tuples using Spring JDBCTemplate? - talks about tuples. If i have to query 2 tables with tableA.id = tableB.id and tableA.id = ? and tableB.seq_id = ?, then the above solution doesnt work.
I googled a lot but now i have hit a road block. Kindly help.