import java.sql.*; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidPooledConnection; public class DataSourceManagement { Log log = LogFactory.getLog(DataSourceManagement.class); DruidDataSource dataSource = new DruidDataSource(); private DruidPooledConnection dpc = null; private Statement stmt = null; private DatabaseMetaData dbmd = null; private PreparedStatement pst; private String schema = null; /*** * 初始化 数据源 * * @param driverClass * @param username * @param password * @param Url * @param schemaName */ public DataSourceManagement(String driverClass, String username, String password, String Url, String schemaName) { /* * dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver"); * dataSource.setUsername("test"); * dataSource.setPassword("test"); * dataSource.setUrl("jdbc:oracle:thin:@IP:1521:orcl"); * * dataSource.setUsername("test"); * dataSource.setPassword("test"); * dataSource.setDriverClassName("com.mysql.jdbc.Driver"); * dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/mybatis"); */ schema = schemaName; dataSource.setDriverClassName(driverClass); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setUrl(Url); dataSource.setInitialSize(5); dataSource.setMinIdle(1); dataSource.setMaxActive(10); // 启用监控统计功能 try { dataSource.setFilters("stat"); dpc = dataSource.getConnection(); stmt = dpc.createStatement(); dbmd = dpc.getMetaData(); } catch (SQLException e) { log.error(e.getMessage()+" datasource init fail!"); } } public void update(String sql) { try { stmt = dpc.createStatement(); stmt.executeUpdate(sql); } catch (SQLException e) { log.error(e.getMessage()); } } public void update(String sql, Object[] objs) { try { pst = dpc.prepareStatement(sql); for (int i = 0; i < objs.length; i++) { pst.setObject(i + 1, objs[i]); } pst.executeUpdate(); } catch (SQLException e) { log.error(e.getMessage()); } } /** * 批量更新数据 * @param sql sql * @param datas 修改的数据,object[]中的数据和sql中?的位置一一对应 * @throws SQLException */ public void updateBatch(String sql, List<Object[]> datas) throws SQLException { pst = dpc.prepareStatement(sql); for(Object[] objects : datas) { for(int i=0;i<objects.length;i++) { pst.setObject(i + 1, objects[i]); } pst.addBatch(); } pst.executeBatch(); } public ResultSet query(String sql) { ResultSet rs = null; try { stmt = dpc.createStatement(); rs = stmt.executeQuery(sql); } catch (SQLException e) { log.error(e.getMessage()); } return rs; } public ResultSet query(String sql, Object[] objs) { ResultSet rs = null; try { pst = dpc.prepareStatement(sql); for (int i = 0; i < objs.length; i++) { pst.setObject(i + 1, objs[i]); } rs = pst.executeQuery(); } catch (SQLException e) { log.error(e.getMessage()); } return rs; } /*** * 获取表信息列表 * @param catalog * @param schemaPattern * @param tableNamePattern * @param types * @return */ public ResultSet getTables(String catalog,String schemaPattern,String tableNamePattern,String [] types){ ResultSet rs = null; try { rs = dbmd.getTables(catalog, schemaPattern, tableNamePattern, types); } catch (SQLException e) { log.error(e.getMessage()); } return rs; } /*** * 获取列信息 * @param catalog * @param schemaPattern * @param tableNamePattern * @param columnNamePattern * @return */ public ResultSet getColumns(String catalog, String schemaPattern,String tableNamePattern,String columnNamePattern){ ResultSet rs = null; try { rs = dbmd.getColumns(catalog, schemaPattern, tableNamePattern, columnNamePattern); } catch (SQLException e) { log.error(e.getMessage()); } return rs; } /**** * 获取主键信息 * @param catalog * @param schemaPattern * @param table * @return */ public ResultSet getPrimaryKeys(String catalog,String schemaPattern,String table){ ResultSet rs = null; try { rs = dbmd.getPrimaryKeys(catalog, schemaPattern, table); } catch (SQLException e) { log.error(e.getMessage()); } return rs; } /**** * 获取外键信息 * @param catalog * @param schemaPattern * @param table * @return */ public ResultSet getImportedKeys(String catalog,String schemaPattern,String table){ ResultSet rs = null; try { rs = dbmd.getImportedKeys(catalog, schemaPattern, table); } catch (SQLException e) { log.error(e.getMessage()); } return rs; } /*** * 关闭数据源 */ public void close() { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { log.error(e.getMessage()); } } if (dpc != null) { try { dpc.close(); } catch (SQLException e) { log.error(e.getMessage()); } } } public DruidDataSource getDataSource() { return dataSource; } public void setDataSource(DruidDataSource dataSource) { this.dataSource = dataSource; } public DruidPooledConnection getDpc() { return dpc; } public void setDpc(DruidPooledConnection dpc) { this.dpc = dpc; } public Statement getStmt() { return stmt; } public void setStmt(Statement stmt) { this.stmt = stmt; } public DatabaseMetaData getDbmd() { return dbmd; } public void setDbmd(DatabaseMetaData dbmd) { this.dbmd = dbmd; } public PreparedStatement getPst() { return pst; } public void setPst(PreparedStatement pst) { this.pst = pst; } }
浙公网安备 33010602011771号