`
一纸红颜岂值倾尽天下
  • 浏览: 27938 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

c3p0 连接池使用

    博客分类:
  • java
阅读更多

C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的开源项目Hibernate,Spring等。

 

 这里不详谈 只做实现 去往官网下载c3p0 jar包:http://www.126disk.com/fileview_1619526.html

 

1、jdbc.properties 配置文件

#JDBC连接驱动
jdbc.driver=com.mysql.jdbc.Driver
#服务器连接地址
jdbc.url=jdbc:mysql://127.0.0.1:3306/data?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
#用户名
jdbc.username=root
#密码
jdbc.password=123456

#连接池
#初始化连接
jdbc.initialPoolSize=10
# 最大连接
jdbc.maxPoolSize=20
#最小连接
jdbc.minPoolSize=5
#等待时间s
jdbc.maxIdleTime=30000

 

2、程序实现

package com.ai.util;

import java.beans.PropertyVetoException;
import java.io.*;
import java.sql.*;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.mchange.v2.c3p0.ComboPooledDataSource;


public class JDBCUtil {
	
	private static Logger log=Logger.getLogger(JDBCUtil.class);
	//使用c3p0 连接池
	private static ComboPooledDataSource ds;
	static {
		
		ds=new ComboPooledDataSource();
		
		//加载驱动
		try {
			ds.setDriverClass(getProperties("jdbc.driver"));
		} catch (PropertyVetoException e) {
			log.error("加载驱动失败",e);
			e.printStackTrace();
		}
		//数据库连接
		ds.setJdbcUrl(getProperties("jdbc.url"));
		//用户
		ds.setUser(getProperties("jdbc.username"));
		//密码
		ds.setPassword(getProperties("jdbc.password"));
		//初始化开几个连接
		ds.setInitialPoolSize(Integer.parseInt(getProperties("jdbc.initialPoolSize")));
		//最大开几个
		ds.setMaxPoolSize(Integer.parseInt(getProperties("jdbc.maxPoolSize")));
		//最小开几个
		ds.setMinPoolSize(Integer.parseInt(getProperties("jdbc.minPoolSize")));
		//设置等待时间
		ds.setMaxIdleTime(Integer.parseInt(getProperties("jdbc.maxIdleTime")));
	}

	
	public final static Connection getConnection(){
		
		try {
			return ds.getConnection();
		} catch (SQLException e) {
			log.error("获取连接失败",e);
			e.printStackTrace();
		}
		return null;
	}
	
	
	//关闭连接
	public final static void closeConntion(ResultSet re,PreparedStatement psnt,Connection conn){
		
		
		if(re!=null)
			try {
				re.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		if(psnt!=null)
			try {
				psnt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		if(conn!=null)
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	
	// 读取连接数据文件
	public final static String getProperties(String name){
		InputStream  str=Thread.currentThread().getContextClassLoader().getResourceAsStream("config/hibernet/jdbc.properties");
		Properties properties=new Properties();
		try {
			properties.load(str);
			return properties.getProperty(name);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			log.error("读取jdbc.properties文件不存在",e);
		} catch (IOException e) {
			e.printStackTrace();
			log.error("读取jdbc.properties文件异常",e);
		}
		return "";
	}
	
	
	
	public static String query(){
		
		
		Connection conn=JDBCUtil.getConnection();
		
		PreparedStatement psnt=null;
		
		ResultSet re=null;
		
		String sql="select * from customerinfo";
		try {
			psnt=conn.prepareStatement(sql);
			
			re=psnt.executeQuery();
			
			if(re.next()){
				return re.getString("customerid");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeConntion(re, psnt, conn);
		}
		return "";
	}
	
	
	public static void main(String[] args) {
		
		System.out.println(JDBCUtil.query());
//		System.out.println(getConnection());
//		System.out.println(getProperties("driver"));
//		System.out.println(getProperties("username"));
//		System.out.println(getProperties("password"));
//		System.out.println(getProperties("url"));
		
	}
}

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics