Is there a way to avoid the isNull () method?

I have a large ResultSet(receiving from a request JDBC) several thousand rows. Using each of these lines, I have to create an instance Object, setting it according to the fields of this result set. Now, as we all know, methods of getXXX()this JDBC APIreturn 0if this column was null. Therefore, for each field of each row, I have to do wasNull()before setting the value in mine Object, which looks pretty ugly and can be inefficient. So, is there any other way I can avoid this?

Besides JDBC, if there is some completely different, standard, commonly used way, I can also find out about it.

Thank!

EDIT 1

patientInput.setRaceCodeId(patients.getShort("race_code_id"));  
if(patients.wasNull())
    patientInput.setRaceCodeId(null);

patientsis ResultSet. patientInputis Object. This is the code I'm trying to avoid. I mean, every time I do getXXX()and do setXXX(), I have to check again that what I got from ResultSetwas not null. If so, then set this field of the object as null, since it getXXX()returns 0in this case.

+3
source share
2 answers

Ok I believe that there are two possible approaches to "streamlining" your code. However, this can lead to a difference of opinion regarding what is neat!

Solution 1 - replace getXXX () with getObject () , which returns zero, for example.

Short s = (Short)patients.getObject("race_code_id");
patientInput.setRaceCodeId(s); 

2 - ,

protected final <T> T getNullableValue(T returnType, String colName, ResultSet rs) throws SQLException {  
  Object colValue = rs.getObject(colName);  
  return (T) colValue;  
}

final static Integer INT = 0;
final static Short SHORT = 0;
.
.
.
patientInput.setRaceCodeId(getNullableValue(SHORT,"race_code_id",patients));
+3

, , , , , . , .

, ORM, , Hibernate.

+1

All Articles