欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

利用commons-dbcp和commons-pool搭建数据库连接池

程序员文章站 2022-07-12 20:37:52
...
依赖commons-dbcp-1.4.jar和commons-pool-1.5.6.jar和ojdbc14.jar

TestDbPools.java代码:
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.log4j.Logger;

/**
 * 数据库连接池
 * */
public class TestDbPools {

	static Logger logger = Logger.getLogger(TestDbPools.class);
	
	private static BasicDataSource dataSource = null;
	
	public TestDbPools(){
		
	}
	
	public static void init() {
		if (dataSource != null) {
			try {
                dataSource.close();
            } catch (Exception e) {
            	logger.error(e.getMessage());
            }
            dataSource = null;
        }

        try {
            Properties p = new Properties();
            p.setProperty("driverClassName", "oracle.jdbc.driver.OracleDriver");
            p.setProperty("url", "jdbc:oracle:thin:@192.168.1.10:1521:oraid");
            p.setProperty("password", "zznode");
            p.setProperty("username", "shihuan");
            p.setProperty("maxActive", "30");
            p.setProperty("maxIdle", "10");
            p.setProperty("maxWait", "1000");
            p.setProperty("removeAbandoned", "false");
            p.setProperty("removeAbandonedTimeout", "120");
            p.setProperty("testOnBorrow", "true");
            p.setProperty("logAbandoned", "true");

            dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(p);
        } catch (Exception e) {
        	logger.error(e.getMessage());
        }
    }

	/**
	 * 外口调用方法,调用此方法即可获取一个数据库的连接池
	 * */
    public static synchronized Connection getConnection() {
        if (dataSource == null) {
            init();
        }
        Connection conn = null;
        if (dataSource != null) {
            try {
				conn = dataSource.getConnection();
			} catch (SQLException e) {
				logger.error(e.getMessage());
			}
        }
        return conn;
    }
}


OperatorDb.java代码:
private Connection conn = null;
	private PreparedStatement pstmt = null;
    private ResultSet rs = null;
	
    public ResultSet getRs(){
		conn = TestDbPools.getConnection();
		String sql = "SELECT T.MODULE_NAME, T.PARAM_NAME, T.SHOW_NAME, T.REL_SYMBOLS, T.GREEN_VALUE, T.YELLOW_VALUE, T.RED_VALUE, T.UNIT, T.SCPOE FROM TB_THRESHOLD_SETTING T WHERE T.PARAM_NAME = ?";
		try {
			pstmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
			pstmt.setString(1, "CSZJFX");
			rs = pstmt.executeQuery();
		    while(rs.next()){
		    	System.out.println("shihuan= " + rs.getString(1) + " -- " + rs.getString("MODULE_NAME"));
		    }
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return rs;
    }
    
    public void closeDb(){
    	if(rs != null){   //关闭记录集
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(pstmt != null){   //关闭声明
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn != null){   ////关闭连接
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
    }
    
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		OperatorDb odb = new OperatorDb();
		odb.getRs();
		odb.closeDb();
	}

}
相关标签: commons dbcp pool