【发布时间】:2021-12-30 10:54:35
【问题描述】:
这是我在使用 traderCode 参数发出请求时遇到的错误:
org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [SELECT * FROM tblCustomer WHERE TraderCode = :traderCode;]; The index 1 is out of range.; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: The index 1 is out of range.
Postman 中提供了参数: Screenshot
完整的获取请求:
@GetMapping
public ResponseEntity<List<Customer>> getCustomers(@RequestParam(required = false) String traderCode, String compCode) {
try {
List<Customer> customers = new ArrayList<Customer>();
JdbcTemplate template = getDataSource(compCode);
if (traderCode == null){
customers = template.query("SELECT * FROM tblCustomer;",
new BeanPropertyRowMapper<Customer>(Customer.class));
//customerRepository.findAll().forEach(customers::add);
} else {
customers = template.query("SELECT * FROM tblCustomer WHERE TraderCode = :traderCode;",
new BeanPropertyRowMapper<Customer>(Customer.class),
new MapSqlParameterSource()
.addValue("traderCode", traderCode));
//customerRepository.findByTraderCode(traderCode).forEach(customers::add);
}
if (customers.isEmpty())
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
return new ResponseEntity<>(customers, HttpStatus.OK);
} catch (Exception e) {
System.out.print(e);
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
【问题讨论】:
-
从这里的第一条评论开始:stackoverflow.com/questions/55449496/…。 “JdbcTemplate 不支持命名参数。确保使用 NamedParameterJdbcTemplate。”
标签: java spring-boot jdbctemplate