在线看毛片视频-国产免费av在线-欧美日韩一区二区三区-国产成人无码av在线播放无广告-亚洲人va欧美va人人爽-国产第一草草-西班牙黄色片-四虎在线网站8848-最新av片免费网站入口-东京热无码中文字幕av专区-日本大人吃奶视频xxxx-欧美精品一区二区三区四区五区-国产片天天弄-国产免费内射又粗又爽密桃视频-欧美爱爱网站-日韩v欧美

當前位置:雨林木風下載站 > 技術開發教程 > 詳細頁面

Jakarta-Common-BeanUtils研究心得(2)

Jakarta-Common-BeanUtils研究心得(2)

更新時間:2021-11-16 文章作者:未知 信息來源:網絡 閱讀次數:

允許隨意轉載,但請注明出處及作者。

=========================================

Jakarta-Common-BeanUtils研究心得(2)
SonyMusic
2003.05.13

六、ConstructorUtils補遺
創建對象還有一個方法:invokeExactConstructor,該方法對參數要求
更加嚴格,傳遞進去的參數必須嚴格符合構造方法的參數列表。
例如:
Object[] args={new Integer(1), "Jan"};
Class[] argsType={int.class, String.class};
Object obj;
//下面這句調用將不會成功,因為args[0]的類型為Integer,而不是int
//obj = ConstructorUtils.invokeExactConstructor(Month.class, args);

//這一句就可以,因為argsType指定了類型。
obj = ConstructorUtils.invokeExactConstructor(Month.class, args, argsType);
Month month=(Month)obj;
System.out.println(BeanUtils.getProperty(month,"value"));


七、MethodUtils
與ConstructorUtils類似,不過調用的時候,通常需要再指定一個method name的參數。

八、DynaClass/DynaBean
這似乎是BeanUtils中最有趣的部分之一了,很簡單,簡單到光看這兩個接口中的方法會不明白
為什么要設計這兩個接口。不過看到ResultSetDynaClass后,就明白了。下面是java doc中的代碼:
 ResultSet rs = ...;
 ResultSetDynaClass rsdc = new ResultSetDynaClass(rs);
 Iterator rows = rsdc.iterator();
 while (rows.hasNext()){
 DynaBean row = (DynaBean) rows.next();
 ... process this row ...
 }
 rs.close();
原來這是一個ResultSet的包裝器,ResultSetDynaClass實現了DynaClass,它的iterator方法返回一個
ResultSetIterator,則是實現了DynaBean接口。
在獲得一個DynaBean之后,我們就可以用
 DynaBean row = (DynaBean) rows.next();
 System.out.println(row.get("field1")); //field1是其中一個字段的名字

再看另一個類RowSetDynaClass的用法,代碼如下:
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/2hu?useUnicode=true&characterEncoding=GBK";
String username="root";
String password="";

java.sql.Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url);
ps=con.prepareStatement("select * from forumlist");
rs=ps.executeQuery();
//先打印一下,用于檢驗后面的結果。
while(rs.next()){
System.out.println(rs.getString("name"));
}
rs.beforeFirst();//這里必須用beforeFirst,因為RowSetDynaClass只從當前位置向前滾動

RowSetDynaClass rsdc = new RowSetDynaClass(rs);
rs.close();
ps.close();
List rows = rsdc.getRows();//返回一個標準的List,存放的是DynaBean
for (int i = 0; i <rows.size(); i++) {
DynaBean b=(DynaBean)rows.get(i);
System.out.println(b.get("name"));
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
con.close();
} catch (Exception e) {
}
}

是不是很有趣?封裝了ResultSet的數據,代價是占用內存。如果一個表有10萬條記錄,rsdc.getRows()
就會返回10萬個記錄。@_@

需要注意的是ResultSetDynaClass和RowSetDynaClass的不同之處:
1,ResultSetDynaClass是基于Iterator的,一次只返回一條記錄,而RowSetDynaClass是基于
List的,一次性返回全部記錄。直接影響是在數據比較多時ResultSetDynaClass會比較的快速,
而RowSetDynaClass需要將ResultSet中的全部數據都讀出來(并存儲在其內部),會占用過多的
內存,并且速度也會比較慢。
2,ResultSetDynaClass一次只處理一條記錄,在處理完成之前,ResultSet不可以關閉。
3,ResultSetIterator的next()方法返回的DynaBean其實是指向其內部的一個固定
對象,在每次next()之后,內部的值都會被改變。這樣做的目的是節約內存,如果你需要保存每
次生成的DynaBean,就需要創建另一個DynaBean,并將數據復制過去,下面也是java doc中的代碼:
 ArrayList results = new ArrayList(); // To hold copied list
 ResultSetDynaClass rsdc = ...;
 DynaProperty properties[] = rsdc.getDynaProperties();
 BasicDynaClass bdc =
 new BasicDynaClass("foo", BasicDynaBean.class,
rsdc.getDynaProperties());
 Iterator rows = rsdc.iterator();
 while (rows.hasNext()) {
 DynaBean oldRow = (DynaBean) rows.next();
 DynaBean newRow = bdc.newInstance();
 PropertyUtils.copyProperties(newRow, oldRow);
 results.add(newRow);
 }

