????????????????????????????? 保存ResultSet中的數(shù)據(jù)
??????????????????????????????????????????? 馬嘉楠??????? 2006.8.26
當(dāng)我們在編寫程序的時候,免不了要與數(shù)據(jù)庫打交道,相信ResultSet大家也并不陌生,從數(shù)據(jù)庫讀取的數(shù)據(jù)將會存入其中。
操作結(jié)束關(guān)閉數(shù)據(jù)庫連接以及ResultSet,否則保持?jǐn)?shù)據(jù)庫的連接并直接對ResultSet中的數(shù)據(jù)進(jìn)行操作,會使效率低下,同時很可能出現(xiàn)錯誤。
此時我們就需要在關(guān)閉結(jié)果集ResultSet之前,保存其中我們需要的數(shù)據(jù)。
我這里介紹的方法是將ResultSet中的數(shù)據(jù)存儲到一個VO對象當(dāng)中,ResultSet中每一個記錄對應(yīng)一個VO(VO就是數(shù)據(jù)庫中表的映射),之后再把這些VO存儲到ArrayList當(dāng)中,返回List方便其他方法處理。
希望能給初學(xué)者以幫助,同時請高手不吝賜教,有沒有更好的處理方法,或者我的代碼由不合理的地方,也請您提出寶貴意見!共同學(xué)習(xí),共同進(jìn)步!
代碼如下(我把實際代碼簡化了一下,BlogJavaVO中的一些函數(shù)在這里沒有調(diào)用):
protected ArrayList fetchMultiResults(ResultSet rs) throws SQLException {
??? ArrayList resultList = new ArrayList();
????while (rs.next()) {
??????? BlogJavaVO vo = new BlogJavaVO();
??????? populateData( vo, rs);
??????? resultList.add( vo );
??? }
??? return resultList;
}
?
?protected void populateData(BlogJavaVO vo, ResultSet rs) throws SQLException {
????? vo.setUrl(DAOUtils.trimStr(rs.getString("url")));
????? vo.setName(DAOUtils.trimStr(rs.getString("name")));
????? vo.setDate(DAOUtils.getFormatedDate(rs.getDate("date"));
?}
public class DAOUtils {
??????? ... ...
?/**
? * 提供刪除字符串前后的空格的功能
? * @param str
? * @return
? */
?public static String trimStr(String str) {
???? if (null == str)
???????? return "";
???? else
???????? return str.trim();
?}
??????? ... ...
}
VO是數(shù)據(jù)庫中表的映射
Vo代碼如下:
public class BlogJavaVO implements VO {
?private String url = "";?
?private String name = "";
?private String date = "";?//數(shù)據(jù)庫表BlogJava中的屬性設(shè)置為時間
?public BlogJavaVO() {}
?public BlogJavaVO( String purl, String pname, String pdate ) {
???? this.url = purl;
??? ?this.name = pname;
??? ?this.date = pdate;
?}
?public String getUrl() {
???? return url;
?}
?public String getName() {
?? ??return name;
?}
?public String getDate() {
??? ?return date;
?}
?public void setUrl(String purl) {
??? ?this.url = purl;
?}
?public void setName(String pname) {
??? ?this.name = pname;
?}
?public void setDate(String pdate) {
??? ?this.date = pdate;
?}
?public HashMap unloadToHashMap() {
??? ?HashMap hashMap = new HashMap();
?? ??hashMap.put("URL",this.url);
??? ?hashMap.put("NAME",this.name);
?? ??hashMap.put("DATE",this.date);
??? ?return hashMap;
?}
?public void loadFromHashMap(HashMap hashMap) {
???? if (hashMap != null) {
??????? ?this.url = (String) hashMap.get("URL");
??????? ?this.name = (String) hashMap.get("NAME");
??????? ?this.date = (String) hashMap.get("DATE");
??? }
?}
?public List getKeyFields() {
???? ArrayList arrayList = new ArrayList();
??? ?arrayList.add("url");
?? ??return arrayList;
?}
?public String getTableName() {
??? ?return "BlogJava";
?}
}
?
?
import java.io.Serializable;
import java.util.*;
/**
?* @Classname : VO
?* @Description : 公共數(shù)據(jù)對象接口,對于每個表值對象, 相應(yīng)的接口函數(shù)使用代碼生成器進(jìn)行生產(chǎn)
?* @Copyright
?* @Author :
?* @Create Date :
?*
?* @Last Modified :
?* @Modified by :
?* @Version : 1.0
?*/
public interface VO extends Serializable {
?// 把數(shù)據(jù)導(dǎo)出到HashMap中, HashMap的關(guān)鍵字為對應(yīng)的表字段名字
?// 使用代碼生產(chǎn)器生產(chǎn)
?public HashMap unloadToHashMap();
?// 從HashMap中讀取數(shù)據(jù), 使用代碼生產(chǎn)器生成
?public void loadFromHashMap(HashMap map);
?// 獲取關(guān)鍵字列表, 使用代碼生產(chǎn)器生成
?public List getKeyFields();
?// 獲取對應(yīng)的表名, 使用代碼生產(chǎn)器生成
?String getTableName();
}
馬嘉楠
jianan.ma@gmail.com