posts - 39,  comments - 44,  trackbacks - 0
          封裝數(shù)據(jù)庫操作,目的就是為了隱藏java.sql包內(nèi)的類,在編碼中去掉核心的數(shù)據(jù)庫操作代碼。以杜絕直接數(shù)據(jù)庫操作容易帶來的資源未釋放問題。同時也減少了數(shù)據(jù)庫操作的編碼量。

            但是很多網(wǎng)友在封裝時,卻喜歡返回結(jié)果集(ResultSet對象),那么這個封裝就沒有意義了。

            1. 又是直接操作核心數(shù)據(jù)庫類,跟封裝前幾乎沒什么變化。

            2. 結(jié)果集總是依賴于它使用的連接(Connection)對象。因此當(dāng)連接對象在方法內(nèi)被關(guān)閉后,你返回的ResultSet就沒有用了。

            如果真的要獲得查詢數(shù)據(jù)庫的結(jié)果集,就把結(jié)果集對象內(nèi)的所有數(shù)據(jù),轉(zhuǎn)儲到以Map為元素的List對象內(nèi)。

            當(dāng)然,這種方式,不能適應(yīng)大數(shù)據(jù)量的查詢,不過如果真的碰到大數(shù)據(jù)量的查詢,那用什么封裝都不好,還是得直接數(shù)據(jù)庫操作. :)))

            下面是簡單的數(shù)據(jù)庫操作Javabean的代碼

            DbWrapper.java

            import java.sql.*;

            import java.util.*;

            public class DbWrapper

            {

            // 定義連接池對象為靜態(tài)變量,將一直存在,直到工作目錄關(guān)閉。

            private static DataSource ds = null;

            // 1.用連接池的方式獲得連接

            // 如果不是做多數(shù)據(jù)庫程序,推薦使用此方法

            // 相關(guān)內(nèi)容:在tomcat管理界面配置連接池

            public static Connection openConnection() throws Exception

            {

            // 只需要初始化1次

            if ( ds == null )

            {

            Context initContext = new InitialContext();

            Context envContext = (Context) initContext.lookup("java:/comp/env");

            DataSource ds = (DataSource) envContext.lookup("jdbc/MyDataSource");

            }

            return ds.getConnection();

            }

            // 2.用jdbc驅(qū)動獲得連接

            // 相關(guān)內(nèi)容:JSP數(shù)據(jù)庫連接大全

            public static Connection openConnection(

            String driver,

            String url,

            String username,

            String password)

            throws Exception

            {

            Class.forName(driver).newInstance();

            return DriverManager.getConnection(url, username, password);

            }

            public static void closeConnection(Connection conn) throws Exception

            {

            if ( conn != null )

            {

            conn.close();

            }

            }

            public static int executeUpdate(String sql) throws Exception

            {

            int count = 0;

            Connection conn = null;

            Statement stmt = null;

            try

            {

            conn = openConnection();

            stmt = conn.createStatement();

            count = stmt.executeUpdate(sql);

            }

            catch ( Exception e )

            {

            throw e;

            }

            finally

            {

            closeConnection(conn);

            }

            return count;

            }

            public static List executeQuery(String sql) throws Exception

            {

            List list = new ArrayList();

            Connection conn = null;

            Statement stmt = null;

            ResultSet rs = null;

            try

            {

            conn = openConnection();

            stmt = conn.createStatement();

            rs = stmt.executeQuery(sql);

            ResultSetMetaData rsmd = rs.getMetaData();

            while ( rs.next() )

            {

            Map map = new HashMap();

            for ( int i = 1; i < = rsmd.getColumnCount(); i++ )

            {

            map.put(rsmd.getColumnName(i), rs.getObject(i));

            }

            list.add(map);

            } }

            catch ( Exception e )

            {

            e.printStackTrace();

            }

            finally

            {

            if ( rs != null ) rs.close();

            closeConnection(conn);

            }

            return list; }

            }

            使用示例:

            // 1.對于insert, update, delete語句int count = DbWrapper.executeUpdate(sql);

            // 2.對于selete語句

            java.util.List list = DbWrapper.executeQuery(sql);

            // 方法一:按名字取值,注意大小寫是嚴(yán)格區(qū)分的

            for ( int i = 0; i < list.size(); i++ )

            {

            java.util.Map map = (java.util.Map)list.get(i);

            out.println(mag.get("column_name").toString());

            }

            // 方法二:遍歷取值

            for ( int i = 0; i < list.size(); i++ )

            {

            java.util.Map map = (java.util.Map)list.get(i);

            for (java.util.Iterator it = map.keySet().iterator(); it.hasNext();)

            {

            String column_name = it.next().toString()); // 取值時注意null判斷

            out.println(column_name + " = " + map.get(column_name) == null ? "" : map.get(column_name).toString());

            }

            }

          posted on 2008-05-07 18:52 礦礦 閱讀(144) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 拉萨市| 咸阳市| 昌平区| 北辰区| 雷山县| 台安县| 嵊州市| 仪陇县| 尖扎县| 本溪| 镇沅| 乐都县| 库伦旗| 庄河市| 宜兰县| 定西市| 遂宁市| 伊川县| 太原市| 柏乡县| 香港 | 瑞安市| 白河县| 天柱县| 那坡县| 宁波市| 庄浪县| 始兴县| 辛集市| 英山县| 台东县| 太和县| 达孜县| 昔阳县| 新昌县| 信阳市| 仪陇县| 平乐县| 屯昌县| 浏阳市| 自贡市|