1

Spring JDBC を使用すると、私はいつも次のようなことをしていることに気づきます。

NamedParameterJdbcTemplat njt = ...;

String SQL = "SELECT blah FROM blah_table WHERE column = :condition";
SqlParameterSource params = new MapSqlParameterSource('condition', variableName);
List<Integer> rows = njt.query(SQL, params, Integer.class);

if(rows.size() == 0)
{
    //record did not exist, whew avoided index out of bounds exception
}

//do something with rows.get(0);

もっと良い方法があるはずですよね?

4

1 に答える 1

0

queryForObjectたとえば、次のように使用できます。

Integer i = null;
try {
    i = template.queryForObject(sql, Integer.class, args);
} catch (EmptyResultDataAccessException e) { // zero rows
}

独自のラッパー メソッドを記述して、EmptyResultDataAccessException を処理し、よりクリーンにすることができます。

于 2013-09-05T20:53:51.733 に答える