commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bali...@apache.org
Subject cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils DbUtils.java
Date Sun, 16 Mar 2003 13:59:21 GMT
baliuka     2003/03/16 05:59:20

  Modified:    dbutils/src/java/org/apache/commons/dbutils DbUtils.java
  Log:
  fixed null handling
  
  Revision  Changes    Path
  1.19      +32 -10    jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/DbUtils.java
  
  Index: DbUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/DbUtils.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- DbUtils.java	16 Mar 2003 13:28:53 -0000	1.18
  +++ DbUtils.java	16 Mar 2003 13:59:20 -0000	1.19
  @@ -64,6 +64,20 @@
   
   public final class DbUtils {
       
  +    static public java.util.Map DEFAULTS = new java.util.Hashtable();
  +    
  +    static{
  +        
  +     DEFAULTS.put(int.class,     new Integer(0));
  +     DEFAULTS.put(short.class,   new Short((short)0));
  +     DEFAULTS.put(byte.class,    new Byte((byte)0));
  +     DEFAULTS.put(float.class,   new Float(0f));
  +     DEFAULTS.put(double.class,  new Double(0.0));
  +     DEFAULTS.put(long.class,    new Long(0L));
  +     DEFAULTS.put(boolean.class, Boolean.FALSE);
  +     DEFAULTS.put(char.class,    new Character('\u0000'));
  +     
  +    }
      
       public static abstract class ListAdapter extends ArrayList implements ResultSetHandler{
           
  @@ -298,7 +312,10 @@
            ResultSetMetaData rsmd = rs.getMetaData();
            int cnt = rsmd.getColumnCount();
            for( int i = 1; i <= cnt; i++ ){
  -            result.put(rsmd.getColumnName(i),rs.getObject(i));  
  +            Object value = rs.getObject(i);
  +            if(!rs.wasNull()){
  +              result.put(rsmd.getColumnName(i),value);  
  +            }
            }
            return  result;         
       }
  @@ -322,15 +339,20 @@
            for( int i = 1; i <= cnt; i++ ){
              String name = rsmd.getColumnName(i);
              for( int j = 0; j < pd.length; j++ ){
  -                if(name.equals(pd[j].getName())){
  -                 try{   
  -                  pd[j].getWriteMethod().
  -                      invoke(obj, new Object[]{ rs.getObject(i) });
  -                 }catch(Exception e){
  -                   throw new DbException( "can not set " + name , e); 
  -                 }   
  -                      continue LOOP;
  -                }
  +               if(name.equals(pd[j].getName())){
  +                  Object value = rs.getObject(i);
  +                  if(!rs.wasNull() && pd[j].getPropertyType().isPrimitive() ){
  +                        DEFAULTS.get( pd[j].getPropertyType() );     
  +                  }
  +                  try{
  +                      
  +                     pd[j].getWriteMethod().invoke(obj, new Object[]{ value });
  +                     
  +                  }catch(Exception e){
  +                     throw new DbException( "can not set " + name , e); 
  +                  }   
  +                continue LOOP;
  +               }
              }
             throw new SQLException(name + " not found in " + obj.getClass().getName() );

            }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message