package com.ydbg.gis.utils; import java.sql.SQLException; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ColumnListHandler; import org.apache.commons.dbutils.handlers.MapHandler; import org.apache.commons.dbutils.handlers.MapListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import org.apache.commons.lang.NumberUtils; /** * @author y * @date 2015-5-10 10:43:04 * @version V1.0 * @desc QuerryRunner 工具类 */ public final class QrUtil { private static final QueryRunner qr = new QueryRunner(); private static class QrUtilHolder{ private static final QrUtil instance = new QrUtil(); } public static QrUtil getInstance(){ return QrUtilHolder.instance; } /** * 执行 insert,delete,update * @param sql * @param params * @return */ public int update(String sql, Object params[]) { int i = -1; try { i = qr.update(TransactionManager.getInstance().get(), sql, params); } catch (SQLException ex) { Logger.getLogger(QrUtil.class.getName()).log(Level.SEVERE, null, ex); } finally{ TransactionManager.getInstance().close(); } return i; } /** * 执行多条的 insert,update * @param sql * @param params * @return */ public int batch(String sql, Object params[][]) { int i = -1; try { i = qr.batch(TransactionManager.getInstance().get(), sql, params).length; } catch (SQLException ex) { Logger.getLogger(QrUtil.class.getName()).log(Level.SEVERE, null, ex); } finally{ TransactionManager.getInstance().close(); } return i; } /** * 查询一个实体Bean,返回结果要进行强制类型转换 * @param sql * @param params * @param clazz * @return */ public Object queryBean(String sql, Object params[], Class clazz) { Object obj = null; try { obj = qr.query(TransactionManager.getInstance().get(), sql, new BeanHandler(clazz), params); } catch (SQLException ex) { Logger.getLogger(QrUtil.class.getName()).log(Level.SEVERE, null, ex); } finally{ TransactionManager.getInstance().close(); } return obj; } /** * 查询一个实体Bean Array,返回结果要进行强制类型转换 * @param sql * @param params * @param clazz * @return */ public Object queryBeanList(String sql, Object params[], Class clazz) { Object obj = null; try { obj = qr.query(TransactionManager.getInstance().get(), sql, new BeanListHandler(clazz), params); } catch (SQLException ex) { Logger.getLogger(QrUtil.class.getName()).log(Level.SEVERE, null, ex); } finally{ TransactionManager.getInstance().close(); } return obj; } /** * 获取一个Map * @param sql * @param params * @return */ public Map<String, Object> queryMap(String sql, Object params[]) { Map<String, Object> map = null; try { map = qr.query(TransactionManager.getInstance().get(), sql, new MapHandler(), params); } catch (SQLException ex) { Logger.getLogger(QrUtil.class.getName()).log(Level.SEVERE, null, ex); } finally{ TransactionManager.getInstance().close(); } return map; } /** * 获取List<Map<String,Object>> * @param sql * @param params * @return */ public List<Map<String, Object>> queryListMap(String sql, Object params[]){ List<Map<String, Object>> list = null; try { list = qr.query(TransactionManager.getInstance().get(), sql, new MapListHandler(), params); } catch (SQLException ex) { Logger.getLogger(QrUtil.class.getName()).log(Level.SEVERE, null, ex); } finally{ TransactionManager.getInstance().close(); } return list; } /** * 进行查询操作,返回一个数值(一般用于select count(id) from table的处理) * @param sql * @param params * @return */ public int queryForInt(String sql, Object params[]){ Object obj = null; try { obj = qr.query(TransactionManager.getInstance().get(), sql, new ScalarHandler(), params); } catch (SQLException ex) { Logger.getLogger(QrUtil.class.getName()).log(Level.SEVERE, null, ex); } finally{ TransactionManager.getInstance().close(); } return NumberUtils.stringToInt("" + obj, 0); } /** * 获取单列值 * @param sql * @param params * @return */ public List<String> queryListString(String sql, Object params[]) { List<String> list = null; try { list = qr.query(TransactionManager.getInstance().get(), sql, new ColumnListHandler<String>(1), params); } catch (SQLException ex) { Logger.getLogger(QrUtil.class.getName()).log(Level.SEVERE, null, ex); } finally{ TransactionManager.getInstance().close(); } return list; } }
使用方式:
public List<DjNsrxx> getDjNsrxxList(int count) { sb.setLength(0); sb.append(" select nsrdzdah,scjydz from gis_dj_nsrxx ") .append(" where (lng is null or lat is null) and rownum<? "); Object params[] = {count}; return (List<DjNsrxx>) QrUtil.getInstance().queryBeanList(sb.toString(), params, DjNsrxx.class); }
时间: 2024-11-07 19:09:48