The concept of null in java: in java, null indicates that a class or variable is null and does not represent any object or instance. You can assign null to a reference type variable, however, null cannot be assigned to basic type variables. The concept of NULL in a database: null in a database indicates that null is not filled, unknown, or unavailable. Unlike java, NULL can be assigned to any data type in a database. In this way, after reading the field value from the database, how can we determine whether the read value is null in the java program? Is x = null used? Obviously not, unless X is of the String type. The wasNull () method in ResultSet can solve this problem:
WasNull (): report whether the last read column has a value of SQL NULL. Note: you must first call a get method for the column to try to read its value, and then call the wasNull method to check whether the read value is SQL NULL.
String value = "";
Int n;
While (rs. next ()){
Value = rs. getString ("name ");
If (rs. wasNull ()){
System. out. println ("null ");
} Else {
System. out. println (value );
}
N = rs. getInt ("number ");
If (rs. wasNull ()){
System. out. println ("null ");
} Else {
System. out. println (n );
}
}
Author: ERDP Technical Architecture"