????????????????????????????? 保存ResultSet中的數(shù)據(jù)
??????????????????????????????????????????? 馬嘉楠??????? 2006.8.26
當(dāng)我們?cè)诰帉?xiě)程序的時(shí)候,免不了要與數(shù)據(jù)庫(kù)打交道,相信ResultSet大家也并不陌生,從數(shù)據(jù)庫(kù)讀取的數(shù)據(jù)將會(huì)存入其中。
操作結(jié)束關(guān)閉數(shù)據(jù)庫(kù)連接以及ResultSet,否則保持?jǐn)?shù)據(jù)庫(kù)的連接并直接對(duì)ResultSet中的數(shù)據(jù)進(jìn)行操作,會(huì)使效率低下,同時(shí)很可能出現(xiàn)錯(cuò)誤。
此時(shí)我們就需要在關(guān)閉結(jié)果集ResultSet之前,保存其中我們需要的數(shù)據(jù)。
我這里介紹的方法是將ResultSet中的數(shù)據(jù)存儲(chǔ)到一個(gè)VO對(duì)象當(dāng)中,ResultSet中每一個(gè)記錄對(duì)應(yīng)一個(gè)VO(VO就是數(shù)據(jù)庫(kù)中表的映射),之后再把這些VO存儲(chǔ)到ArrayList當(dāng)中,返回List方便其他方法處理。
希望能給初學(xué)者以幫助,同時(shí)請(qǐng)高手不吝賜教,有沒(méi)有更好的處理方法,或者我的代碼由不合理的地方,也請(qǐng)您提出寶貴意見(jiàn)!共同學(xué)習(xí),共同進(jìn)步!
代碼如下(我把實(shí)際代碼簡(jiǎn)化了一下,BlogJavaVO中的一些函數(shù)在這里沒(méi)有調(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ù)庫(kù)中表的映射
Vo代碼如下:
public class BlogJavaVO implements VO {
?private String url = "";?
?private String name = "";
?private String date = "";?//數(shù)據(jù)庫(kù)表BlogJava中的屬性設(shè)置為時(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ù)對(duì)象接口,對(duì)于每個(gè)表值對(duì)象, 相應(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)鍵字為對(duì)應(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();
?// 獲取對(duì)應(yīng)的表名, 使用代碼生產(chǎn)器生成
?String getTableName();
}
馬嘉楠
jianan.ma@gmail.com