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

来源:互联网 发布:百度poi数据抓取 编辑:程序博客网 时间:2024/06/05 16:15

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);   }}//转成Listpublic 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