Reputation: 412
I'm selecting from a database and I want to make sure if I need to check if the RowSet
is empty. Do I need to make a simple if
statement or can i leave it?
So is this enough?
//RowSet rs = (...)
try {
rs.next()
//process data
} catch (SQLException e) {
}
Or do i have to put an if statement in it like this:
//RowSet rs = (...)
try {
if (rs.next()) {
//process data
}
} catch (SQLException e) {
}
Does the rs.next()
throws SQLException
even if the RowSet
is empty or just when it's closed? Another way to ask the question: Is the RowSet closed when it is empty?
Upvotes: 0
Views: 165
Reputation: 1254
Although this doesn't answer your question... I'd ask why you aren't using the built in transformation/mapping functions of JdbcTemplate
. Spring abstracts away most of the ResultSet open, iteration, close operations for you so that you can just focus on mapping the data to objects. You could use an implementation of RowMapper
which gives you a mapRow(ResultSet rs, int rowNum)
method and you can map each row to an object. If no rows come back then the mapper is never executed. The result of each mapping goes into a List so if no rows then your list would be empty.
List<MyType> rowsFromDatabaseMapped = jdbcTemplate.query(SQL_STR, new Object[]{any_params}, new MyTypeMapper());
Upvotes: 2