Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JdbcTemplate queryForList return value in case of no results

The question is pretty much summed up in the title. What will JdbcTemplate.queryForList() return when the query returns no results. Will it return an empty List or null value? I couldn't find a definitive answer from the documentation. Thanks in advance!

like image 266
Reins Avatar asked Jul 17 '14 07:07

Reins


People also ask

What does queryForList return?

queryForList(sql); JdbcTemplate's queryForList() method returns a list of rows from the table.

Does JdbcTemplate return null?

Clearly, if the RowMapper returns null, null will be added to the list. Since ArrayList permits null elements, this will return a list with nulls in it.

How do you use JdbcTemplate to find an object?

In Java's Spring framework, we can use jdbcTemplate. queryForObject() to get a single row record from the database, and convert the row into an object (i.e. POJO or your entity class) via the row mapper feature. Java's Spring framework provides class, which is most useful and also Recommended.

What is the default timeout for JdbcTemplate?

Set the query timeout for statements that this JdbcTemplate executes. Default is 0, indicating to use the JDBC driver's default. Note: Any timeout specified here will be overridden by the remaining transaction timeout when executing within a transaction that has a timeout specified at the transaction level.


2 Answers

The javadoc states that it will return

a List of objects that match the specified element type

If there's no element, the list is empty.

like image 144
Stephane Nicoll Avatar answered Oct 27 '22 22:10

Stephane Nicoll


It will return an empty list. So you can test it with list.isEmpty(). It will return true in this case.

like image 39
Sachin Kumar Avatar answered Oct 27 '22 21:10

Sachin Kumar