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

前端 未结 17 1220
执笔经年
执笔经年 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 16:56

    You can do this way:

    String cert = DataAccessUtils.singleResult(
        jdbcTemplate.queryForList(
            "select ID_NMB_SRZ from codb_owner.TR_LTM_SLS_RTN where ID_STR_RT = :id_str_rt and ID_NMB_SRZ = :id_nmb_srz",
            new MapSqlParameterSource()
                .addValue("id_str_rt", "999")
                .addValue("id_nmb_srz", "60230009999999"),
            String.class
        )
    )
    

    DataAccessUtils.singleResult + queryForList:

    • returns null for empty result
    • returns single result if exactly 1 row found
    • throws exception if more then 1 rows found. Should not happen for primary key / unique index search

    DataAccessUtils.singleResult

提交回复
热议问题