简体   繁体   English

查询可能不存在的行的正确方法 - Java8

[英]Proper way to query for a row that may not exist - Java8

I'm trying to write a method that will retrieve a row in a database.我正在尝试编写一种方法来检索数据库中的一行。 Around 50% of the time, this row will not exist.大约 50% 的时间,该行将不存在。 I tried using jdbcTemplate.queryForObject, but this resulted in exceptions being thrown if the row didn't exist in the DB.我尝试使用 jdbcTemplate.queryForObject,但是如果数据库中不存在该行,则会引发异常。 I don't think its good practice to try catch the method.我不认为尝试捕获该方法是一种好习惯。

I read that if your data may be null, jdbcTemplate.query was a better choice.我读到如果您的数据可能是 null,jdbcTemplate.query 是一个更好的选择。 But I can only get it working if I return a list.但只有返回一个列表,我才能让它工作。 Returning a list doesn't really make sense here, since I'm only selecting 1 row, that may or may not exist.返回一个列表在这里并没有什么意义,因为我只选择了 1 行,它可能存在也可能不存在。

public List<LoanDetailsBean> getLoanDetailsByInsaddr(String insaddr) {
     String SQL_GET_LOAN_DETAILS_BY_INSADDR =
                "SELECT TOP 1 cip.tranchesize, " +
                        "             cip.maturitydate, " +
                        "             cip.moodysissuerrating, " +
                        "             cip.snpissuerrating, " +
                        "             cip.moodysassetrating, " +
                        "             cip.snpassetrating " +
                        "FROM   cloinstrumentproperty cip " +
                        "WHERE  insaddr = ? " +
                        "ORDER  BY updatedtime DESC ";

     return isdbJdbcTemplate.query(SQL_GET_LOAN_DETAILS_BY_INSADDR, new Object[] { insaddr }, (rs, rowNum) ->
             new LoanDetailsBean(
                  rs.getDouble("tranchesize"),
                  rs.getString("maturitydate"),
                  rs.getString("moodysissuerrating"),
                  rs.getString("snpissuerrating"),
                  rs.getString("moodysassetrating"),
                  rs.getString("snpassetrating")
             )
     );
}

How can I re-write this to make a little more sense?我怎样才能重新编写它以使其更有意义?

Is there an option to use 3rd-party libraries in your project?是否可以选择在您的项目中使用 3rd-party 库?

this could be done musch simpler with my own:这可以用我自己的方法更简单地完成:

<dependency>
   <groupId>com.github.buckelieg</groupId>
   <artifactId>db-fn</artifactId>
   <version>0.3.4</version>
</dependency>

and then:接着:

public List<LoanDetailsBean> getLoanDetailsByInsaddr(String insaddr) {
    try (DB db = new DB("jdbc:postgresql://host:port/database?user=user&password=pass")) {
         return db.select("SELECT TOP 1 cip.tranchesize, cip.maturitydate, cip.moodysissuerrating, cip.snpissuerrating, cip.moodysassetrating, cip.snpassetrating FROM cloinstrumentproperty cip WHERE insaddr=? ORDER BY updatedtime DESC", insaddr)
             .list(rs -> new LoanDetailsBean(rs.getDouble("tranchesize"), rs.getString("maturitydate"), rs.getString("moodysissuerrating"), rs.getString("snpissuerrating"), rs.getString("moodysassetrating"), rs.getString("snpassetrating"))));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM