Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I check if resultset is empty or null using datastax cassandra driver for java

How should I check for an empty resultset using datastax java cassandra driver?

Suppose I'm executing the following query "SELECT * FROM my_table WHERE mykey=something"

there is a great chance that the query will not be matched. The following code does not work:

if (rs != null) 
   rs.one().getString("some_column");
like image 748
dtrihinas Avatar asked Apr 06 '15 12:04

dtrihinas


2 Answers

You were pretty close, the correct solution is:

Row r = rs.one();
if (r != null)
    r.getString("some_column");

The driver will always return a result set, whether or not there were any returned results. The documentation for one() states that if no rows were returned rs.one() returns null.

You can also use getAvailableWithoutFetching() which returns the number of rows in the result set without fetching more rows. Since pageSize has to be >= 1, you can be assured if there are is at least 1 row this will always return a value greater than 0.

like image 191
Andy Tolbert Avatar answered Oct 20 '22 04:10

Andy Tolbert


You can also check the value of rs.isExhausted() in order to determine whether or not there is additional data in the result set...

like image 38
Darwin Airola Avatar answered Oct 20 '22 04:10

Darwin Airola