Thursday, July 6, 2017

How to tell if value in database is 0 or null when the value in ResultSet is 0?

For example, you have a count column in your sales table. When you query the table,

      String sql = "select itemId, count from sales where employeeId = 950780";
      PreparedStatement ps = conn.prepareStatement(sql);
      ResultSet rs = ps.executeQuery();
      while (rs.next()) {
            System.out.println("Item: " + rs.getString("itemId") + ", Count: "+rs.getInt("count"));
      }

When the count is null in the database, it returns 0 by rs.getInt("count"). How could you tell if the count is 0 or null? The ResultSet wasNull() method will give you the answer.

     int count = rs.getInt("count"); //After this immediately call the wasNull method.
     boolean isNull = rs.wasNull();

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment