Java SQL ResultSet Read getListFromRS(Class clazz, ResultSet rs)

Here you can find the source of getListFromRS(Class clazz, ResultSet rs)

Description

get List From RS

License

Apache License

Declaration

public static <T> List<T> getListFromRS(Class<T> clazz, ResultSet rs) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static <T> List<T> getListFromRS(Class<T> clazz, ResultSet rs) {
        return getListFromRS(clazz, rs, false);
    }//from   w ww  .  j a v a 2s . co m

    public static <T> List<T> getListFromRS(Class<T> clazz, ResultSet rs, Boolean firstOnly) {
        Field field = null;
        List<T> lists = new ArrayList<T>();

        try {
            ResultSetMetaData rsmd = rs.getMetaData();
            int counts = rsmd.getColumnCount();
            while (rs.next()) {
                T t = clazz.newInstance();
                for (int i = 0; i < counts; i++) {
                    String elename = firstLowerCase(rsmd.getColumnName(i + 1));
                    field = clazz.getDeclaredField(elename);

                    Class<?> beanType = field.getType();

                    Object value = rs.getObject(elename);

                    if (value != null) {

                        Class<?> dbType = value.getClass();

                        if (dbType == java.sql.Timestamp.class && beanType == java.util.Date.class) {
                            value = new java.util.Date(((java.sql.Timestamp) value).getTime());
                        } else if (dbType == java.math.BigDecimal.class && beanType == double.class) {
                            value = new Double(value.toString());
                        } else if (dbType == java.math.BigDecimal.class && beanType == int.class) {
                            value = new Integer(value.toString());
                        }
                    }

                    String setMethodName = "set" + firstUpperCase(elename);
                    Method m = t.getClass().getMethod(setMethodName, beanType);

                    m.invoke(t, value);
                }

                lists.add(t);
                if (firstOnly)
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return lists;
    }

    public static String firstLowerCase(String old) {
        return old.substring(0, 1).toLowerCase() + old.substring(1);
    }

    public static String firstUpperCase(String old) {
        return old.substring(0, 1).toUpperCase() + old.substring(1);
    }
}

Related

  1. getHtmlTable(ResultSet results)
  2. getId(ResultSet key)
  3. getIndex(ResultSet rs, int index)
  4. getList(ResultSet resultSet, String columnLabel, Class clazz)
  5. getList(ResultSet rs)
  6. getListMapFromResultSet(ResultSet rs)
  7. getLocalDate(ResultSet res, String name)
  8. getLocalDate(ResultSet rs, String columnName)
  9. getMap(ResultSet resultSet)