您的位置:首页 > 其它

交换路由命令及配置

2016-02-22 16:52 441 查看
public class ConnectionManager {
private static ConnectionManager instance;
private ComboPooledDataSource ds;
public static Connection connection;
private ConnectionManager() throws Exception{
String path=ConnectionManager.class.getClassLoader().getResource("").getFile();
InputStream is = new FileInputStream(path+"/database.properties");
Properties prop = new Properties();
prop.load(is);
ds=new ComboPooledDataSource();
ds.setDriverClass(prop.getProperty("drive").trim());
ds.setJdbcUrl(prop.getProperty("url").trim());
ds.setUser(prop.getProperty("username").trim().toString());
ds.setPassword(prop.getProperty("password").trim().toString());
ds.setInitialPoolSize(Integer.parseInt(prop.getProperty("initPoolSize").trim()));
ds.setMaxPoolSize(Integer.parseInt(prop.getProperty("initPoolSize").trim()));
ds.setMinPoolSize(Integer.parseInt(prop.getProperty("minPoolSize").trim()));
ds.setAcquireIncrement(Integer.parseInt(prop.getProperty("acquireIncrement").trim()));
ds.setAutoCommitOnClose(true);//启动事物管理
ds.setTestConnectionOnCheckin(true);//
ds.setIdleConnectionTestPeriod(Integer.parseInt(prop.getProperty("testPoolTime").trim()));
ds.setMaxIdleTime(Integer.parseInt(prop.getProperty("maxTimePool").trim()));
ds.setAutoCommitOnClose(Boolean.parseBoolean(prop.getProperty("autoCommitOnClose").trim()));
ds.setAcquireRetryDelay(Integer.parseInt(prop.getProperty("acquireRetryDelay").trim()));
}
public static final ConnectionManager getInstance() {
if (instance == null) {
try {
instance = new ConnectionManager();
} catch (Exception e) {
e.printStackTrace();
}
}
return instance;
}

public synchronized final Connection getConnection() {
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}

protected void finalize() throws Throwable {
DataSources.destroy(ds); //
super.finalize();
}

}

