MySql - 数据库连接池

       数据库连接池负责分配、管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个;释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏。这项技术能明显提高对数据库操作的性能。 --- 百度百科

C3P0连接池使用步骤;

1、导入相关jar包

      

2、编写c3p0-config.xml文件 -- 自动加载

<?xml version="1.0" encoding="utf-8"?>
<c3p0-config>
  <default-config>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/数据库名</property>
    <property name="user">root</property>
    <property name="password">密码</property>
    <!-- 初始化连接数 -->
    <property name="initialPoolSize">5</property>
    <!-- 最大连接数 -->
    <property name="maxPoolSize">10</property>
    <!-- 最长等待时间 -->
    <property name="checkoutTimeout">3000</property>
  </default-config>

  <named-config name="otherc3p0"> 
  </named-config>
</c3p0-config>

3、测试

public class C3P0Test {

	public static void main(String[] args) throws Exception {
		// 导入jar包
		// 初始化连接池对象
		DataSource ds = new ComboPooledDataSource();
		// 获取连接
		for (int i = 0; i < 10; i++) {
			Connection conn = ds.getConnection();
			System.out.println(conn);
		}
	}
}

Druid连接池使用步骤:

1、导入jar包

     

2、编写druid.properties文件

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/数据库名
username=root
password=密码
#初始化连接数量
initialSize=5
#最大连接数
maxActive=10
#最长等待时间
maxWait=3000
#最大空闲数
maxIdle=8
minIdle=3

3、测试

public class DruidTest {
	public static void main(String[] args) throws Exception {
		// 加载配置文件 -- druid 需手动加载配置文件
		Properties pro =  new Properties();
		InputStream is = DruidTest.class.getClassLoader().getResourceAsStream("druid.properties");
		pro.load(is);
		DataSource source = DruidDataSourceFactory.createDataSource(pro);
		Connection conn = source.getConnection();
		System.out.println(conn);
		//Connection conn = DruidUtils.getConnection(); 后面封装druid工具类、
		System.out.println(conn);			
	}
}

DruidUtils工具类

public class DruidUtils {
	private static DataSource ds;
	static {
		try {
			// 加载属性文件
			Properties pro = new Properties();
			pro.load(DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
			// 创建连接池对象
			ds = DruidDataSourceFactory.createDataSource(pro);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取连接
	 * 
	 * @return
	 * @throws SQLException
	 */
	public static Connection getConnection() throws SQLException {
		return ds.getConnection();
	}

	public DataSource getDataSource() {
		return ds;
	}

	/**
	 * 释放资源 -归还连接池
	 * 
	 * @param stat
	 * @param conn
	 */
	public void close(Statement stat, Connection conn) {

		if (stat != null) {
			try {
				stat.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 释放资源2
	 * 
	 * @param rs
	 * @param stat
	 * @param conn
	 */
	public void close(ResultSet rs, Statement stat, Connection conn) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		close(stat, conn);
	}
}

JdbcTemplate 使用

              spring 封装的对jdbc模板的封装。

public class JdbcTemplateTest {

	public static void main(String[] args) throws Exception {
		DataSource ds;
		Properties pro = new Properties();
		pro.load(DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
		// 创建连接池对象
		ds = DruidDataSourceFactory.createDataSource(pro);
		JdbcTemplate template = new JdbcTemplate(ds);
		// 插入一条语句
		
//		 String sql = "insert into userinfo values(default,?,?)";
//		 int index = template.update(sql, "zs", "123");
//		 System.out.println(index);
		
		// 修改一条记录
//		 String sql = "update userinfo set pwd = '789' where id = ?";
//		 int index = template.update(sql, 1);
//		 System.out.println(index);

		// 删除一条记录
//		 String sql = "delete from userinfo where id=?";
//		 int index = template.update(sql, 5);
//		 System.out.println(index);

		// 查询 QueryForMap -- 只能查询一条记录
//		 String sql = "select *from userinfo where id=?";
//		 Map<String, Object> index = template.queryForMap(sql,1);
//		 System.out.println(index);

		// 查询 queryForList -- 查询多条 且记录为Map类型
//		 String sql = "select *from userinfo";
//		 List<Map<String, Object>> index = template.queryForList(sql);
//		 System.out.println(index);

		// 查询Quert -- 将查询结果封装为一个JavaBean对象
		String sql = "select *from userinfo";
		List<UserInfo> index = template.query(sql, new BeanPropertyRowMapper<UserInfo>(UserInfo.class));
		for (UserInfo userInfo : index) {
			System.out.println(userInfo);
		}
		
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值