2012-06-10 94 views
3

如何在以下jdbctemplate示例中指定'age'參數的值?如何使用spring的jdbcTemplate在SQL查詢中指定參數

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; 

回答

5

您將使用queryForList()方法以參數作爲參數,例如:

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

學會閱讀(一般和文檔)API文檔。這就是你學習的方式。

+3

謝謝,我正在查看文檔,但有點困惑,因爲它在檢索與標準JDBC相比稍微不同的單行或多行時工作方式不同。 – ziggy

相關問題