【问题标题】:Trying to do SELECT statement with parameters using Jdbctemplate spring尝试使用 Jdbctemplate spring 使用参数执行 SELECT 语句
【发布时间】: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);
        }
    }

【问题讨论】:

标签: java spring-boot jdbctemplate


【解决方案1】:

您应该如下编辑源代码。

    @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 || traderCode == ""){
                customers = template.query("SELECT * FROM tblCustomer",
                        new BeanPropertyRowMapper<Customer>(Customer.class));
               
            } else {
                customers = template.query("SELECT * FROM tblCustomer WHERE TraderCode = :TraderCode",
                        new MapSqlParameterSource().addValue("TraderCode", traderCode),
                        new BeanPropertyRowMapper<Customer>(Customer.class));
            }

            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);
        }
    }

【讨论】:

  • 成功了!谢谢!
猜你喜欢
  • 1970-01-01
  • 2021-01-04
  • 2011-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-07
  • 2012-08-27
相关资源
最近更新 更多