Java SQL Query queryObjectList(Connection con, String sql, Class objClass)

Here you can find the source of queryObjectList(Connection con, String sql, Class objClass)

Description

query Object List

License

Open Source License

Declaration

public static <T> List<T> queryObjectList(Connection con, String sql, Class<T> objClass)
            throws SQLException, InstantiationException, IllegalAccessException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Constructor;

import java.sql.*;
import java.util.*;

public class Main {
    public static <T> List<T> queryObjectList(Connection con, String sql, Class<T> objClass)
            throws SQLException, InstantiationException, IllegalAccessException {
        List<T> lists = new ArrayList<T>();
        Statement stmt = null;/*from   w  ww  . j a v  a2s. com*/
        ResultSet rs = null;
        try {
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);
            label: while (null != rs && rs.next()) {
                Constructor<?>[] constor = objClass.getConstructors();
                for (Constructor<?> c : constor) {
                    Object value = rs.getObject(1);
                    try {
                        lists.add((T) c.newInstance(value));
                        continue label;
                    } catch (Exception e) {
                    }
                }
            }
        } finally {
            if (null != rs)
                rs.close();
            if (null != stmt)
                stmt.close();
        }
        return lists;
    }

    public static <T> List<T> queryObjectList(Connection con, String sql, Class<T> objClass, Object... params)
            throws SQLException, InstantiationException, IllegalAccessException {
        List<T> lists = new ArrayList<T>();
        PreparedStatement preStmt = null;
        ResultSet rs = null;
        try {
            preStmt = con.prepareStatement(sql);
            for (int i = 0; i < params.length; i++)
                preStmt.setObject(i + 1, params[i]);
            rs = preStmt.executeQuery();
            label: while (null != rs && rs.next()) {
                Constructor<?>[] constor = objClass.getConstructors();
                for (Constructor<?> c : constor) {
                    String value = rs.getObject(1).toString();
                    try {
                        T t = (T) c.newInstance(value);
                        lists.add(t);
                        continue label;
                    } catch (Exception e) {
                    }
                }
            }
        } finally {
            if (null != rs)
                rs.close();
            if (null != preStmt)
                preStmt.close();
        }
        return lists;
    }
}

Related

  1. getStatement(Connection con, String query, Object... bits)
  2. query(Connection conn, String sql)
  3. query(Connection conn, String sql, Object[] params, Class beanClass)
  4. query(Connection connection, String sql, boolean isClose, Object... params)
  5. queryForList(Connection conn, String sql, int limit)
  6. queryProjectTicketIdFromTicketId(Connection db, int ticketId)
  7. queryReturnRS(String sql)
  8. runQuery(Connection con, String query)
  9. runSqlSelect(String sqlQuery, Connection databaseConnection)