事實上DynaClass/DynaBean可以用于很多地方,存儲各種類型的數據。自己想吧。嘿嘿。


九、自定義的CustomRowSetDynaClass
兩年前寫過一個與RowSetDynaClass目標相同的類,不過多一個功能,就是分頁,只取需要的數據,
這樣內存占用就會減少。

先看一段代碼:
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost/2hu?useUnicode=true&characterEncoding=GBK";
String username="root";
String password="";

java.sql.Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url);
ps=con.prepareStatement("select * from forumlist order by name");
rs=ps.executeQuery();
/*
while(rs.next()){
System.out.println(rs.getString("name"));
}
rs.beforeFirst();
*/

//第二個參數表示第幾頁,第三個參數表示頁的大小
CustomRowSetDynaClass rsdc = new CustomRowSetDynaClass(rs, 2, 5);
//RowSetDynaClass rsdc = new RowSetDynaClass(rs);
rs.close();
ps.close();
List rows = rsdc.getRows();
for (int i = 0; i <rows.size(); i++) {
DynaBean b=(DynaBean)rows.get(i);
System.out.println(b.get("name"));
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
con.close();
} catch (Exception e) {
}
}
在這里用到了一個CustomRowSetDynaClass類,構造方法中增加了page和pageSize兩個參數,
這樣,不管數據庫里有多少條記錄,最多只取pageSize條記錄,若pageSize==-1,則功能和
RowSetDynaClass一樣。這在大多數情況下是適用的。該類的代碼如下:

package test.jakarta.commons.beanutils;

import java.io.*;
import java.sql.*;
import java.util.*;

import org.apache.commons.beanutils.*;

/**
* @author SonyMusic
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class CustomRowSetDynaClass implements DynaClass, Serializable {

// ----------------------------------------------------------- Constructors

/**
* <p>Construct a new {@link RowSetDynaClass} for the specified
* <code>ResultSet</code>.The property names corresponding
* to column names in the result set will be lower cased.</p>
*
* @param resultSet The result set to be wrapped
*
* @exception NullPointerException if <code>resultSet</code>
*is <code>null</code>
* @exception SQLException if the metadata for this result set
*cannot be introspected
*/
public CustomRowSetDynaClass(ResultSet resultSet) throws SQLException {

this(resultSet, true);

}

/**
* <p>Construct a new {@link RowSetDynaClass} for the specified
* <code>ResultSet</code>.The property names corresponding
* to the column names in the result set will be lower cased or not,
* depending on the specified <code>lowerCase</code> value.</p>
*
* <p><strong>WARNING</strong> - If you specify <code>false</code>
* for <code>lowerCase</code>, the returned property names will
* exactly match the column names returned by your JDBC driver.
* Because different drivers might return column names in different
* cases, the property names seen by your application will vary
* depending on which JDBC driver you are using.</p>
*
* @param resultSet The result set to be wrapped
* @param lowerCase Should property names be lower cased?
*
* @exception NullPointerException if <code>resultSet</code>
*is <code>null</code>
* @exception SQLException if the metadata for this result set
*cannot be introspected
*/
public CustomRowSetDynaClass(ResultSet resultSet, boolean lowerCase)
throws SQLException {

this(resultSet, 1, -1, lowerCase);

}

public CustomRowSetDynaClass(
ResultSet resultSet,
int page,
int pageSize,
boolean lowerCase)
throws SQLException {

if (resultSet == null) {
throw new NullPointerException();
}
this.lowerCase = lowerCase;
this.page = page;
this.pageSize = pageSize;

introspect(resultSet);
copy(resultSet);

}

public CustomRowSetDynaClass(ResultSet resultSet, int page, int pageSize)
throws SQLException {
this(resultSet, page, pageSize, true);
}

// ----------------------------------------------------- Instance Variables

/**
* <p>Flag defining whether column names should be lower cased when
* converted to property names.</p>
*/
protected boolean lowerCase = true;

protected int page = 1;
protected int pageSize = -1;

