oracle数据库操作辅助类

package db;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.configuration.PropertyConfigurator;
import util.Config;
/**
* 数据库操作辅助类
* @author
*/
public class JDBCUtil {
private static Log logger = LogFactory.getLog(JDBCUtil.class);
static {
try {
Properties props = new Properties();
FileInputStream input = new FileInputStream(System.getProperty("user.dir") + "/config/proxool.properties");
props.load(input);
/**
* 来读取proxool.properties属性档
*/
PropertyConfigurator.configure(props);
} catch (ProxoolException e) {
logger.error("init proxool failt", e);
} catch (IOException e) {
logger.error("read proxool config file error!", e);
}
}

private JDBCUtil() {

}
/**
* 返回数据库连接,并设置为手动提交
* @return Connection 
*/
public static Connection getConnection() throws SQLException {
Connection con=null;
try {
con = DriverManager.getConnection("proxool.db");
con.setAutoCommit(false);
return con;
} catch (SQLException e) {
logger.error("can't get connection!",e);
ConnectionPool.getInstance().putConnection(con);
throw e;
}
}
/**
* 关闭ResultSet
* @param rs
*/
public static void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
}
catch (SQLException ex) {
logger.info("Could not close JDBC ResultSet", ex);
}
catch (Exception ex) {
logger.info("Unexpected exception on closing JDBC ResultSet", ex);
}
}
}
/**
* 关闭Statement
* @param stmt close statement
*/
public static void closeStatement(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException ex) {
logger.info("Can not close JDBC Statement", ex);
}
catch (Exception ex) {
logger.info("Unexpected exception about closing JDBC Statement", ex);
}
}
}
/**
* 关闭PreparedStatement
* @param stmt close PreparedStatement
*/
public static void closePreparedStatement(PreparedStatement stmt) {
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException ex) {
logger.info("Could not close JDBC Statement", ex);
}
catch (Exception ex) {
logger.info("Unexpected exception on closing JDBC Statement", ex);
}
}
}
/**
* 关闭PreparedStatement
* @param stmt close PreparedStatement
*/
public static void closeCallableStatement(CallableStatement stmt) {
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException ex) {
logger.info("Could not close JDBC Statement", ex);
}
catch (Exception ex) {
logger.info("Unexpected exception on closing JDBC Statement", ex);
}
}
}
/**
* 关闭Connection
* @param con
*/
public static void closeConnection(Connection con) {
if (con != null) {
try {
con.close();
}
catch (SQLException ex) {
logger.info("Could not close JDBC Connection", ex);
}
catch (Exception ex) {
logger.info("Unexpected exception on closing JDBC Connection", ex);
}
}
}
/**
* 回滚事务
* @param con
* @param des
*/
public static void rollback(Connection con, String des) {
if (con != null) {
try {
con.rollback();
} catch (Exception ex) {
logger.warn(des, ex);
}
}
}

public static void closeOthers(PreparedStatement ps, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
} catch (Exception e) {
Config.getLog().writeLog(1, "关闭结果集或操作对象失败:" + e.getMessage());
}
}
}

proxool.properties

jdbc.proxool.alias=db
jdbc.proxool.driver-url=jdbc:oracle:thin:@10.202.46.37:1521:sssadsit
jdbc.proxool.driver-class=oracle.jdbc.driver.OracleDriver
jdbc.user=sssw
jdbc.password=sssw1!!123
#jdbc.password=ENC(MzYtOWNkMjNkZjgtMzM2Zi00NmRkLWEwY2MtNmZlZmM1MjBiNjdhLWM5OTJlMjZjYWQ4ZDUzNWU=)
jdbc.proxool.house-keeping-sleep-time=1800000
jdbc.proxool.maximum-connection-count=20
jdbc.proxool.minimum-connection-count=5
jdbc.proxool.house-keeping-test-sql=select 2 from dual
jdbc.proxool.test-before-use=true

  

posted @ 2017-12-15 14:31  杯子茶壶  阅读(119)  评论(0)    收藏  举报