亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: apache commons DbUtils使用說明[轉(zhuǎn)] [打印本頁(yè)]

作者: sinkingboat    時(shí)間: 2010-02-19 15:24
標(biāo)題: apache commons DbUtils使用說明[轉(zhuǎn)]
    commons-dbutils 是 Apache 組織提供的一個(gè)開源 JDBC 工具類庫(kù),對(duì)傳統(tǒng)操作數(shù)據(jù)庫(kù)的類進(jìn)行二次封裝,可以把結(jié)果集轉(zhuǎn)化成List。
項(xiàng)目主頁(yè):
http://commons.apache.org/dbutils/

文檔地址:
http://commons.apache.org/dbutils/examples.html
下載地址:
http://commons.apache.org/downloads/download_dbutils.cgi
(1)org.apache.commons.dbutils
DbUtils  : 提供如關(guān)閉連接、裝載 JDBC 驅(qū)動(dòng)程序等常規(guī)工作的工具類
QueryRunner : 該類簡(jiǎn)單化了 SQL 查詢,它與 ResultSetHandler 組合在一起使用可以完成大部分的數(shù)據(jù)庫(kù)操作,能夠大大減少編碼量。
QueryLoader : 屬性文件加載器,主要用于加載屬性文件中的 SQL 到內(nèi)存中。
(2)org.apache.commons.dbutils.handlers
ArrayHandler :將ResultSet中第一行的數(shù)據(jù)轉(zhuǎn)化成對(duì)象數(shù)組
ArrayListHandler將ResultSet中所有的數(shù)據(jù)轉(zhuǎn)化成List,List中存放的是Object[]
BeanHandler :將ResultSet中第一行的數(shù)據(jù)轉(zhuǎn)化成類對(duì)象
BeanListHandler :將ResultSet中所有的數(shù)據(jù)轉(zhuǎn)化成List,List中存放的是類對(duì)象
ColumnListHandler :將ResultSet中某一列的數(shù)據(jù)存成List,List中存放的是Object對(duì)象
KeyedHandler :將ResultSet中存成映射,key為某一列對(duì)應(yīng)為Map。Map中存放的是數(shù)據(jù)
MapHandler :將ResultSet中第一行的數(shù)據(jù)存成Map映射
MapListHandler :將ResultSet中所有的數(shù)據(jù)存成List。List中存放的是Map
ScalarHandler :將ResultSet中一條記錄的其中某一列的數(shù)據(jù)存成Object