/**
* <p>The set of dynamic properties that are part of this
* {@link DynaClass}.</p>
*/
protected DynaProperty properties[] = null;

/**
* <p>The set of dynamic properties that are part of this
* {@link DynaClass}, keyed by the property name.Individual descriptor
* instances will be the same instances as those in the
* <code>properties</code> list.</p>
*/
protected Map propertiesMap = new HashMap();

/**
* <p>The list of {@link DynaBean}s representing the contents of
* the original <code>ResultSet</code> on which this
* {@link RowSetDynaClass} was based.</p>
*/
protected List rows = new ArrayList();

// ------------------------------------------------------ DynaClass Methods

/**
* <p>Return the name of this DynaClass (analogous to the
* <code>getName()</code> method of <code>java.lang.Class</code), which
* allows the same <code>DynaClass</code> implementation class to support
* different dynamic classes, with different sets of properties.</p>
*/
public String getName() {

return (this.getClass().getName());

}

/**
* <p>Return a property descriptor for the specified property, if it
* exists; otherwise, return <code>null</code>.</p>
*
* @param name Name of the dynamic property for which a descriptor
*is requested
*
* @exception IllegalArgumentException if no property name is specified
*/
public DynaProperty getDynaProperty(String name) {

if (name == null) {
throw new IllegalArgumentException("No property name specified");
}
return ((DynaProperty) propertiesMap.get(name));

}

/**
* <p>Return an array of <code>ProperyDescriptors</code> for the properties
* currently defined in this DynaClass.If no properties are defined, a
* zero-length array will be returned.</p>
*/
public DynaProperty[] getDynaProperties() {

return (properties);

}

/**
* <p>Instantiate and return a new DynaBean instance, associated
* with this DynaClass.<strong>NOTE</strong> - This operation is not
* supported, and throws an exception.</p>
*
* @exception IllegalAccessException if the Class or the appropriate
*constructor is not accessible
* @exception InstantiationException if this Class represents an abstract
*class, an array class, a primitive type, or void; or if instantiation
*fails for some other reason
*/
public DynaBean newInstance()
throws IllegalAccessException, InstantiationException {

throw new UnsupportedOperationException("newInstance() not supported");

}

// --------------------------------------------------------- Public Methods

/**
* <p>Return a <code>List</code> containing the {@link DynaBean}s that
* represent the contents of each <code>Row</code> from the
* <code>ResultSet</code> that was the basis of this
* {@link RowSetDynaClass} instance.These {@link DynaBean}s are
* disconnected from the database itself, so there is no problem with
* modifying the contents of the list, or the values of the properties
* of these {@link DynaBean}s.However, it is the application's
* responsibility to persist any such changes back to the database,
* if it so desires.</p>
*/
public List getRows() {

return (this.rows);

}

// ------------------------------------------------------ Protected Methods

/**
* <p>Copy the column values for each row in the specified
* <code>ResultSet</code> into a newly created {@link DynaBean}, and add
* this bean to the list of {@link DynaBean}s that will later by
* returned by a call to <code>getRows()</code>.</p>
*
* @param resultSet The <code>ResultSet</code> whose data is to be
*copied
*
* @exception SQLException if an error is encountered copying the data
*/
protected void copy(ResultSet resultSet) throws SQLException {
int abs = 0;
int rowsCount = 0;
int currentPageRows = 0;
resultSet.last();
rowsCount = resultSet.getRow();
if (pageSize != -1) {
int totalPages = (int) Math.ceil(((double) rowsCount) / pageSize);
if (page > totalPages)
page = totalPages;
if (page < 1)
page = 1;
abs = (page - 1) * pageSize;

//currentPageRows=(page==totalPages?rowsCount-pageSize*(totalPages-1):pageSize);
} else
pageSize = rowsCount;
if (abs == 0)
resultSet.beforeFirst();
else
resultSet.absolute(abs);
//int
while (resultSet.next() && ++currentPageRows <= pageSize) {
DynaBean bean = new BasicDynaBean(this);
for (int i = 0; i < properties.length; i++) {
String name = properties[i].getName();
bean.set(name, resultSet.getObject(name));
}
rows.add(bean);
}

}

