Example usage for org.apache.commons.dbutils BasicRowProcessor toBeanList

List of usage examples for org.apache.commons.dbutils BasicRowProcessor toBeanList

Introduction

In this page you can find the example usage for org.apache.commons.dbutils BasicRowProcessor toBeanList.

Prototype

@Override
public <T> List<T> toBeanList(ResultSet rs, Class<T> type) throws SQLException 

Source Link

Document

Convert a ResultSet into a List of JavaBeans.

Usage

From source file:gr.osmosis.rcpsamples.contact.db.derby.DerbyContactsDAO.java

public Contact[] selectContacts(int id) {
    // Create Query
    // and where statement
    String whereStatement = "";
    if (id > -1) {
        whereStatement = " WHERE ID = " + id;
    }/*w  w w  .  ja v  a2s  .co  m*/

    StringBuffer sbSelect = new StringBuffer();
    sbSelect.append("SELECT * ");
    sbSelect.append(" FROM ");
    sbSelect.append(ContactsConstants.CONTACTS_TABLE_NAME);
    sbSelect.append(whereStatement);

    // Create a QueryRunner that will use connections from
    // the given DataSource
    DataSource d = DerbyDAOFactory.getDataSource();
    QueryRunner run = new QueryRunner(d);

    ResultSetHandler h = new ResultSetHandler() {
        public Object handle(ResultSet rs) throws SQLException {

            BasicRowProcessor p = new BasicRowProcessor();

            List l = p.toBeanList(rs, Contact.class);

            return l;
        }
    };
    Object result;
    ArrayList list;
    Contact[] c = null;
    try {
        result = run.query(sbSelect.toString(), h);

        list = (ArrayList) result;

        c = new Contact[list.toArray().length];
        list.toArray(c);

        System.out.print(result.toString());

    } catch (SQLException sex) {
        sex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return c;
}