标签:common exce jdbcutil bean oid class obj tst nec
1, 列用工具包 阿里的
再次封装成通用的 update, query
package com.ljs.dao; import java.sql.Connection; import java.util.ArrayList; import java.util.List; 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.ScalarHandler; import com.ljs.util.JDBCUtil; public class DBUtilDao{ QueryRunner qRunner = new QueryRunner(); Connection connection = null; /* * 查询多条结果。 */ public<T> List<T> queryMutil(Class<T> clazz, String sql, Object...object){ List<T> list = new ArrayList<>(); try { connection = JDBCUtil.getConn(); list = qRunner.query(connection, sql,new BeanListHandler<T>(clazz),object); }catch (Exception e) { e.printStackTrace(); }finally { JDBCUtil.close(null, null, connection); } return list; } /* * 查询一条结果。 */ public<T> T queryOne(Class<T> clazz, String sql, Object...objects){ try { connection = JDBCUtil.getConn(); T t = clazz.newInstance(); t = qRunner.query(connection, sql, new BeanHandler<T>(clazz), objects); return t; } catch (Exception e) { e.printStackTrace(); } return null; } /* * 更新表 */ public<T> void update(String sql, Object...objects){ try { connection = JDBCUtil.getConn(); qRunner.update(connection, sql, objects); } catch (Exception e) { e.printStackTrace(); } } /* * 查询某个数scalar */ public String scalar(String sql, Object...objects){ try { connection = JDBCUtil.getConn(); return qRunner.query(connection, sql,new ScalarHandler<>(), objects).toString(); } catch (Exception e) { e.printStackTrace(); } return null; } }
标签:common exce jdbcutil bean oid class obj tst nec
原文地址:https://www.cnblogs.com/lijins/p/10122617.html