MapListHandler()

来源:互联网 发布:xtream path cs6 mac 编辑:程序博客网 时间:2024/05/22 03:13

 DbUtils类

  ResultSetHandler 接口

  MapListHandler 类  (实现ResultSetHandler 接口)把从数据库中查询出的记录 都 放到List  集合当中, List集合中每一个对象都是Map类型,可以根据这条记录的字段名读出相对应的值.

  BeanListHandler 类  (实现ResultSetHandler 接口)把从数据库中的记录 放到List集合中 ,List集合中每一个对象都是一个JavaBean类型的对象,可以根据get 方法得到值

  QreryRunner类   执行SQL语名,其中一个参数为以上两种类型的对象

  01 import java.sql.Connection;

  02 import java.sql.SQLException;

  03 import java.util.List;

  04 import javax.naming.Context;

  05 import javax.naming.InitialContext;

  06 import javax.naming.NamingException;

  07 import javax.sql.DataSource;

  08 import mons.dbutils.QueryRunner;

  09 import mons.dbutils.ResultSetHandler;

  10 /**

  11  * 该类主要完成对数据库的操作

  12  *

  13  */

  14 public class SqlUtil {

  15     private static String datasoucename ="java:comp/env/jdbc/exam";

  16     private  DataSource da;

  17     private static sqlUtil sqlutil = null;

  18

  19     public static sqlUtil newInstance()

  20     {

  21         if(sqlutil == null) {

  22             sqlutil = new sqlUtil();

  23         }

  24         return sqlutil;

  25     }

  26

  27

  28     public Connection getConnection() throws SQLException

  29     {

  30         synchronized (da) {

  31             return da.getConnection();

  32         }

  33     }

  34

  35     /**

  36      * 构造方法,设置数据源

  37      * @param datasoucename

  38      */

  39     private sqlUtil() {

  40         init();

  41     }

  42

  43

  44     /**

  45      * 初始化数据源的方法

  46      */

  47     private void init() {

  48         try {

  49             Context ct = new InitialContext();

  50             this.da = (DataSource) ct.lookup(datasoucename);

  51             ct.close();

  52         } catch (NamingException e) {

  53             e.printStackTrace();

  54         }

  55

  56     }

  57

  58     /**

  59      * 对数据增删改的方法,需要一个object的数组,数据是sql语句的占位符

  60      * @param sql

  61      * @param pring

  62      * @return

  63      */

  64     public boolean update(String sql, Object pring[]) {

  65         QueryRunner qu = new QueryRunner();

  66         int i = 0;

  67         boolean flag = false;

  68         try {

  69             i = qu.update(getConnection(),sql, pring);

  70             if(i>=0){

  71                 flag = true;

  72             }

  73         } catch (SQLException e) {

  74             e.printStackTrace();

  75         }

  76

  77         return flag;

  78     }

  79

  80     /**

  81      * 对数据查找的方法,需要一个objece的数组,数据是sql语句的占位符,ResultSetHandler参数是你要把查询出的数据转换的类型

  82      * 可以是一个javabean   (如:new BeanListHandler (JavaBean.class)或:new MapListHandler )

  83      * @param sql

  84      * @param pring  参数数组可以为空

  85      * @param rsh

  86      * @return

  87      */

  88     public List query(String sql, Object pring[], ResultSetHandler rsh) {

  89         QueryRunner qu = new QueryRunner();

  90         List result = null;

  91         try {

  92             result = (List) qu.query(getConnection(),sql, pring, rsh);

  93         } catch (SQLException e) {

  94             // TODO Auto-generated catch block

  95             e.printStackTrace();

  96         }

  97         return result;

  98     }

  99 }

  调用:

  01 /**

  02      * 删除

  03      * @return boolean

  04      * @param ItemPool的Id

  05      */

  06     public boolean deteItemPool(String ipId) {

  07         String sql1 = "delete from ItemPool where ipId =? ";

  08 //指定所需要的参数 数组

  09         Object[] pring = {ipId};

  10 //执行更新

  11         boolean result = sql.update(sql1, pring);

  12         return result;

  13     }

  01 /**

  02      * 根据题库Id查找题库的方法

  03      */

  04     public ItemPool searchItemPoolById(String ipId) {

  05         String sql1 = "select * from ItemPool where ipId=?";

  06 //指定参数数组

  07         Object[] pring = {ipId};

  08 List list = sql.query(sql1, pring, new BeanListHandler(ItemPool.class));

  09         ItemPool itempool=(ItemPool)list.get(0);

  10         return itempool;

  11     }

0 0
原创粉丝点击