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

前端 未结 17 1235
执笔经年
执笔经年 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:09

    That's not a good solution because you're relying on exceptions for control flow. In your solution it's normal to get exceptions, it's normal to have them in the log.

    public String test() {
        String sql = "select ID_NMB_SRZ from codb_owner.TR_LTM_SLS_RTN where id_str_rt = '999' and ID_NMB_SRZ = '60230009999999'";
        List certs = jdbc.queryForList(sql, String.class); 
        if (certs.isEmpty()) {
            return null;
        } else {
            return certs.get(0);
        }
    }
    

提交回复
热议问题