JDBC搭配DHCP链接池



DBCP

简要说明:DBCP——开源组件,连接池。

1. 需要用到三个Jar包:

commons-collections-3.2.1.jar

commons-dbcp-1.2.2.jar

commons-pool-1.5.2.jar


import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

/*
 * 此类是和数据库建立连接,直接对数据库进行插入、查询、删除、更新操作
 * */
public class JdbcUtil {
	private static Connection conn;

	private static ResultSet rs;

	private static Statement st;
	
    private static DataSource datasource;  

	public static void Init() { //初始化,创建连接
		try {
			InputStream inStream= JdbcUtil.class.getResourceAsStream("/dhcp.properties"); 
			Properties pro=new Properties();
			pro.load(inStream);
			datasource=BasicDataSourceFactory.createDataSource(pro);
			inStream.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("创建数据库连接失败");
		}
		
	}
	
	
	
	 public static void getConn(){  
	        try {
	        	if (datasource == null) {
	        		Init();
				}
	        	conn  =  datasource.getConnection();  
	        	st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
						ResultSet.CONCUR_UPDATABLE);
	        } catch (SQLException e) {  
	            e.printStackTrace(); 
	           
	        }  
	    } 
	 
	
	

	public static int Insert(String sql) { //执行插入操作
		int i = 0;
		try {
			i = st.executeUpdate(sql,Statement.RETURN_GENERATED_KEYS);
			rs = st.getGeneratedKeys();   
		    if(rs.next()){   
		    	i	=   rs.getInt(1);   
		    }  
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("插入数据失败");
		}
		return i;
	}

	public static ResultSet Query(String sql) { //查询操作

		try {
			rs = st.executeQuery(sql);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("查询数据失败");
		}
		return rs;
	}

	public static int Delete(String sql) { //删除操作
		int i = 0;
		try {
			i = st.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("删除数据失败");
		}
		return i;
	}

	public static int Update(String sql) { //更新操作
		int i = 0;
		try {
			i = st.executeUpdate(sql,Statement.RETURN_GENERATED_KEYS);
			rs = st.getGeneratedKeys();   
		    if(rs.next()){   
		    	i	=   rs.getInt(1);   
		    }
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("更新数据失败");
		}
		return i;
	}

	public static void close() { //关闭对象
		try {
			if (rs != null)
				rs.close();
			if (st != null)
				st.close();
			if (conn != null)
				conn.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("关闭数据失败");
		}
	}




dhcp.properties 配置
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://117.135.139.64:3306/jyonline?useUnicode=true&characterEncoding=utf-8
username=mysqladmin
password=blmysqlfr

#<!-- 初始化连接 -->
dataSource.initialSize=20

#<!-- 最大空闲连接 -->
dataSource.maxIdle=20

#<!-- 最小空闲连接 -->
dataSource.minIdle=5

#最大连接数量
dataSource.maxActive=100

#是否在自动回收超时连接的时候打印连接的超时错误
dataSource.logAbandoned=true

#是否自动回收超时连接
dataSource.removeAbandoned=true

#超时时间(以秒数为单位)
#设置超时时间有一个要注意的地方,超时时间=现在的时间-程序中创建Connection的时间,如果 maxActive比较大,比如超过100,那么removeAbandonedTimeout可以设置长一点比如180,也就是三分钟无响应的连接进行回收,当然应用的不同设置长度也不同。
dataSource.removeAbandonedTimeout=180

#<!-- 超时等待时间以毫秒为单位 -->
#maxWait代表当Connection用尽了,多久之后进行回收丢失连接
dataSource.maxWait=1000


http://www.java3z.com/cwbwebhome/article/article8/83504.html?id=4588

http://www.fengfly.com/plus/view-41597-1.html


DBCP

简要说明:DBCP——开源组件,连接池。

1. 需要用到三个Jar包:

commons-collections-3.2.1.jar

commons-dbcp-1.2.2.jar

commons-pool-1.5.2.jar

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值