How to specify parameters in an SQL query using spring's jdbcTemplate

28,922

You would use the queryForList() method taking arguments as argument, for example:

List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql, theAge);    

Learn to read API documentation (and documentation in general). That's how you learn.

Share:
28,922
ziggy
Author by

ziggy

Updated on July 09, 2022

Comments

  • ziggy
    ziggy almost 2 years

    How would i specify the value for the 'age' parameter in the following jdbctemplate example?

    String sql = "SELECT * FROM CUSTOMER where age = ? ";
    
        List<Customer> customers = new ArrayList<Customer>();
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    
        List<Map> rows = jdbcTemplate.queryForList(sql);
        for (Map row : rows) {
            Customer customer = new Customer();
            customer.setCustId((Long)(row.get("CUST_ID")));
            customer.setName((String)row.get("NAME"));
            customer.setAge((Integer)row.get("AGE"));
            customers.add(customer);
        }
    
    return customers;
    
  • ziggy
    ziggy almost 12 years
    Thanks, I was looking at the documentation but got a bit confused because it works differently for when retrieving a single row or multiple rows which is slightly different compared to standard JDBC.