----- Original Message ----- From: "Mohammed Zabin" <[EMAIL PROTECTED]>
To: "Tomcat Users List" <users@tomcat.apache.org>
Sent: Thursday, June 21, 2007 6:45 AM
Subject: Null


Hi All

Anyone knows how to deal with null values in JDBC ResultSet??

I am trying to render a table in jsp page that read its value from the
database, sometimes, the database returns null values, and so, the whole
table couldn't be rendered. Is there any way to deal with null values.


The correct way to detect nulls in a JDBC ResultSet is rather different from the replies you've had so far. You should detect the presence of nulls in a given result set row by using the ResultSet.isNull() method.

So, let's say that we have a result set called 'rs' (you will probably have used a Statement or PreparedStatement or some other technique to get your result set from the database):

   Statement stmt = ____________; //an arbitrary Statement object
ResultSet rs = stmt.executeQuery("select employee_number, last_name, first_name, middle_initial from employee_table");

Now, on to the matter of nulls. Let's say that you are working your way through your ResultSet. As you know, you get the rows of the ResultSet one row at a time, using the next() method, as follows:

   while (rs.next()) { //as long as there are rows in the result set
       //handle one row of the result set
   }

Within the while loop, each iteration of the loop will handle one complete row of the result set.

Let's assume you don't know which columns of the result set are null and you want to display the values in each column of the result set exactly as they appear in the result set, except that when the value is null, you want to display the word "unknown". Here's what you would need to do:


   String empno = null;
   String lastname = null;
   String firstname = null;
   String midinit = null;

   while (rs.next()) {

empno = rs.getString("employee_number"); //get the employee number from this row of the result set
       if (rs.wasNull()) { //if the employee number is null
           System.out.println("Employee Number is unknown");
           }
       else {
           System.out.println("Employee Number is " + empno);
           }

lastname = rs.getString("last_name"); //get the last name from this row of the result set
       if (rs.wasNull()) { //if the last name is null
           System.out.println("Last Name is unknown");
           }
       else {
           System.out.println("Last Name  is " + lastname);
           }

firstname = rs.getString("first_name"); //get the first name from this row of the result set
       if (rs.wasNull()) { //if the first name is null
           System.out.println("First Name is unknown");
           }
       else {
           System.out.println("First Name is " + firstname);
           }


midinit = rs.getString("middle_initial"); //get the middle initial from this row of the result set
       if (rs.wasNull()) { //if the middle initial is null
           System.out.println("Middle Initial is unknown");
           }
       else {
           System.out.println("Middle Initial is " + midinit);
           }

       }

Although this example uses only result set columns that are being stored in Strings, the technique differs very little for non-text data. Let's say that we had a column called annual_salary in our result set, where the annual_salary was an integer containing the number of dollars that the person earned per year. Let's assume that some people are still negotiating their salaries so that the salary is stored as null for those people until the salary has been fully agreed.. The technique to handle the salary would look like this:

   int salary = null;

   while (rs.next()) {
salary = getInt("annual_salary"); //get the salary from this row of the result set
       if (rs.isNull()) { //if the salary is null
           System.out.println("Annual Salary is unknown");
           }
       else {
           System.out.println("Annual Salary is " + salary);
   }


I hope this helps you with your null handling!

--
Rhino

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to