用c3p0实现Apache—DBUtils框架的Dao层源码

用c3p0实现Apache—DBUtils框架的Dao层源码
分类: 课堂随笔 2011-11-22 20:43 484人阅读 评论(0) 收藏 举报

用c3p0实现Apache—DBUtils框架的Dao层源码

      import java.sql.SQLException;

import java.util.Arrays;

import java.util.List;

import java.util.Map;



import org.apache.commons.dbutils.QueryRunner;

import org.apache.commons.dbutils.handlers.ArrayHandler;

import org.apache.commons.dbutils.handlers.ArrayListHandler;

import org.apache.commons.dbutils.handlers.BeanHandler;

import org.apache.commons.dbutils.handlers.BeanListHandler;

import org.apache.commons.dbutils.handlers.MapListHandler;



import com.hbsi.domain.User;

import com.hbsi.utils.DBManager_c3p0;



public class UserDaoImpl {

      // 插入操作

      public void insert() {

           // 得到数据库连接池对象

           QueryRunner runner = newQueryRunner(DBManager_c3p0.getDataSource());

           String sql = "insertinto users(name,password) values('liop','8988')";

           try {

                 runner.update(sql);

           } catch (SQLException e){

                 // TODOAuto-generated catch block

                 e.printStackTrace();

           }

      }



      // 删除操作

      public void delete() {

           QueryRunner runner = newQueryRunner(DBManager_c3p0.getDataSource());

           String sql = "deletefrom users where id=1";

           try {

                 runner.update(sql);

           } catch (SQLException e){

                 // TODOAuto-generated catch block

                 e.printStackTrace();

           }

      }



      // 更新操作

      public void update() {

           QueryRunner runner = newQueryRunner(DBManager_c3p0.getDataSource());

           String sql = "updateusers set name='老王',password=222 where id=3";

           try {

                 runner.update(sql);

           } catch (SQLException e){

                 // TODOAuto-generated catch block

                 e.printStackTrace();

           }

      }



      // 通过Id查询操作

      public void findById() {

           QueryRunner runner = newQueryRunner(DBManager_c3p0.getDataSource());

           String sql = "select* from users where id=3";

           BeanHandler rsh = newBeanHandler(User.class);

           try {

                 User user =runner.query(sql, rsh);

                 System.out.println(user.toString());

           } catch (SQLException e){

                 // TODOAuto-generated catch block

                 e.printStackTrace();

           }

      }



      // 查询所有

      public void findAll() {

           QueryRunner runner = newQueryRunner(DBManager_c3p0.getDataSource());

           String sql = "select* from users";

           BeanListHandler rsh = newBeanListHandler(User.class);



           try {

                 List<User>entities = runner.query(sql, rsh);

                 System.out.println(entities.size());

                 for (User entity :entities) {

                      System.out.println(entity.toString());

                 }

           } catch (SQLException e){

                 // TODOAuto-generated catch block

                 e.printStackTrace();

           }

      }



      // 批处理

      public void bath() {

           QueryRunner runner = newQueryRunner(DBManager_c3p0.getDataSource());

           String sql = "insertinto users(name,password) values(?,?)";

           Object[][] params = { {"aaa", "111" }, { "qqq", "222" },

                      {"www", "333" }, { "eee", "444" } };

           try {

                 runner.batch(sql,params);

           } catch (SQLException e){

                 // TODOAuto-generated catch block

                 e.printStackTrace();

           }

      }



      // ArrayHandler的使用

      public void testArrayHandler(){

           QueryRunner runner = newQueryRunner(DBManager_c3p0.getDataSource());

           String sql = "select* from users where id=5";

           ArrayHandler rsh = newArrayHandler();

           try {

                 Object[] array =runner.query(sql, rsh);

                 System.out.println(Arrays.asList(array));

           } catch (SQLException e){

                 // TODOAuto-generated catch block

                 e.printStackTrace();

           }

      }



      // ArrayListHandler的使用

      public void testArrayListHandler(){

           QueryRunner runner = newQueryRunner(DBManager_c3p0.getDataSource());

           String sql = "select* from users";

           ArrayListHandler rsh =new ArrayListHandler();

           try {

                 List<Object[]>array = runner.query(sql, rsh);

                 for (int i = 1; i< array.size(); i++) {

                      System.out.println(Arrays.asList(array.get(i)));

                 }

           } catch (SQLException e){

                 // TODOAuto-generated catch block

                 e.printStackTrace();

           }



      }



      // Map的应用

      public voidtestMapListHandler(){

           QueryRunner runner = newQueryRunner(DBManager_c3p0.getDataSource());

           String sql = "select* from users";

           MapListHandler rsh = newMapListHandler();

           List<Map<String,Object>> result=null;

           try {

                 result =(List<Map<String, Object>>) runner.query(sql,rsh);

                 for (int i = 0; i< result.size(); i++) {

                      Map<String,Object> map = result.get(i);

                      System.out.println("---------------------");



                      for(Map.Entry<String, Object> me : map.entrySet()) {

                            StringcolName = me.getKey();

                            Objectvalue = me.getValue();



                            System.out.println(colName+ " = " + value);

                      }

                 }

           } catch (SQLException e){

                 // TODOAuto-generated catch block

                 e.printStackTrace();

           }

         

      }



      public static voidmain(String[] args) {

           // new UserDaoImpl().insert();

           // newUserDaoImpl().delete();

           // newUserDaoImpl().update();

           // newUserDaoImpl().findById();

           // newUserDaoImpl().findAll();

           // 批处理

           // newUserDaoImpl().bath();

           // 数组使用

           // newUserDaoImpl().testArrayHandler();

           //

           //newUserDaoImpl().testArrayListHandler();

           newUserDaoImpl().testMapListHandler();

      }

你可能感兴趣的:(DbUtils)