日期:2014-05-16  浏览次数:20524 次

最近看了以前写的的JDBC封装类,拿出来晒晒,大家提提意见

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