例子二:
动态创建属性
文件名:BeanUtilsExample2,源码:
ExpandedBlockStart.gif
None.gif package com.sy;
None.gif
None.gif import java.util.GregorianCalendar;
None.gif import org.apache.commons.beanutils.LazyDynaBean;
None.gif import org.apache.commons.beanutils.BeanUtils;
None.gif
ExpandedBlockStart.gif public class BeanUtilsExample2 {
InBlock.gif     //动态创建属性
ExpandedSubBlockStart.gif     public static void main(String args[]) throws Exception {
InBlock.gif
InBlock.gif         LazyDynaBean hh = new LazyDynaBean();
InBlock.gif         hh.set("country", "中国");
InBlock.gif         hh.set("city", "北京");
InBlock.gif         hh.set("postCode", "100120");
InBlock.gif         hh.set("addr", "aaaaaaa");
InBlock.gif
InBlock.gif         LazyDynaBean bb = new LazyDynaBean();
InBlock.gif         bb.set("phone", "home", "11011011");
InBlock.gif         bb.set("phone", "office", "111111");
InBlock.gif         bb.set("email", "sh@126.com");
InBlock.gif         bb.set("address", 0, hh);
InBlock.gif         bb.set("birthDate", new GregorianCalendar(1990, 3, 29).getTime());
InBlock.gif
InBlock.gif         LazyDynaBean tt = new LazyDynaBean();
InBlock.gif         tt.set("userId", new Long(8888888));
InBlock.gif         tt.set("gggg", "施杨");
InBlock.gif         tt.set("password", "sgsgsgsg");
InBlock.gif         tt.set("dddd", bb);
InBlock.gif
InBlock.gif         System.out.println(BeanUtils.getProperty(tt, "gggg"));
InBlock.gif         System.out.println(BeanUtils.getProperty(tt, "dddd.birthDate"));
InBlock.gif         System.out.println(BeanUtils.getProperty(tt,
InBlock.gif                 "dddd.address[0].addr"));
InBlock.gif         System.out
InBlock.gif                 .println(BeanUtils.getProperty(tt, "dddd.phone(office)"));
ExpandedSubBlockEnd.gif     }
ExpandedBlockEnd.gif }
None.gif
 
例子三:
连接Mysql数据库
文件名BeanUtilsExample3.java源码:
 
ExpandedBlockStart.gif
None.gif package com.sy;
None.gif
None.gif import java.sql.Connection;
None.gif import java.sql.DriverManager;
None.gif import java.sql.PreparedStatement;
None.gif import java.sql.ResultSet;
None.gif import java.sql.SQLException;
None.gif import java.util.Iterator;
None.gif import org.apache.commons.beanutils.DynaBean;
None.gif import org.apache.commons.beanutils.ResultSetDynaClass;
None.gif
ExpandedBlockStart.gif public class BeanUtilsExample3 {
ExpandedSubBlockStart.gif     public static void main(String args[]) throws Exception {
InBlock.gif         Connection conn = getConnection();
InBlock.gif         PreparedStatement ps = conn
InBlock.gif                 .prepareStatement("select id,title,time from guestbook2 order by id desc");
InBlock.gif         ResultSet rs = ps.executeQuery();
InBlock.gif
InBlock.gif         ResultSetDynaClass rsdc = new ResultSetDynaClass(rs);//重点,二次封装,对连接对象有依赖
InBlock.gif         Iterator itr = rsdc.iterator();
ExpandedSubBlockStart.gif         while (itr.hasNext()) {
InBlock.gif             DynaBean bean = (DynaBean) itr.next();
InBlock.gif             System.out.print(bean.get("id") + "\t");
InBlock.gif             System.out.print(bean.get("title") + "\t");
InBlock.gif             System.out.println(bean.get("time"));
ExpandedSubBlockEnd.gif         }
InBlock.gif         conn.close();
ExpandedSubBlockEnd.gif     }
InBlock.gif
ExpandedSubBlockStart.gif     private static Connection getConnection() {
InBlock.gif         String url = "jdbc:mysql://localhost:3306/guestbook";
InBlock.gif         String username = "root";
InBlock.gif         String password = "hicc";
InBlock.gif         Connection conn = null;
ExpandedSubBlockStart.gif         try {
InBlock.gif             Class.forName("com.mysql.jdbc.Driver");
InBlock.gif             conn = DriverManager.getConnection(url, username, password);
ExpandedSubBlockStart.gif         } catch (ClassNotFoundException e) {
InBlock.gif             e.printStackTrace();
ExpandedSubBlockStart.gif         } catch (SQLException e) {
InBlock.gif             e.printStackTrace();
ExpandedSubBlockEnd.gif         }
InBlock.gif         return conn;
ExpandedSubBlockEnd.gif     }
ExpandedBlockEnd.gif }
 
例子四:
文件BeanUtilsExample4.java,源码:
 
ExpandedBlockStart.gif
None.gif package com.sy;
None.gif
None.gif import java.sql.Connection;
None.gif import java.sql.DriverManager;
None.gif import java.sql.PreparedStatement;
None.gif import java.sql.ResultSet;
None.gif import java.sql.SQLException;
None.gif import java.util.Iterator;
None.gif import org.apache.commons.beanutils.DynaBean;
None.gif import org.apache.commons.beanutils.RowSetDynaClass;
None.gif
ExpandedBlockStart.gif public class BeanUtilsExample4 {
ExpandedSubBlockStart.gif     public static void main(String args[]) throws Exception {
InBlock.gif         Connection conn = getConnection();
InBlock.gif         PreparedStatement ps = conn
InBlock.gif                 .prepareStatement("select id,title,time from guestbook2 order by id desc");
InBlock.gif         ResultSet rs = ps.executeQuery();
InBlock.gif
InBlock.gif         RowSetDynaClass rsdc = new RowSetDynaClass(rs);
InBlock.gif         //重点,与ResultSetDynaClass的区别
InBlock.gif         conn.close();//重点,关闭连接后仍能读取
InBlock.gif         Iterator itr = rsdc.getRows().iterator();
ExpandedSubBlockStart.gif         while (itr.hasNext()) {
InBlock.gif             DynaBean bean = (DynaBean) itr.next();
InBlock.gif             System.out.print(bean.get("id") + "\t");
InBlock.gif             System.out.print(bean.get("title") + "\t");
InBlock.gif             System.out.println(bean.get("time"));
ExpandedSubBlockEnd.gif         }
ExpandedSubBlockEnd.gif     }
InBlock.gif
ExpandedSubBlockStart.gif     private static Connection getConnection() {
InBlock.gif         String url = "jdbc:mysql://localhost:3306/guestbook";
InBlock.gif         String username = "root";
InBlock.gif         String password = "hicc";
InBlock.gif         Connection conn = null;
ExpandedSubBlockStart.gif         try {
InBlock.gif             Class.forName("com.mysql.jdbc.Driver");
InBlock.gif             conn = DriverManager.getConnection(url, username, password);
ExpandedSubBlockStart.gif         } catch (ClassNotFoundException e) {
InBlock.gif             e.printStackTrace();
ExpandedSubBlockStart.gif         } catch (SQLException e) {
InBlock.gif             e.printStackTrace();
ExpandedSubBlockEnd.gif         }
InBlock.gif         return conn;
ExpandedSubBlockEnd.gif     }
ExpandedBlockEnd.gif }
这就是Common BeanUtils组件的主要应用!!!