Jdbctemplate query for string: EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0

前端 未结 17 1240
执笔经年
执笔经年 2020-11-29 16:22

I am using Jdbctemplate to retrieve a single String value from the db. Here is my method.

    public String test() {
        String cert=null;
        Strin         


        
17条回答
  •  日久生厌
    2020-11-29 17:07

    I just catch this "EmptyResultDataAccessException"

    public Myclass findOne(String id){
        try {
            Myclass m = this.jdbcTemplate.queryForObject(
                    "SELECT * FROM tb_t WHERE id = ?",
                    new Object[]{id},
                    new RowMapper() {
                        public Myclass mapRow(ResultSet rs, int rowNum) throws SQLException {
                            Myclass m = new Myclass();
                            m.setName(rs.getString("name"));
                            return m;
                        }
                    });
            return m;
        } catch (EmptyResultDataAccessException e) { // result.size() == 0;
            return null;
        }
    }
    

    then you can check:

    if(m == null){
        // insert operation.
    }else{
        // update operation.
    }
    

提交回复
热议问题