/**
* <p>Introspect the metadata associated with our result set, and populate
* the <code>properties</code> and <code>propertiesMap</code> instance
* variables.</p>
*
* @param resultSet The <code>resultSet</code> whose metadata is to
*be introspected
*
* @exception SQLException if an error is encountered processing the
*result set metadata
*/
protected void introspect(ResultSet resultSet) throws SQLException {

// Accumulate an ordered list of DynaProperties
ArrayList list = new ArrayList();
ResultSetMetaData metadata = resultSet.getMetaData();
int n = metadata.getColumnCount();
for (int i = 1; i <= n; i++) { // JDBC is one-relative!
DynaProperty dynaProperty = createDynaProperty(metadata, i);
if (dynaProperty != null) {
list.add(dynaProperty);
}
}

// Convert this list into the internal data structures we need
properties =
(DynaProperty[]) list.toArray(new DynaProperty[list.size()]);
for (int i = 0; i < properties.length; i++) {
propertiesMap.put(properties[i].getName(), properties[i]);
}

}

/**
* <p>Factory method to create a new DynaProperty for the given index
* into the result set metadata.</p>
*
* @param metadata is the result set metadata
* @param i is the column index in the metadata
* @return the newly created DynaProperty instance
*/
protected DynaProperty createDynaProperty(
ResultSetMetaData metadata,
int i)
throws SQLException {

String name = null;
if (lowerCase) {
name = metadata.getColumnName(i).toLowerCase();
} else {
name = metadata.getColumnName(i);
}
String className = null;
try {
className = metadata.getColumnClassName(i);
} catch (SQLException e) {
// this is a patch for HsqlDb to ignore exceptions
// thrown by its metadata implementation
}

// Default to Object type if no class name could be retrieved
// from the metadata
Class clazz = Object.class;
if (className != null) {
clazz = loadClass(className);
}
return new DynaProperty(name, clazz);

}

/**
* <p>Loads and returns the <code>Class</code> of the given name.
* By default, a load from the thread context class loader is attempted.
* If there is no such class loader, the class loader used to load this
* class will be utilized.</p>
*
* @exception SQLException if an exception was thrown trying to load
*the specified class
*/
protected Class loadClass(String className) throws SQLException {

try {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
cl = this.getClass().getClassLoader();
}
return (cl.loadClass(className));
} catch (Exception e) {
throw new SQLException(
"Cannot load column class '" + className + "': " + e);
}

}

}

大部分代碼從BeanUtils的源碼中取得,只做了簡單的修改,沒有加多余的注釋。如果要正式使用,
需要再做精加工。

========================================
關于這個包,只準備測試到這里了,不過已經有了大概的印象了,至少,知道這個包可以做些什么。
其實這個筆記也只是起到這個作用。@_@

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

