//package com.ynxbd.wx.config.datasource; // //import com.mchange.v2.c3p0.ComboPooledDataSource; //import com.ynxbd.common.helper.ErrorHelper; //import org.apache.commons.beanutils.BeanUtils; //import org.apache.commons.beanutils.BeanUtilsBean; //import org.apache.commons.beanutils.ConvertUtils; //import org.apache.commons.beanutils.converters.DateConverter; //import org.slf4j.Logger; //import org.slf4j.LoggerFactory; // //import java.lang.reflect.Field; //import java.sql.*; //import java.util.ArrayList; //import java.util.List; // // //public class C3P0DataSource { // private final static Logger log = LoggerFactory.getLogger(C3P0DataSource.class); // // // 内部接口 // public interface IPS { // void setParams(PreparedStatement ps) throws SQLException; // } // // private static ComboPooledDataSource dataSource; // private static ComboPooledDataSource dataSourcePacs; // private static ComboPooledDataSource dataSourceLis; // // private static void init() { // // 初始化读取c3p0-config.xml配置 // dataSource = new ComboPooledDataSource(); // } // // private static void initPacsDB() { // dataSourcePacs = new ComboPooledDataSource("pacs"); // } // // private static void initLisDB() { // dataSourceLis = new ComboPooledDataSource("lis"); // } // // /** // * 获取连接 // */ // public static Connection getConnection() { // Connection conn = null; // try { // if (dataSource == null) init(); // conn = dataSource.getConnection(); // } catch (Exception e) { // e.printStackTrace(); // log.error("数据库连接失败!{" + dataSource.getJdbcUrl() + "}"); // if (dataSource != null) dataSource.close(); // } // return conn; // } // // // /** // * 获取lis连接 // */ // public static Connection getLisConnection() { // Connection conn = null; // try { // if (dataSourceLis == null) initLisDB(); // conn = dataSourceLis.getConnection(); // } catch (Exception e) { // e.printStackTrace(); // log.error("lis数据库连接失败!{" + dataSourceLis.getJdbcUrl() + "}"); // // if (dataSourceLis != null) dataSourceLis.close(); // } // return conn; // } // // // /** // * 获取pacs连接 // */ // public static Connection getPacsConnection() { // Connection conn = null; // try { // if (dataSourcePacs == null) initPacsDB(); // conn = dataSourcePacs.getConnection(); // } catch (Exception e) { // e.printStackTrace(); // log.error("pacs数据库连接失败!{" + dataSourcePacs.getJdbcUrl() + "}"); // if (dataSourcePacs != null) dataSourcePacs.close(); // } // return conn; // } // // // public static void main(String[] args) { // } // //// public static void closePool() { //// if (dataSource != null) { //// dataSource.close(); //// } //// } // // private static String getDBName(String url) { // if (url.lastIndexOf("?") > 0) // return url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("?")); // else // return url.substring(url.lastIndexOf("/") + 1).trim(); // } // // // public static List selectList(String sql, Class clazz, IPS ips) { // return select(getLisConnection(), sql, clazz, ips); // } // // public static List selectList(String sql, Class clazz) { // return select(getLisConnection(), sql, clazz, null); // } // // /** // * prepareStatement 赋值查询 // * // * @param clazz 集合类型 // * @param ips 设置占位符数据 // * @param 泛型 // * @return 集合 // */ // public static List select(String sql, Class clazz, IPS ips) { // return select(getConnection(), sql, clazz, ips); // } // // /** // * 拼接sql语句查询 // * // * @param sql SQL语句 // * @param clazz 集合类型 // * @param 泛型 // * @return 泛型集合 // */ // public static List select(String sql, Class clazz) { // return select(getConnection(), sql, clazz, null); // } // // // /** // * [私有]查询方法封装 // * // * @param conn 连接 // * @param sql sql语句 // * @param clazz 类 // * @param ips ips // */ // private static List select(Connection conn, String sql, Class clazz, IPS ips) { // List list = new ArrayList<>(); // if (ips == null) { // Statement stmt = null; // ResultSet rs = null; // try { // stmt = conn.createStatement(); // rs = stmt.executeQuery(sql); // list = executeSelect(clazz, rs); // } catch (Exception e) { // e.printStackTrace(); // ErrorHelper.println(e); // } finally { // close(rs, stmt, conn); // } // } else { // PreparedStatement ps = null; // ResultSet rs = null; // try { // ps = conn.prepareStatement(sql); // ips.setParams(ps); // rs = ps.executeQuery(); // list = executeSelect(clazz, rs); // } catch (Exception e) { // ErrorHelper.println(e); // } finally { // close(rs, ps, conn); // } // } // return list; // } // // // /** // * 核心-执行查询 // * // * @param clazz 类型 // * @param rs rs // * @return 集合 // */ // private static List executeSelect(Class clazz, ResultSet rs) throws Exception { // List list = new ArrayList<>(); // Field[] fields = clazz.getDeclaredFields(); // // Class superclass = clazz.getSuperclass(); // Field[] superFields = superclass.getDeclaredFields(); // // T bean; // ResultSetMetaData rmd; // String colName; // // // // 解决BigDecimal类型转化报错 // BeanUtilsBean.getInstance().getConvertUtils().register(false, false, 0); // // 解决Date类型转化报错 // ConvertUtils.register(new DateConverter(null), java.util.Date.class); // // while (rs.next()) { // bean = clazz.newInstance(); // // rmd = rs.getMetaData(); // for (int i = 1; i <= rmd.getColumnCount(); i++) { // colName = rmd.getColumnLabel(i).toLowerCase(); // for (Field f : fields) { // f.setAccessible(true); // // if (f.getName().toLowerCase().equals(colName)) { // BeanUtils.setProperty(bean, f.getName(), rs.getObject(i)); // break; // } // } // // for (Field f : superFields) { // f.setAccessible(true); // if (f.getName().toLowerCase().equals(colName)) { // BeanUtils.setProperty(bean, f.getName(), rs.getObject(i)); // break; // } // } // } // list.add(bean); // } // return list; // } // // // /** // * 查询某列,一般用于查询count // * // * @param sql SQL语句 // * @param clazz 指定查询的数据的类型 // * @param ips 通过匿名函数设定占位符,可为空 // * @return clazz类型的数据 // */ // public static T selectByColumn(String sql, Class clazz, IPS ips) { // Connection conn = getConnection(); // PreparedStatement ps = null; // ResultSet rs = null; // T value = null; // try { // ps = conn.prepareStatement(sql); // if (ips != null) ips.setParams(ps); // rs = ps.executeQuery(); // while (rs.next()) { // value = rs.getObject(1, clazz); // } // } catch (Exception e) { // ErrorHelper.println(e); // } finally { // close(rs, ps, conn); // } // return value; // } // // /** // * 查询单个数据,主要用于count等单个字段的查询 // * // * @param sql SQL语句 // * @param clazz 指定查询的数据的类型 // * @return clazz类型的数据 // */ // public static T selectByColumn(String sql, Class clazz) { // return selectByColumn(sql, clazz, null); // } // // // /** // * 添加成功后返回自增ID // * // * @param sql SQL语句 // * @param ips ips // * @return 自增ID // */ // public static int insertGetId(String sql, IPS ips) { // Connection conn = null; // PreparedStatement ps = null; // ResultSet rs = null; // int id = 0; // try { // conn = getConnection(); // ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); // if (ips != null) ips.setParams(ps); // ps.executeUpdate(); // // rs = ps.getGeneratedKeys(); // if (rs.next()) { // id = rs.getInt(1); // } // ps.clearBatch(); // } catch (SQLException e) { // ErrorHelper.println(e); // try { // conn.rollback(); // } catch (SQLException err) { // ErrorHelper.println(err); // } // return 0; // } finally { // close(rs, ps, conn); // } // return id; // } // // /** // * 插入数据 // * // * @param sql SQL语句 // * @param ips 设置占位符数据 // * @return 修改成功行数 // */ // public static int insert(String sql, IPS ips) { // return update(sql, ips); // } // // /** // * 插入数据 // * // * @param sql SQL语句 // * @param ips 设置占位符数据 // * @return 修改成功行数 // */ // public static int delete(String sql, IPS ips) { // return update(sql, ips); // } // // // /** // * 增、删、改 // * // * @param sql SQL语句 // * @param ips 设置占位符数据 // * @return 修改成功行数 // */ // public static int update(String sql, IPS ips) { // int rows = 0; // try (Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) { // if (ips != null) ips.setParams(ps); // rows = ps.executeUpdate(); // } catch (Exception e) { // ErrorHelper.println(e); // } // return rows; // } // // // /** // * 关闭数据库 // * // * @param conn 数据库连接 // */ // public static void close(Connection conn) { // close(null, null, conn); // } // // public static void close(Statement stat, Connection conn) { // close(null, stat, conn); // } // // public static void close(ResultSet rs, Connection conn) { // close(rs, null, conn); // } // // /** // * 关闭连接-先开后关 // * // * @param rs rs // * @param stat ps // * @param conn conn // */ // public static void close(ResultSet rs, Statement stat, Connection conn) { // if (rs != null) { // try { // rs.close(); // } catch (SQLException e) { // ErrorHelper.println(e); // } // } // // if (stat != null) { // try { // stat.close(); // } catch (SQLException e) { // ErrorHelper.println(e); // } // } // // if (conn != null) { // try { // conn.close(); // } catch (SQLException e) { // ErrorHelper.println(e); // } // } // } // // //// /** //// * 分页查询 //// * //// * @param sql 查询数据的sql语句 //// * @param countSql 查询数据count的sql //// * @param clazz 数据类型 //// * @param ips ips //// * @return page对象 //// */ //// public static PageInfo pagingQuery(String sql, String countSql, Class clazz, IPS ips) { //// List list = ips == null ? query(sql, clazz) : query(sql, clazz, ips); //// //// long count = queryOne(countSql, Integer.class); //// //// PageInfo page = new PageInfo<>(); //// //// page.setTotal(count); //// //// if (list != null) page.setList(list); //// //// return page; //// } // // //// /** //// * 分页查询 //// * //// * @param sql 查询数据的sql语句 //// * @param countSql 查询数据count的sql //// * @param clazz 数据类型 //// * @return page对象 //// */ //// public static PageInfo pageQuery(String sql, String countSql, Class clazz) { //// return pagingQuery(sql, countSql, clazz, null); //// } //}