import java.sql.*;importjava.util.List;importjava.util.Map;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importcom.alibaba.druid.pool.DruidDataSource;importcom.alibaba.druid.pool.DruidPooledConnection;public classDataSourceManagement {
Log log= LogFactory.getLog(DataSourceManagement.class);
DruidDataSource dataSource= newDruidDataSource();private DruidPooledConnection dpc = null;private Statement stmt = null;private DatabaseMetaData dbmd = null;privatePreparedStatement pst;private String schema = null;/***
* 初始化 数据源
*
*@paramdriverClass
*@paramusername
*@parampassword
*@paramUrl
*@paramschemaName*/
publicDataSourceManagement(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 voidupdate(String sql) {try{
stmt=dpc.createStatement();
stmt.executeUpdate(sql);
}catch(SQLException e) {
log.error(e.getMessage());
}
}public voidupdate(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());
}
}/*** 批量更新数据
*@paramsql sql
*@paramdatas 修改的数据,object[]中的数据和sql中?的位置一一对应
*@throwsSQLException*/
public void updateBatch(String sql, List datas) throwsSQLException {
pst=dpc.prepareStatement(sql);for(Object[] objects : datas) {for(int i=0;i
pst.setObject(i+ 1, objects[i]);
}
pst.addBatch();
}
pst.executeBatch();
}publicResultSet query(String sql) {
ResultSet rs= null;try{
stmt=dpc.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException e) {
log.error(e.getMessage());
}returnrs;
}publicResultSet 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());
}returnrs;
}/***
* 获取表信息列表
*@paramcatalog
*@paramschemaPattern
*@paramtableNamePattern
*@paramtypes
*@return
*/
publicResultSet 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());
}returnrs;
}/***
* 获取列信息
*@paramcatalog
*@paramschemaPattern
*@paramtableNamePattern
*@paramcolumnNamePattern
*@return
*/
publicResultSet 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());
}returnrs;
}/****
* 获取主键信息
*@paramcatalog
*@paramschemaPattern
*@paramtable
*@return
*/
publicResultSet getPrimaryKeys(String catalog,String schemaPattern,String table){
ResultSet rs= null;try{
rs=dbmd.getPrimaryKeys(catalog, schemaPattern, table);
}catch(SQLException e) {
log.error(e.getMessage());
}returnrs;
}/****
* 获取外键信息
*@paramcatalog
*@paramschemaPattern
*@paramtable
*@return
*/
publicResultSet getImportedKeys(String catalog,String schemaPattern,String table){
ResultSet rs= null;try{
rs=dbmd.getImportedKeys(catalog, schemaPattern, table);
}catch(SQLException e) {
log.error(e.getMessage());
}returnrs;
}/***
* 关闭数据源*/
public voidclose() {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());
}
}
}publicDruidDataSource getDataSource() {returndataSource;
}public voidsetDataSource(DruidDataSource dataSource) {this.dataSource =dataSource;
}publicDruidPooledConnection getDpc() {returndpc;
}public voidsetDpc(DruidPooledConnection dpc) {this.dpc =dpc;
}publicStatement getStmt() {returnstmt;
}public voidsetStmt(Statement stmt) {this.stmt =stmt;
}publicDatabaseMetaData getDbmd() {returndbmd;
}public voidsetDbmd(DatabaseMetaData dbmd) {this.dbmd =dbmd;
}publicPreparedStatement getPst() {returnpst;
}public voidsetPst(PreparedStatement pst) {this.pst =pst;
}
}