public class DatabaseHandle {
private Connection connection;
private PreparedStatement preparedStatement;
private ResultSet result;
private CallableStatement call;
private DatabaseHandle() {
ConnectionManager connectionManger = ConnectionManager.getInstance();
connection = connectionManger.getConnection();
}

public static DatabaseHandle getDatabaseHandle(){
return new DatabaseHandle();
}
public List findList(String sql) throws SQLException{
return findList(sql,null);
}

public List findList(String sql, Object[] values) throws SQLException {
List list=new ArrayList();
try{

preparedStatement = connection.prepareStatement(sql);
setAttribute(preparedStatement, values);
result= preparedStatement.executeQuery();
ResultSetMetaData metadata=result.getMetaData();
int mapSize=metadata.getColumnCount();
while(result.next()){
Map map=new HashMap(mapSize);
for(int i=0;i<mapSize;i++){
map.put(metadata.getColumnLabel(i+1), result.getObject(metadata.getColumnLabel(i+1)));
}
list.add(map);
}
}catch(SQLException e){
throw e;
}
return list;
}
public void executeList(List<String> sqls,List<Object[]> objs) throws SQLException{
try{
if(sqls.size()!=objs.size()){return;}
connection.setAutoCommit(false);
for(int i=0,j=sqls.size();i<j;i++){
preparedStatement = connection.prepareStatement(sqls.get(i));
setAttribute(preparedStatement, objs.get(i));
preparedStatement.execute();
}
connection.commit();
connection.setAutoCommit(true);
}catch(SQLException e){
connection.rollback();
throw e;
}
}
public boolean insertData(String sql,Object values[]) throws SQLException{
boolean b;
try{
connection.setAutoCommit(false);
preparedStatement = connection.prepareStatement(sql);;
setAttribute(preparedStatement, values);
b=preparedStatement.execute();
connection.commit();
connection.setAutoCommit(true);
}catch(SQLException e){
connection.rollback();
throw e;
}
return b;
}
public boolean insertData(String sql) throws SQLException{
return insertData(sql,null);
}
public int updateData(String sql,Object values[]) throws SQLException{
int size;
connection.setAutoCommit(false);
try{
preparedStatement = connection.prepareStatement(sql);;
setAttribute(preparedStatement, values);
size=preparedStatement.executeUpdate();
connection.commit(); //
connection.setAutoCommit(true);
}catch(SQLException e){
connection.rollback();
throw e;
}
return size;
}

public void close(){
try{
if (result != null) {
result.close();
}
}catch(Exception e){}
try{
if (preparedStatement != null) {
preparedStatement.close();
}
}catch (Exception e){}
try{
if (call != null) {
call.close();
}
}catch (Exception e){}
try{
if (connection != null){
connection.close();
}
}catch (Exception e){}
}

public int updateData(String sql) throws SQLException{
return updateData(sql,null);
}
/**
* LIST
* @param callName
* @param list
* @return
* @throws SQLException
*/
public int[] callProcedureList(String callName,List<List> list) throws SQLException{
CallableStatement call=connection.prepareCall(callName);
int i[]=null;
try{
addList(call, list);
i=call.executeBatch();
}catch(SQLException e){
throw e;
}
return i;
}

/**
* 批处理 触发器
* @param callName
* @param list
* @return
* @throws SQLException
*/
public int[] callProcedureArray(String callName,List<Object[]> list) throws SQLException{
CallableStatement call=connection.prepareCall(callName);
int i[]=null;
try{
addArray(call, list);
i=call.executeBatch();
}catch(SQLException e){
throw e;
}
return i;
}
/**
* 集合参数批处理
* @param sql
* @param list
* @return
* @throws SQLException
*/
public int[] addBatchList(String sql,List<List> list) throws SQLException{
PreparedStatement preparedStatement=null;
int i[]=null;
try{
connection.setAutoCommit(false);
preparedStatement = connection.prepareStatement(sql);
addList(preparedStatement, list);
i=preparedStatement.executeBatch();
connection.commit(); //
connection.setAutoCommit(true);
}catch(SQLException e){
connection.rollback();
throw e;
}
return i;
}
/**
* 数组参数批处理
* @param sql
* @param list
* @return
* @throws SQLException
*/
public int[] addBatchArray(String sql,List<Object[]> list) throws SQLException{
PreparedStatement preparedStatement=null;
int i[]=null;
try{
preparedStatement = connection.prepareStatement(sql);
addArray(preparedStatement, list);
i=preparedStatement.executeBatch();
}catch(SQLException e){
connection.rollback();
throw e;
}
return i;
}

private void addArray(PreparedStatement preparedStatement,List<Object[]> list) throws SQLException{
for(Object[] objs:list){
for(int size=0;size<objs.length;size++){
preparedStatement.setObject(size+1, objs[size]);
}
preparedStatement.addBatch();
}
}
private void addList(PreparedStatement preparedStatement,List<List> list) throws SQLException{
for(List lists:list){
for(int size=0;size<lists.size();size++){
preparedStatement.setObject(size+1, lists.get(size));
}
preparedStatement.addBatch();
}
}
/**
* 设置属性
* @param preparedStatement
* @param values
* @throws SQLException
*/
private void setAttribute(PreparedStatement preparedStatement,
Object[] values) throws SQLException {
if (values != null) {
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
preparedStatement.setObject(i+1, values[i]);
}
}
}
}

配置文件
databaseName=mysql ##oracle
url=....
username=root
password=dcbicc
drive=com.mysql.jdbc.Driver
initPoolSize=10
maxPoolSize=100
minPoolSize=10
acquireIncrement=20
testPoolTime=60
maxTimePool=2500
autoCommitOnClose=true
acquireRetryDelay=1000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: