• C3P0数据库连接池使用


    C3P0数据库连接池使用

    1、拷贝jar包:c3p0-0.9.1.2.jar     c3p0-0.9.1.2-jdk1.3.jar    c3p0-oracle-thin-extras-0.9.1.2.jar(oracle须要)

    2、书写配制文件放在src文件夹下:c3p0-config.xml(名字仅仅能是这个)

    3、类C3P0Util

    c3p0-config.xml 内容:

    <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
    	<!-- This is default config! -->
    	<default-config>
    		<property name="initialPoolSize">10</property>
    		<property name="maxIdleTime">30</property>
    		<property name="maxPoolSize">100</property>
    		<property name="minPoolSize">10</property>
    		<property name="maxStatements">200</property>
    	</default-config>
    
    	<!-- This is my config for mysql-->
    	<named-config name="mysql">
    		<property name="driverClass">com.mysql.jdbc.Driver</property>
    		<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
    		<property name="user">root</property>
    		<property name="password"></property>
    		<property name="initialPoolSize">10</property>
    		<property name="maxIdleTime">30</property>
    		<property name="maxPoolSize">100</property>
    		<property name="minPoolSize">10</property>
    		<property name="maxStatements">200</property>
    	</named-config>
    	
    	
    	<!-- This is my config for oracle -->
    	<named-config name="oracle">
    		<property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
    		<property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl</property>
    		<property name="user">scott</property>
    		<property name="password">liang</property>
    		<property name="initialPoolSize">10</property>
    		<property name="maxIdleTime">30</property>
    		<property name="maxPoolSize">100</property>
    		<property name="minPoolSize">10</property>
    		<property name="maxStatements">200</property>
    	</named-config>
    </c3p0-config>
    

    C3P0Util:

    package com.liang.util;
    
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Properties;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    /**
     * 数据库工具类
     * @author liang
     *
     */
    public class C3P0Util {
    	static ComboPooledDataSource cpds=null;
    	static{
    		//这里有个长处,写好配置文件,想换数据库。简单
    		//cpds = new ComboPooledDataSource("oracle");//这是oracle数据库
    		cpds = new ComboPooledDataSource("mysql");//这是mysql数据库
    	}
    	/**
    	 * 获得数据库连接
    	 * @return   Connection
    	 */
    	public static Connection getConnection(){
    		try {
    			return cpds.getConnection();
    		} catch (SQLException e) {
    			e.printStackTrace();
    			return null;
    		}
    	}
    	
    	/**
    	 * 数据库关闭操作
    	 * @param conn  
    	 * @param st    
    	 * @param pst
    	 * @param rs
    	 */
    	public static void close(Connection conn,PreparedStatement pst,ResultSet rs){
    		if(rs!=null){
    			try {
    				rs.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    		if(pst!=null){
    			try {
    				pst.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    
    		if(conn!=null){
    			try {
    				conn.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	/**
    	 * 測试DBUtil类
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Connection conn=getConnection();
    		System.out.println(conn.getClass().getName());
    		close(conn,null,null);
    	}
    }
    





  • 相关阅读:
    通过scrapy内置的ImagePipeline下载图片到本地、并提取本地保存地址
    算法的时间复杂度和空间复杂度
    session cookie的区别最全总结
    汉明码(海明码)计算方法
    测试 markdown
    PHP扩展--opcache安装及配置
    PHP_EOL
    BUG:php7.1 访问yii数据库 自动加端口3306 报错
    BUG:upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected
    图的遍历之 深度优先搜索和广度优先搜索
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6811878.html
Copyright © 2020-2023  润新知