dgraham 2003/10/14 20:43:32 Modified: dbutils/src/java/org/apache/commons/dbutils DbUtils.java Log: Simplifed resultSetToBeanCollection() logic and return faster ArrayList instead of tired old Vector. Revision Changes Path 1.28 +73 -67 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.27 retrieving revision 1.28 diff -u -r1.27 -r1.28 --- DbUtils.java 15 Oct 2003 03:30:01 -0000 1.27 +++ DbUtils.java 15 Oct 2003 03:43:32 -0000 1.28 @@ -61,6 +61,10 @@ package org.apache.commons.dbutils; +import java.beans.BeanInfo; +import java.beans.IntrospectionException; +import java.beans.Introspector; +import java.beans.PropertyDescriptor; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -70,6 +74,7 @@ import java.sql.Types; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -380,68 +385,70 @@ return obj; } + /** - * optimized for collections of beans - * + * Optimized for collections of beans. */ - public static java.util.Collection resultSetToBeanCollection(ResultSet rs, Class cls) throws SQLException { - java.util.List results = new java.util.Vector(); - try{ - - if(rs.next()){ - - java.beans.BeanInfo bInfo = java.beans.Introspector.getBeanInfo(cls); - java.beans.PropertyDescriptor pd[] = bInfo.getPropertyDescriptors(); - ResultSetMetaData rsmd = rs.getMetaData(); - int cnt = rsmd.getColumnCount(); - int nameToIndex[] = new int[ cnt + 1 ]; + public static Collection resultSetToBeanCollection(ResultSet rs, Class cls) + throws SQLException { + + Collection results = new ArrayList(); + + if (!rs.next()) { + return results; + } + + BeanInfo beanInfo = null; + try { + beanInfo = Introspector.getBeanInfo(cls); + } catch (IntrospectionException ie) { + throw new DbException(ie); + } + + PropertyDescriptor pd[] = beanInfo.getPropertyDescriptors(); + ResultSetMetaData rsmd = rs.getMetaData(); + int cnt = rsmd.getColumnCount(); + int nameToIndex[] = new int[cnt + 1]; + + LOOP : 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())) { + nameToIndex[i] = j; + continue LOOP; + } + } + throw new SQLException(" index not found for " + name); + } + + do { + Object obj; + try { + obj = cls.newInstance(); + + } catch (Exception e) { + throw new DbException("can not create " + cls.getName(), e); + } + + for (int i = 1; i <= cnt; i++) { + Object value = rs.getObject(i); + int index = nameToIndex[i]; + if (rs.wasNull()) { + continue; + } - LOOP: - 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())){ - nameToIndex[i] = j; - continue LOOP; - } - } - throw new SQLException(" index not found for " + name ); - } - do{ - - Object obj; - try{ - - obj = cls.newInstance(); - - }catch(Exception e){ - throw new DbException( "can not create " + cls.getName() , e); - } - - for(int i = 1; i <= cnt; i++){ - Object value = rs.getObject(i); - int index = nameToIndex[i]; - if( rs.wasNull() ){ - continue; - } - try{ - - pd[index].getWriteMethod().invoke(obj, new Object[]{ value }); - - - }catch(Exception e){ - throw new DbException( "can not set " + pd[index].getName() , e); - } - } - results.add(obj); - }while(rs.next()); + try { + pd[index].getWriteMethod().invoke(obj, new Object[] { value }); + + } catch (Exception e) { + throw new DbException("can not set " + pd[index].getName(), e); + } } - }catch(java.beans.IntrospectionException ie){ - throw new DbException(ie); - } - - + results.add(obj); + + } while (rs.next()); + return results; } @@ -452,15 +459,14 @@ */ public static Object[] resultSetToArray(ResultSet rs) throws SQLException { ResultSetMetaData meta = rs.getMetaData(); - int sz = meta.getColumnCount(); - Object[] objs = new Object[sz]; - for (int i = 0; i < sz; i++) { + int cols = meta.getColumnCount(); + Object[] objs = new Object[cols]; + for (int i = 0; i < cols; i++) { Object obj = rs.getObject(i + 1); - if (rs.wasNull()) { - obj = null; - } - objs[i] = obj; + + objs[i] = rs.wasNull() ? null : obj; } + return objs; }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]