JDBC连接数据库之DBUtils简单使用(6)

Apache——DBUtils简介

commons-dbutils是Apache组织提供的一个开源JDBC工具类库,它是对JDBC的简单封装。

API介绍:

——org.apache.commons.dbutils.QueryRunner

——org.apache.commons.dbutils.ResultSetHandler

——工具类

       org.apache.commons.dbutils.DbUtils

jar:commons-dbutils.jar

测试:

<span style="font-family: Arial, Helvetica, sans-serif;">	/*</span><span style="font-family: Arial, Helvetica, sans-serif;"> * 测试QueryRunner类的update方法。</span>
	 * 可用于增删改查*/
   public void testQuertyRunnerUpdate(){
	   //创建QueryRunner的实现类。
	   QueryRunner queryRunner=new QueryRunner();
	   String sql="delete from customers "+
	   "where id in (?,?)";
	   Connection connection=null;
	   try{
		   connection=JDBCTools.getConnection();
		   queryRunner.update(connection, sql, 12,13);
	   }catch(Exception e){
		   e.printStackTrace();
	   }finally{
		   JDBCTools.releaseDB(null, null, connection);
	   }
	 
   }
测试查询方法:
	QueryRunner queryRunner=new QueryRunner();
	class MyResultSetHandler implements ResultSetHandler{

		public Object handle(ResultSet rs) throws SQLException {
	//		System.out.println("handle....");
			List<Customer> customers=new ArrayList<Customer>();
			while(rs.next()){
				Integer id=rs.getInt(1);
				String name=rs.getString(2);
				String email=rs.getString(3);
				Date birth=rs.getDate(4);
				Customer customer=new Customer(id, name, email, birth);
				customers.add(customer);
			}
			return customers;
		}
		
	}
	public void testQuery(){
		Connection connection=null;
		  try{
			   connection=JDBCTools.getConnection();
			   String sql="select id,name,email,birth from customers";
			Object  obj=  queryRunner.query(connection, sql, new MyResultSetHandler());
			System.out.println(obj);
		   }catch(Exception e){
			   e.printStackTrace();
		   }finally{
			   JDBCTools.releaseDB(null, null, connection);
		   }
	}
	//吧结果集转为一个数值。
	//查询多列时,只返回第一列。
	public void testScalarHandler(){
		Connection connection=null;
		 try{
			   connection=JDBCTools.getConnection();
			   String sql="select name from customers where id = ?";
			Object  result=  queryRunner.query(connection, sql,new ScalarHandler(),5);
			System.out.println(result);
		   }catch(Exception e){
			   e.printStackTrace();
		   }finally{
			   JDBCTools.releaseDB(null, null, connection);
		   }
	}
	//
	public void testMapListHandler(){
		Connection connection=null;
		 try{
			   connection=JDBCTools.getConnection();
			   String sql="select id,name,email,birth from customers ";
			List<Map<String,Object>>  result=  queryRunner.query(connection, sql,new MapListHandler());
			System.out.println(result);
		   }catch(Exception e){
			   e.printStackTrace();
		   }finally{
			   JDBCTools.releaseDB(null, null, connection);
		   }
	}
	//返回SQL对应的第一条记录对应的Map对象
	//键 :SQL查询的列名(不是别名),
	public void testMapHandler(){
		Connection connection=null;
		 try{
			   connection=JDBCTools.getConnection();
			   String sql="select id,name,email,birth from customers ";
			Map<String,Object>  result=  queryRunner.query(connection, sql,new MapHandler());
			System.out.println(result);
		   }catch(Exception e){
			   e.printStackTrace();
		   }finally{
			   JDBCTools.releaseDB(null, null, connection);
		   }
	}
	//转成List
	public void testBeanListHandler(){
		Connection connection=null;
		  try{
			   connection=JDBCTools.getConnection();
			   String sql="select * from customers ";
			List<Customer>  customers=  queryRunner.query(connection, sql,new BeanListHandler(Customer.class));
			System.out.println(customers);
		   }catch(Exception e){
			   e.printStackTrace();
		   }finally{
			   JDBCTools.releaseDB(null, null, connection);
		   }
	}
	//test BeanHandler
	//把结果集的第一条记录转为创建BeanHandler对象时传入的Class参数对应的对象。
	public void testBeanHandler(){
		Connection connection=null;
		  try{
			   connection=JDBCTools.getConnection();
			   String sql="select id,name,email,birth from customers where id = ?";
			Customer  customer=  queryRunner.query(connection, sql,new BeanHandler(Customer.class),1);
			System.out.println(customer);
		   }catch(Exception e){
			   e.printStackTrace();
		   }finally{
			   JDBCTools.releaseDB(null, null, connection);
		   }
	}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值