(3)org.apache.commons.dbutils.wrappers
SqlNullCheckedResultSet :該類是用來對(duì)sql語(yǔ)句執(zhí)行完成之后的的數(shù)值進(jìn)行null的替換。
StringTrimmedResultSet :去除ResultSet中中字段的左右空格。Trim()
二、例子
Java代碼
  • public class TestDbUtils {  
  •   
  •     /**
  •      * BeanListHandler :將ResultSet中所有的數(shù)據(jù)轉(zhuǎn)化成List,List中存放的是類對(duì)象
  •      */  
  •     public static void getBeanListData() {  
  •         Connection conn = getConnection();  
  •         QueryRunner qr = new QueryRunner();  
  •         try {  
  •             ResultSetHandler rsh = new BeanHandler(TUser.class);  
  •             TUser usr = (TUser) qr.query(conn,"SELECT id,username,gender FROM t_user WHERE id=10000",rsh);  
  •             System.out.println(StringUtils.center("findById", 50, '*'));  
  •             System.out.println("id=" + usr.getId() + " name=" + usr.getUsername() + " gender=" + usr.getGender());  
  •               
  •             List results = (List) qr.query(conn,"SELECT id,username,gender FROM t_user",  
  •                     new BeanListHandler(TUser.class));  
  •             System.out.println(StringUtils.center("findAll", 50, '*'));  
  •             for (int i = 0; i
  •                 TUser user = (TUser) results.get(i);  
  •                 System.out.println("id=" + user.getId() + "  name=" + user.getUsername() + "  gender=" + user.getGender());  
  •             }  
  •         } catch (SQLException e) {  
  •             e.printStackTrace();  
  •         } finally {  
  •             DbUtils.closeQuietly(conn);  
  •         }  
  •     }  
  •   
  •     /**
  •      * MapListHandler :將ResultSet中所有的數(shù)據(jù)存成List。List中存放的是Map
  •      */  
  •     public static void getMapListData() {  
  •         Connection conn = getConnection();  
  •         QueryRunner qr = new QueryRunner();  
  •         try {  
  •             List results = (List) qr.query(conn,"SELECT id,username,gender FROM t_user",  
  •                     new MapListHandler());  
  •             for (int i = 0; i
  •                 Map map = (Map) results.get(i);  
  •                 System.out.println("id=" + map.get("id") + " name=" + map.get("username") + " gender=" + map.get("gender"));  
  •             }  
  •         } catch (SQLException e) {  
  •             e.printStackTrace();  
  •         } finally {  
  •             DbUtils.closeQuietly(conn);  
  •         }  
  •     }  
  •   
  •     /**
  •      *新增和更新例子
  •      */  
  •     public static void insertAndUpdateData(){  
  •         Connection conn = getConnection();  
  •         QueryRunner qr = new QueryRunner();  
  •         try{  
  •             //創(chuàng)建一個(gè)數(shù)組來存要insert的數(shù)據(jù)  
  •             Object[] insertParams = {"John Doe", "000000","男"};  
  •             int inserts = qr.update(conn, "INSERT INTO t_user(username,password,gender) VALUES (?,?,?)",  
  •                     insertParams );  
  •             System.out.println("inserted " + inserts + " data");  
  •               
  •             Object[] updateParams = {"111111", "John Doe"};  
  •             int updates = qr.update(conn, "UPDATE t_user SET password=? WHERE username=?",  
  •                                       updateParams );  
  •             System.out.println("updated "+ updates + " data");  
  •         }catch (SQLException e) {  
  •             e.printStackTrace();  
  •         } finally {  
  •             DbUtils.closeQuietly(conn);  
  •         }  
  •     }  
  •   
  •     /**
  •      * Unlike some other classes in DbUtils, this class(SqlNullCheckedResultSet) is NOT thread-safe.
  •      */  
  •     public static void findUseSqlNullCheckedResultSet(){  
  •         Connection conn = getConnection();  
  •         try{  
  •             Statement stmt = conn.createStatement();  
  •             ResultSet rs = stmt.executeQuery("SELECT id, username, gender FROM t_user");  
  •             SqlNullCheckedResultSet wrapper = new SqlNullCheckedResultSet(rs);  
  •             wrapper.setNullString("N/A"); // Set null string  
  •             rs = ProxyFactory.instance().createResultSet(wrapper);  
  •               
  •             while(rs.next()){  
  •                 System.out.println("id="+rs.getInt("id") + " username=" + rs.getString("username")  
  •                         + " gender="+rs.getString("gender"));  
  •             }  
  •               
  •             rs.close();  
  •         }catch(Exception e){  
  •             e.printStackTrace();  
  •         }finally{  
  •             DbUtils.closeQuietly(conn);  
  •         }  
  •     }  
  •       
  •     /****數(shù)據(jù)庫(kù)連接*** */  
  •     public static Connection getConnection() {  
  •         Connection conn = null;  
  •         String driver = "com.mysql.jdbc.Driver";  
  •         String url = "jdbc:mysql://127.0.0.1/springapp?useUnicode=true&characterEncoding=gb2312";  
  •   
  •         DbUtils.loadDriver(driver);  
  •   
  •         try {  
  •             conn = DriverManager.getConnection(url, "root", "root");  
  •         } catch (SQLException ex) {  
  •             ex.printStackTrace();  
  •         }  
  •         return conn;  
  •     }  
  •       
  • }  
                   
                   
                   

    本文來自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u/21752/showart_2182510.html




    歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2