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