0

When querying the database directly, result is NULL, now Im using ResultSet to check if result is NULL, do something else, if result is not NULL, print the result: This my COde:

if(rs4 != null) {
            while(rs4.next()) {
            String ad =rs4.getString("number");                    
            System.out.println(ad);
            }
        }
        else{
            System.out.println("ZERO ENTRIES");
        }` 

Database row value is NULL,since there is no row returned from my query so definitely i expect the else statement to run, but now the if statement is still being excecuted and prints null

Beroo
  • 29
  • 8
  • Even if there are no results, rs4 should still not be null (unless your code does something strange). – Steve Smith Apr 05 '17 at 15:18
  • Assuming you are using `Statement`and `executeQuery` , it returns : "a `ResultSet` object that contains the data produced by the given query; _never null_" – Arnaud Apr 05 '17 at 15:19
  • 1
    Possible duplicate of [Java ResultSet how to check if there are any results](http://stackoverflow.com/questions/867194/java-resultset-how-to-check-if-there-are-any-results) – Arnaud Apr 05 '17 at 15:28
  • @steveSmith oh, okay, coz it prints true, so I need to check the value from the number collumn that is returned, it is supposed to be some numbers, but since i have no data for the selected query, it is empty. How do I go around that and print something different.... – Beroo Apr 05 '17 at 15:43
  • @berger Im using `PreparedStatement` – Beroo Apr 05 '17 at 15:43
  • @Beroo : `PreparedStatement` extends `Statement` . – Arnaud Apr 05 '17 at 15:44

3 Answers3

1

What you see as NULL when querying the DB directly is the value of the column number, not the value of the row.

so, that's why you get a non-null row with a NULL value in number column in your ResultSet.

alayor
  • 4,537
  • 6
  • 27
  • 47
0

You can use something like:

if (!resultSet.isBeforeFirst() ) {    
    System.out.println("No data"); 
} 

Or:

if (!resultSet.next() ) {
    System.out.println("no data");
} 
dcalap
  • 1,048
  • 2
  • 13
  • 37
  • as states by @alayor above, the row value returned is `NULL`, meaning there is something being returned, and I need a way to get past that. Row value is expected to be numbers, digits, and when empty, it should be `null` as expected, ....I want to get around that `null` aspect and print something else – Beroo Apr 05 '17 at 15:32
  • https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#wasNull() this maybe helps... – dcalap Apr 05 '17 at 15:33
0

I finally found a way to go around it. Using rs.getInt() this will return 0 whenever row value is NULL and return new row value , whenever there is a row found.

This solved it :-)

while(rs.next())
        {
            // using rs.getInt() will return zero when row value from your query is NULL
            int i = rs.getInt("number");
            if(i<0) //check if zero
            {
                // When result is NULL i =0
                System.out.println("your value");
            }else{
                //When there are rows found from database i > 0
                String ad=rs.getString("number");
                System.out.println(ad);
            }
        }`
Beroo
  • 29
  • 8