Example usage for org.apache.commons.dbutils QueryRunner query

List of usage examples for org.apache.commons.dbutils QueryRunner query

Introduction

In this page you can find the example usage for org.apache.commons.dbutils QueryRunner query.

Prototype

public <T> T query(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException 

Source Link

Document

Execute an SQL SELECT query with replacement parameters.

Usage

From source file:org.rti.zcore.dar.utils.DatabaseUtils.java

/**
 * Stuffs AuditInfo//from w  w w  . j  a  v a2s . c om
 * @param conn
 * @param clazz
 * @param sql
 * @param values
 * @return
 * @throws ServletException
 * @throws SQLException
 * @throws ObjectNotFoundException
 */
public static Object getZEPRSAuditInfoBean(Connection conn, Class clazz, String sql, ArrayList values)
        throws ServletException, SQLException, ObjectNotFoundException {
    QueryRunner run = new QueryRunner();
    BeanProcessor beanprocessor = new AuditInfoBeanProcessor();
    RowProcessor convert = new ZEPRSRowProcessor(beanprocessor);
    ResultSetHandler h = new BeanHandler(clazz, convert);
    // return the results in a new object generated by the BeanHandler.
    Object result = null;
    try {
        result = run.query(conn, sql, values.toArray(), h);
        // log.info("Getting the bean " + clazz.toString());
    } catch (SQLException e) {
        //log.error(e);
        e.printStackTrace();
    }
    if (result == null) {
        throw new ObjectNotFoundException();
    }

    return result;
}

From source file:org.rti.zcore.dar.utils.DatabaseUtils.java

/**
 * Fetch a simple list of values//from  w  ww.  j a  v a 2s.  c o  m
 *
 * @param clazz
 * @param sql
 * @param values
 * @return
 * @throws ServletException
 * @throws SQLException
 */
public static List getList(Connection conn, Class clazz, String sql, ArrayList values)
        throws ServletException, SQLException {
    QueryRunner run = null;
    try {
        // run = new QueryRunner(dataSource);
        run = new QueryRunner();
    } catch (Exception e) {
        log.error(e);
    }
    ResultSetHandler h = new BeanListHandler(clazz, new BasicRowProcessor(new ZEPRSBeanProcessor()));
    // return the results in a new object generated by the BeanHandler.
    List list = null;
    try {
        list = (List) run.query(conn, sql, values.toArray(), h);
    } catch (SQLException e) {
        log.error(e);
        e.printStackTrace();
    }
    return list;
}

From source file:org.rti.zcore.dar.utils.DatabaseUtils.java

/**
 * For classes w/ nested objects such as AuditInfo. Takes a connection (reports)
 *
 * @param clazz//www .  j a v  a2s. co m
 * @param sql
 * @param values
 * @param convert
 * @return
 * @throws ServletException
 * @throws SQLException
 */
public static List getList(Connection conn, Class clazz, String sql, ArrayList values, RowProcessor convert)
        throws ServletException, SQLException {
    //DataSource dataSource = null;
    //dataSource = DatabaseUtils.getZEPRSDataSource();
    QueryRunner run = new QueryRunner();
    ResultSetHandler h = new BeanListHandler(clazz, convert);
    // return the results in a new object generated by the BeanHandler.
    List list = (List) run.query(conn, sql, values.toArray(), h);
    return list;

}

From source file:org.rti.zcore.dar.utils.DatabaseUtils.java

/**
 * Fetch a list of maps (Skaneateles)//from  w  ww . j  a  va 2s .  c  o  m
 * unused
 *
 * @param conn
 * @param sql
 * @param values
 * @return
 * @throws ServletException
 * @throws SQLException
 */
public static List getMapList(Connection conn, String sql, ArrayList values)
        throws ServletException, SQLException {
    //DataSource dataSource = null;
    //dataSource = DatabaseUtils.getZEPRSDataSource();
    QueryRunner run = new QueryRunner();
    ResultSetHandler h = new MapListHandler();
    // return the results in a new object generated by the BeanHandler.
    List list = (List) run.query(conn, sql, values.toArray(), h);
    return list;
}

From source file:org.rti.zcore.dar.utils.DatabaseUtils.java

/**
 * puts results set in key value pair into Map
 *
 * @param sql//from w ww .j a v  a  2  s .  com
 * @param values
 * @return
 * @throws ServletException
 * @throws SQLException
 */
public static Map getArrayList(Connection conn, String sql, ArrayList values)
        throws ServletException, SQLException {
    ResultSetHandler h = new ArrayListHandler();
    // DataSource dataSource = null;
    //dataSource = DatabaseUtils.getZEPRSDataSource();
    QueryRunner run = new QueryRunner();
    List result = (List) run.query(conn, sql, values.toArray(), h);
    Map map = new HashMap();
    for (int i = 0; i < result.size(); i++) {
        Object[] keyVal = (Object[]) result.get(i);
        map.put(keyVal[1], keyVal[0]);
    }
    return map;
}

From source file:pe.gob.sunat.tecnologia3.arquitectura.framework.desktop.dominio.dao.sqlite.UsuarioDaoSqlite.java

@Override
@LoggerAnnotation(modulo = "Dominio Model")
public Usuario buscarUsuario(String nombreUsuario) {
    logger.log(Level.INFO, "metodo: buscarUsuario...{0}", nombreUsuario);
    List<Usuario> listaUsuarios = new ArrayList<Usuario>();
    try {/*from   ww w  . ja va 2 s  .c o m*/
        String sqlQuery = sqlUsuarioQuery + "usuario =? and status=1";

        QueryRunner queryRunner = new QueryRunner();
        ResultSetHandler blh = new BeanListHandler(Usuario.class);

        listaUsuarios = (List<Usuario>) queryRunner.query(getConexion(), sqlQuery, blh, nombreUsuario);
        if (listaUsuarios != null && !listaUsuarios.isEmpty()) {
            return listaUsuarios.get(0);
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Error en el query." + ex.getMessage());
    }
    return null;
}