dbcp的简单使用_数据库连接池
来源:互联网 发布:淘宝优惠券分享赚佣金 编辑:程序博客网 时间:2024/05/16 19:39
- 一.package dbutil;
- import javax.sql.*;
- import java.sql.*;
- import org.apache.commons.dbcp.BasicDataSource;
- /**
- * 数据库连接类,使用了DBCP 数据库连接池
- *
- */
- public class DBConnection {
- private static DataSource ds = null;
- private static boolean poolOK = false;
- final static String DBDRIVER = "com.mysql.jdbc.Driver";
- final static String DBURL = "jdbc:mysql://localhost:3306/test";
- final static String DBUSER = "root";
- final static String DBPWD = "root";
- /**
- * 取得数据库连接
- *
- * @return 一个数据池中的连接
- */
- public static Connection getConnection() {
- // 如果连接池未安装 或 连接池坏死, 安装连接池
- if (null == ds || false == poolOK) {
- ds = setupDataSource();
- }
- Connection con = null;
- try {
- con = ds.getConnection();
- poolOK = true;
- //检测连接是否有效,如果无效,会抛出异常
- //con.prepareStatement("select * from tab");
- } catch (SQLException e) {
- poolOK = false;
- shutdownDataSource(ds);
- System.out.println("数据库意外关闭" + e);
- }
- return con;
- }
- /**
- * 安装连接池
- *
- * @return 连接池对象
- *
- */
- private static DataSource setupDataSource() {
- BasicDataSource bds = new BasicDataSource();
- bds.setDriverClassName(DBDRIVER);
- bds.setUrl(DBURL);
- bds.setUsername(DBUSER);
- bds.setPassword(DBPWD);
- bds.setInitialSize(10);
- bds.setMaxActive(30);
- return bds;
- }
- /**
- * 关闭连接池
- */
- private static void shutdownDataSource(DataSource ds) {
- BasicDataSource bds = (BasicDataSource) ds;
- try {
- bds.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- /**
- * 打印 数据库连接池状态
- * @param ds
- * @throws SQLException
- */
- public static void printDataSourceStats() {
- BasicDataSource bds = (BasicDataSource) ds;
- System.out.println("NumActive: " + bds.getNumActive());
- System.out.println("NumIdle: " + bds.getNumIdle());
- }
- private DBConnection() {
- }
- public static void main(String[] args) {
- getConnection();
- getConnection();
- printDataSourceStats();
- }
- }
- 二.
- package dbcp;
- import java.sql.*;
- import org.apache.commons.pool.ObjectPool;
- import org.apache.commons.pool.impl.GenericObjectPool;
- import org.apache.commons.dbcp.ConnectionFactory;
- import org.apache.commons.dbcp.PoolingDriver;
- import org.apache.commons.dbcp.PoolableConnectionFactory;
- import org.apache.commons.dbcp.DriverManagerConnectionFactory;
- /*the class connect to oracle with thin methord*/
- public class DBPoolUtil {
- // the orcale jdbc dirver
- //static String driver = "oracle.jdbc.driver.OracleDriver";
- static String driver = "com.mysql.jdbc.Driver";
- // the connection is used to connect to database or dbcp
- private Connection conn = null;
- // the statement is used to execute SQL statement in database
- private Statement stmt = null;
- // the exact database URI locate in database
- //private static String sqlJdbc = "jdbc:oracle:thin:@192.168.0.77:1521:OEMTEP";
- private static String sqlJdbc = "jdbc:mysql://localhost:3306/test";
- // use name
- private static String sqlUser = "root";
- // password
- private static String sqlPwd = "root";
- // the class file of oracle jdbc make it get .class file dynamiclly
- private static Class driverClass = null;
- // the pointer which presents DBPC
- private static ObjectPool connectionPool = null;
- // the construct methord is automaticlly connect to the database for user
- public DBPoolUtil() {
- initDataSource();
- }
- // connect to oracle and print some information about it
- // and synchronized make the pool used a user one time
- private synchronized static void initDataSource() {
- if (driverClass == null) {
- try {
- // load the jdbc
- driverClass = Class.forName(driver);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- if (connectionPool == null) {
- try {
- setupDriver(sqlJdbc);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- // Display some pool statistics
- try {
- printDriverStats();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static void setupDriver(String connectURI) throws Exception {
- // get pool connection of DBPC
- connectionPool = new GenericObjectPool(null);
- ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
- connectURI, sqlUser, sqlPwd);
- PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
- connectionFactory, connectionPool, null, null, false, true);
- Class.forName("org.apache.commons.dbcp.PoolingDriver");
- PoolingDriver driver = (PoolingDriver) DriverManager
- .getDriver("jdbc:apache:commons:dbcp:");
- //
- driver.registerPool("auxdbpoll", connectionPool);
- }
- private static void printDriverStats() throws Exception {
- PoolingDriver driver = (PoolingDriver) DriverManager
- .getDriver("jdbc:apache:commons:dbcp:");
- ObjectPool connectionPool = driver.getConnectionPool("auxdbpoll");
- System.out.println("active connection: "
- + connectionPool.getNumActive());
- System.out.println("idle connection: " + connectionPool.getNumIdle());
- }
- // the a connection from the pool
- public Connection getConnection() {
- try {
- conn = DriverManager
- .getConnection("jdbc:apache:commons:dbcp:auxdbpoll");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return conn;
- }
- /**
- * 释放连接池
- */
- public static void ShutdownPool() {
- try {
- PoolingDriver driver = (PoolingDriver) DriverManager
- .getDriver("jdbc:apache:commons:dbcp:");
- driver.closePool("auxdbpoll");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- dbcp的简单使用_数据库连接池
- DBCP数据库连接池的简单使用
- DBCP数据库连接池的简单使用
- dbcp数据库连接池的使用
- DBCP数据库连接池的使用
- DBCP数据库连接池的使用
- DBCP数据库连接池的使用
- apache dbcp数据库连接池的使用
- 数据库连接池 dbcp与c3p0的使用
- java数据库连接池dbcp的使用
- dbcp,c3p0数据库连接池的使用
- 11、数据库连接池DBCP的使用
- 数据库连接池 dbcp与c3p0的使用
- dbcp数据库连接池简单例子
- 使用dbcp构建数据库连接池
- commons-dbcp.jar 数据库连接池学习_!!!
- dbcp数据库连接池的配置
- DBCP数据库连接池的配置
- Lumaqq移植到Android 之进阶篇
- Windows应用程序调试方法
- 利用dbutils简化数据库操作
- oracle中参数session和 processes的设置
- Thinking in AJAX(三) —— AJAX框架汇总
- dbcp的简单使用_数据库连接池
- Thinking in AJAX(二) —— 基于AJAX的WEB设计
- Thinking in AJAX(一) —— AJAX - X
- 九月八日 阴云
- Unix常用命令
- Java API中文版下载
- javascript 获取GET方法的参数
- Java 载入Jar内资源问题的探究
- 用JS判断两个数字的大小