1:package com.xa12343.utils;
import java.io.BufferedReader;
import java.io.Reader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
/**
* xingsy
* **/
public class DBCommon{
public static final String XPFK="resources/xpfk.properties";
public String getVer(){
return "DBCommon V2.0@2013-12-04 13:04:23";
}
private boolean connected = false;
private Connection conn = null;
private DatabaseMetaData DBMetaData = null;
private Statement stmt;
private static BasicDataSource ds;
private ResultSet rs[] ={ null, null, null };
private Statement stmts[] ={ null, null, null };
protected String driverName;
protected String DBURL;
protected String userName;
protected String passWord;
protected String maxActive;
protected String maxIdle;
protected String maxWait;
protected String initialSize;
protected String errMsg = "";
private boolean isAutoCommit=true;
protected static String dbType="oracle";
private String CONFIG_BUNDLE_NAME = "resources/dbconfig.properties";
// private String CONFIG_BUNDLE_NAME = "resources/jeecg/jeecg_database.properties";
public DBCommon(){
if((ds == null))
initDS();
}
// 为了兼容 留下此方法
public DBCommon(boolean b){
if((ds == null))
initDS();
}
private void initDS(){
//SunLog.i("DBCommonV1.0","DB connection pool init...............");
Map<String,String> configBundle = PropertiesUtil.getAll(CONFIG_BUNDLE_NAME);
// driverName = configBundle.get("diver_name");
driverName = configBundle.get("jdbc.driverClassName.oracle");
DBURL = configBundle.get("jdbc.url.jeecg");
userName = configBundle.get("jdbc.username.jeecg");
passWord = configBundle.get("jdbc.password.jeecg");
maxActive = configBundle.get("maxActive");
maxIdle = configBundle.get("maxIdle");
dbType = configBundle.get("jdbc.dbType");
maxActive=maxActive==null?"100":maxActive;
maxIdle=maxIdle==null?"50":maxIdle;
maxWait=configBundle.get("maxWait");
maxWait=maxWait==null?"1000":maxWait;
initialSize=configBundle.get("initialSize");
initialSize=initialSize==null?"50":initialSize;
String minIdle=configBundle.get("initialSize");
minIdle=minIdle==null?"30":minIdle;
ds=new BasicDataSource();
ds.setDriverClassName(driverName);
ds.setPassword(passWord);
ds.setUsername(userName);
ds.setUrl(DBURL);
ds.setMaxActive(Common.parseInt(maxActive)); //最大活跃连接
ds.setMaxIdle(Common.parseInt(maxIdle)); //最大空闲连接连接
ds.setMinIdle(Common.parseInt(minIdle)); //最小空闲连接连接
ds.setInitialSize(Common.parseInt(initialSize)); //连接池初始化大小,初始化10个数据库连接
ds.setMaxWait(Common.parseLong(maxWait)); //3秒超时
ds.setTestOnBorrow(true);
ds.setTestOnReturn(true);
ds.setValidationQuery("SELECT 1 FROM DUAL ");
ds.setTestWhileIdle(true);
SunLog.i(getVer(),"DBtype:"+dbType+";DBURL:"+DBURL+";username:"+userName+";pw:acdefg"+passWord+"lidkasl;maxActive:"+ds.getMaxActive()+";minIdle:"+ds.getMinIdle()+";maxIdle:"+ds.getMaxIdle()+";maxWait:"+ds.getMaxWait()+";InitialSize:"+ds.getInitialSize()+";defaultAutoCommit:"+ds.getDefaultAutoCommit()+";removeAbandoned:"+ds.getRemoveAbandoned()+";removeAbandonedTimeout:"+ds.getRemoveAbandonedTimeout());
}
public DataSource getDataSource(){
if(ds==null) initDS();
return ds;
}
public boolean isConnected(){
return connected;
}
public Connection getConn(){
try{
if (conn != null && conn.isClosed())
connected = false;
}catch (SQLException ex){
SunLog.e("DB连接异常", ex.toString());
}
if (!dbConnect())
return null;
return conn;
}
public DatabaseMetaData getDBMetaData(){
if (!dbConnect())
return null;
return DBMetaData;
}
/*
* protected void finalize() { closedbConnect(); }
*/
// 获得一个Connection 对象--获得数据库连接。
public boolean dbConnect(){
try {
if (!connected||conn==null||conn.isClosed())
try{
if(ds==null) initDS();
conn=ds.getConnection();
DBMetaData = conn.getMetaData();
}catch (Exception e){
errMsg = "连接conn1:" + e.getMessage();
e.printStackTrace();
connected=false;
return false;
}
} catch (SQLException e) {
e.printStackTrace();
}
connected = true;
return true;
}
// --释放数据库连接,关闭Connection对象
public void closedbConnect(){
if (connected)
try{
errMsg = "";
conn.close();
}catch (Exception e){
errMsg = "连接关闭异常:" + e.getMes
import java.io.BufferedReader;
import java.io.Reader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
/**
* xingsy
* **/
public class DBCommon{
public static final String XPFK="resources/xpfk.properties";
public String getVer(){
return "DBCommon V2.0@2013-12-04 13:04:23";
}
private boolean connected = false;
private Connection conn = null;
private DatabaseMetaData DBMetaData = null;
private Statement stmt;
private static BasicDataSource ds;
private ResultSet rs[] ={ null, null, null };
private Statement stmts[] ={ null, null, null };
protected String driverName;
protected String DBURL;
protected String userName;
protected String passWord;
protected String maxActive;
protected String maxIdle;
protected String maxWait;
protected String initialSize;
protected String errMsg = "";
private boolean isAutoCommit=true;
protected static String dbType="oracle";
private String CONFIG_BUNDLE_NAME = "resources/dbconfig.properties";
// private String CONFIG_BUNDLE_NAME = "resources/jeecg/jeecg_database.properties";
public DBCommon(){
if((ds == null))
initDS();
}
// 为了兼容 留下此方法
public DBCommon(boolean b){
if((ds == null))
initDS();
}
private void initDS(){
//SunLog.i("DBCommonV1.0","DB connection pool init...............");
Map<String,String> configBundle = PropertiesUtil.getAll(CONFIG_BUNDLE_NAME);
// driverName = configBundle.get("diver_name");
driverName = configBundle.get("jdbc.driverClassName.oracle");
DBURL = configBundle.get("jdbc.url.jeecg");
userName = configBundle.get("jdbc.username.jeecg");
passWord = configBundle.get("jdbc.password.jeecg");
maxActive = configBundle.get("maxActive");
maxIdle = configBundle.get("maxIdle");
dbType = configBundle.get("jdbc.dbType");
maxActive=maxActive==null?"100":maxActive;
maxIdle=maxIdle==null?"50":maxIdle;
maxWait=configBundle.get("maxWait");
maxWait=maxWait==null?"1000":maxWait;
initialSize=configBundle.get("initialSize");
initialSize=initialSize==null?"50":initialSize;
String minIdle=configBundle.get("initialSize");
minIdle=minIdle==null?"30":minIdle;
ds=new BasicDataSource();
ds.setDriverClassName(driverName);
ds.setPassword(passWord);
ds.setUsername(userName);
ds.setUrl(DBURL);
ds.setMaxActive(Common.parseInt(maxActive)); //最大活跃连接
ds.setMaxIdle(Common.parseInt(maxIdle)); //最大空闲连接连接
ds.setMinIdle(Common.parseInt(minIdle)); //最小空闲连接连接
ds.setInitialSize(Common.parseInt(initialSize)); //连接池初始化大小,初始化10个数据库连接
ds.setMaxWait(Common.parseLong(maxWait)); //3秒超时
ds.setTestOnBorrow(true);
ds.setTestOnReturn(true);
ds.setValidationQuery("SELECT 1 FROM DUAL ");
ds.setTestWhileIdle(true);
SunLog.i(getVer(),"DBtype:"+dbType+";DBURL:"+DBURL+";username:"+userName+";pw:acdefg"+passWord+"lidkasl;maxActive:"+ds.getMaxActive()+";minIdle:"+ds.getMinIdle()+";maxIdle:"+ds.getMaxIdle()+";maxWait:"+ds.getMaxWait()+";InitialSize:"+ds.getInitialSize()+";defaultAutoCommit:"+ds.getDefaultAutoCommit()+";removeAbandoned:"+ds.getRemoveAbandoned()+";removeAbandonedTimeout:"+ds.getRemoveAbandonedTimeout());
}
public DataSource getDataSource(){
if(ds==null) initDS();
return ds;
}
public boolean isConnected(){
return connected;
}
public Connection getConn(){
try{
if (conn != null && conn.isClosed())
connected = false;
}catch (SQLException ex){
SunLog.e("DB连接异常", ex.toString());
}
if (!dbConnect())
return null;
return conn;
}
public DatabaseMetaData getDBMetaData(){
if (!dbConnect())
return null;
return DBMetaData;
}
/*
* protected void finalize() { closedbConnect(); }
*/
// 获得一个Connection 对象--获得数据库连接。
public boolean dbConnect(){
try {
if (!connected||conn==null||conn.isClosed())
try{
if(ds==null) initDS();
conn=ds.getConnection();
DBMetaData = conn.getMetaData();
}catch (Exception e){
errMsg = "连接conn1:" + e.getMessage();
e.printStackTrace();
connected=false;
return false;
}
} catch (SQLException e) {
e.printStackTrace();
}
connected = true;
return true;
}
// --释放数据库连接,关闭Connection对象
public void closedbConnect(){
if (connected)
try{
errMsg = "";
conn.close();
}catch (Exception e){
errMsg = "连接关闭异常:" + e.getMes