数据库连接池

一开始先在内存中开辟一块空间(集合),一开始先往池子里放置多个连接对象。后面需要连接的话,直接从池子里面取,不需要去自己创建连接。使用完毕,记得归还连接,确保连接对象能够循环利用。

DBCP:

@Test
	public void testDBCP01(){
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			//构建数据源对象
			BasicDataSource dataSource=new BasicDataSource();
			dataSource.setDriverClassName("com.mysql.jdbc.Driver");
			dataSource.setUrl("jdbc:mysql://localhost/bank");
			dataSource.setUsername("root");
			dataSource.setPassword("123456");
			conn = dataSource.getConnection();
			String sql="insert into account values(null,?,?)"; 
			ps = conn.prepareStatement(sql);
			ps.setString(1, "admin");
			ps.setInt(2, 1000);
			ps.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JDBCUtil.release(conn, ps);
		}
	}

使用属性配置文件:

@Test
	public void testDBCP02() {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			BasicDataSourceFactory factory = new BasicDataSourceFactory();
			Properties properties=new Properties();
			FileInputStream is = new FileInputStream("src//dbcpconfig.properties");
			properties.load(is);
			DataSource dataSource=factory.createDataSource(properties);
			conn = dataSource.getConnection();
			String sql="insert into account values(null,?,?)"; 
			ps = conn.prepareStatement(sql);
			ps.setString(1, "xiaoliang");
			ps.setInt(2, 100);
			ps.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JDBCUtil.release(conn, ps);
		}
	}

C3P0

@Test
	public void testC3P0() {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			ComboPooledDataSource dataSource = new ComboPooledDataSource();
			dataSource.setDriverClass("com.mysql.jdbc.Driver");
			dataSource.setJdbcUrl("jdbc:mysql://localhost/bank");
			dataSource.setUser("root");
			dataSource.setPassword("123456");
			conn = dataSource.getConnection();
			String sql="insert into account values(null,?,?)"; 
			ps = conn.prepareStatement(sql);
			ps.setString(1, "admin01");
			ps.setInt(2, 1000);
			ps.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JDBCUtil.release(conn, ps);
		};
		
	}

使用配置文件。配置文件必须是c3p0-config.xml

@Test
	public void testC3P0() {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			ComboPooledDataSource dataSource = new ComboPooledDataSource();
			conn = dataSource.getConnection();
			String sql="insert into account values(null,?,?)"; 
			ps = conn.prepareStatement(sql);
			ps.setString(1, "admin02");
			ps.setInt(2, 1000);
			ps.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JDBCUtil.release(conn, ps);
		};
	}

DBUtils实现增删改查


    @Test
	public void testInsert() throws SQLException, InstantiationException, IllegalAccessException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		QueryRunner queryRunner = new QueryRunner(dataSource);
		//增加
		//queryRunner.update("insert into account values (null,?,?)","aa",1000);
		//删除
		//queryRunner.update("delete from account where id =?",5);
		//修改
		//queryRunner.update("update account set money = ? where id =?",100000,6);
		//查找
		/*
		 * Account account=queryRunner.query("select * from account where id =?", new
		 * ResultSetHandler<Account>() {
		 * 
		 * @Override public Account handle(ResultSet rs) throws SQLException { // TODO
		 * Auto-generated method stub Account account = new Account(); while(rs.next())
		 * { String name = rs.getString("name"); int money = rs.getInt("money");
		 * account.setName(name); account.setMoney(money); } return account; }
		 * 
		 * },6);
		 * System.out.println(account.toString());
		 */
		//查一条
		Account account=queryRunner.query("select * from account where id=?", new BeanHandler<Account>(Account.class),6);
		System.out.println(account);
		//查多条
		List<Account> list = queryRunner.query("select * from account", new BeanListHandler<Account>(Account.class));
		System.out.println(list);
	}

//通过类的字节码得到该类的实例
Account a = new Account();
//创建一个类的实例
Account a1 = Account.class.newInstance();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值