Builder http://builder.com.com Presents your JAVA E-NEWSLETTER for August 19, 2002 <-------------------------------------------> LEARN HOW TO HANDLE NULL IN THE DATABASE The Java Database Connectivity (JDBC) ResultSet class hides a very subtle bug for those who forget the wasNull method. Relational databases have the concept of a NULL value that's similar to Java's null. In a database, any type may be null. However, in Java, the primitive types may not be null. This causes a problem when reading a database field into a primitive type, resultSet.getInt(1). This has lead to the creators of the JDBC specification to institute a workaround. After a value is obtained from the database via a getXxx method such as getInt, getLong, or getFloat, the developer may call wasNull() to find out if the value was null. If wasNull returns true, then it's up to the developer to choose a suitable 'null' value when a primitive is being used. The value that was read by the getXxx method is driver dependant. For example, in one database driver, it may return 0, another may return the default value for that column, and yet another may return the last value the driver read in for that column. When you're expecting NULL values in the database and care about the default value, you need to code in the following manner: int idx = resultSet.getInt(1); if(resultSet.wasNull( )) { idx = -1; // this is our default value for idx } ---------------------------------------- CORRECTION Last time, we discussed working with thread dumps ("Thread dumps: Name your thread and view the system," Aug. 15, 2002). We said, "To get a thread dump of the system, run the server and press [Ctrl] [/]." The correct key combination is [Ctrl] [\]. For Windows users, the sequence is [Ctrl] [Break]. We apologize for any inconvenience this may have caused. ----------------------------------------