在线看毛片视频-国产免费av在线-欧美日韩一区二区三区-国产成人无码av在线播放无广告-亚洲人va欧美va人人爽-国产第一草草-西班牙黄色片-四虎在线网站8848-最新av片免费网站入口-东京热无码中文字幕av专区-日本大人吃奶视频xxxx-欧美精品一区二区三区四区五区-国产片天天弄-国产免费内射又粗又爽密桃视频-欧美爱爱网站-日韩v欧美
  • <li id="86scu"><menu id="86scu"></menu></li>
    <li id="86scu"></li>
    <button id="86scu"></button>
  • <s id="86scu"></s><button id="86scu"><menu id="86scu"></menu></button>
  • 国产成人精品免费看在线播放| 大西瓜av在线| 九九热免费精品视频| 116极品美女午夜一级| 午夜精品久久久内射近拍高清 | 午夜dv内射一区二区| 狠狠97人人婷婷五月| 人妻少妇被粗大爽9797pw| 成人一级片网站| 少妇一级淫免费播放| 国产成人永久免费视频| 欧美a级黄色大片| 成人免费毛片网| 日本女优爱爱视频| 成人黄色片视频| 一二三四视频社区在线| 波多结衣在线观看| 最近中文字幕免费mv| 韩国中文字幕av| av免费观看国产| 蜜桃视频一区二区在线观看| 日本高清久久久| 国产妇女馒头高清泬20p多| 国产中文字幕在线免费观看| 亚洲第一页在线视频| 欧美成人xxxxx| 午夜探花在线观看| 亚洲一区在线不卡| 亚洲成熟丰满熟妇高潮xxxxx| 亚洲国产精品女人| av五月天在线| av磁力番号网| 国产乱淫av片杨贵妃| 午夜探花在线观看| 久草综合在线观看| 日本999视频| 亚洲欧美另类动漫| 少妇一晚三次一区二区三区| 中文久久久久久| av网站在线观看不卡| 亚洲欧美一二三| 欧妇女乱妇女乱视频| 青娱乐精品在线| 性欧美18一19内谢| 老司机午夜性大片| 手机看片一级片| 激情 小说 亚洲 图片: 伦| 国产一区二区三区小说| www.国产在线视频| 亚洲综合20p| 伊人成人免费视频| 亚洲理论中文字幕| 国产精品少妇在线视频| 18禁裸男晨勃露j毛免费观看| www.超碰com| 国产精品区在线| 久久九九国产视频| 北条麻妃在线一区| 99热这里只有精品7| 男人操女人免费| 国产对白在线播放| www.爱色av.com| www.精品在线| 免费观看国产精品视频| 中国丰满熟妇xxxx性| 性欧美极品xxxx欧美一区二区| www.99av.com| 美女扒开大腿让男人桶| 手机看片福利日韩| av免费播放网址| 欧美日韩在线不卡视频| 色婷婷一区二区三区在线观看| 91国在线高清视频| 少妇人妻大乳在线视频| 99sesese| 日韩avxxx| 日本免费一级视频| japanese在线播放| 日本熟妇人妻中出| 九一精品在线观看| 一二三四中文字幕| 午夜免费一区二区| 97av中文字幕| 国产精品亚洲αv天堂无码| 国产免费人做人爱午夜视频| 天堂а√在线中文在线| 日韩a级黄色片| 超碰91在线播放| 3d动漫一区二区三区| 欧美一级特黄a| 女女百合国产免费网站| 中文字幕亚洲乱码| 99精品视频国产| 91蝌蚪视频在线观看| www.中文字幕在线| 久久久精品视频国产| 免费观看美女裸体网站| 天天av天天操| 无码日韩人妻精品久久蜜桃| 99在线精品免费视频 | aⅴ在线免费观看| 屁屁影院ccyy国产第一页| 免费特级黄色片| 日本三日本三级少妇三级66| 成年人小视频网站| 国产日产欧美视频| 8x8ⅹ国产精品一区二区二区| 日韩精品视频一区二区在线观看| 亚洲理论中文字幕| 中文字幕制服丝袜在线| 簧片在线免费看| 黄频视频在线观看| 天堂中文av在线| 91免费视频网站在线观看| 97碰在线视频| 欧美日韩视频免费| 亚洲熟妇无码一区二区三区| 午夜免费高清视频| 日韩一级在线免费观看| www插插插无码免费视频网站| 亚洲精品偷拍视频| 日韩国产成人无码av毛片| 在线观看污视频| 欧美一级免费播放| www.激情网| 美女黄色片网站| 男人的天堂狠狠干| 少妇高潮喷水在线观看| 欧美日韩二三区| 精品少妇人妻av免费久久洗澡| 国模无码视频一区二区三区| 男人天堂999| 波多野结衣之无限发射| 91制片厂免费观看| 精品久久久久久无码国产| 免费观看国产精品视频| 妞干网在线免费视频| 无码播放一区二区三区| 国产a视频免费观看| 国产精品人人妻人人爽人人牛| 99久久久无码国产精品6| 久久久久久三级| 91av在线免费播放| 能看的毛片网站| 中文字幕精品在线播放| 日本欧美黄色片| 国产极品尤物在线| 五月天色婷婷综合| 日日摸天天爽天天爽视频| 熟女少妇精品一区二区| 成人久久久久久久久| 中文字幕有码av| 国产精品jizz在线观看老狼| 成年人免费大片| 在线视频一二区| wwwwxxxx日韩| 在线观看岛国av| 搡女人真爽免费午夜网站| 日本香蕉视频在线观看| 97在线播放视频| 国产免费又粗又猛又爽| 狠狠干狠狠操视频| 超碰人人爱人人| 亚洲精品高清无码视频| 国产天堂在线播放| www.av91| 国产精品网站免费| 老太脱裤子让老头玩xxxxx| 日本阿v视频在线观看| 国产精品入口免费软件| 青青草免费在线视频观看| ijzzijzzij亚洲大全| 欧美国产在线一区| 91蝌蚪视频在线观看| 国产福利在线免费| www.污污视频| www.99av.com| 樱花草www在线| 啊啊啊国产视频| 日日摸日日碰夜夜爽无码| 黄色网在线视频| 超碰成人在线免费观看| 肉大捧一出免费观看网站在线播放| 日本久久久精品视频| 被灌满精子的波多野结衣| 亚洲天堂国产视频| 久久这里只有精品18| 国产精品久久久久9999小说| 丰满人妻一区二区三区53号| www.好吊操| 91精品91久久久中77777老牛| 国产传媒久久久| 亚洲老女人av| 欧美 日韩精品| 男人添女人下部视频免费| 99国产精品白浆在线观看免费| 中文字幕永久有效| 狠狠操狠狠干视频| 久久天天东北熟女毛茸茸| 国产精品又粗又长|