简体   繁体   中英

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

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;

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.

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