Prodigy/kf智
          我的博客
          posts - 20,comments - 7,trackbacks - 0

          package dao_sql;

          import java.sql.Connection;
          import java.sql.ResultSet;
          import java.sql.ResultSetMetaData;
          import java.sql.SQLException;
          import java.sql.Statement;
          import java.util.HashMap;


          public class WebBean
          {
           protected Connection conn = null;

           protected void free(Statement stmt)
           {
            try {
             if (stmt != null)
             {
              stmt.close();
             }
            }
            catch (SQLException e)
            {
             println(e);
            }
            freeConn();
           }

           /**
            * 此處插入方法說明。 創建日期:(2004-3-16 17:01:55)
            */
           protected void freeConn()
           {
            try {
             if (conn != null)
             {
              conn.close();
             }
            }
            catch (SQLException e)
            {
             
             println(e);
            }
           }

           private String gb2u(String s)
           {
            try {
             String sUnicode = new String(s.getBytes("8859_1"), "gb2312");
             s = "";
             for (int i = 0; i < sUnicode.length(); i++)
             {
              if (sUnicode.charAt(i) < 255)
              {
               s = s + sUnicode.charAt(i);
              }
              else
              {
              String sHex = Integer.toHexString(sUnicode.charAt(i))
                .toUpperCase();
              if (sHex.length() < 4)
               sHex = "0000".substring(0, 4 - sHex.length()) + sHex;
              s += "&#x" + sHex + ";";
              }
             }
            } catch (Exception exception) {
            }
            return s;
           }
          //連接數據庫 這個很重要 這個方里 它是重要地方之一 這里需要你傳遞JNDI的名字
           protected boolean getConnection() throws Exception
           {
            return getConnection("bbs");
           }

           protected HashMap getInfo(ResultSet rs) throws SQLException
           {  
            return getInfo(rs, null);
           }
           protected HashMap getInfo(ResultSet rs, HashMap hm) throws SQLException
           {
            ResultSetMetaData rsmd = rs.getMetaData();
            if (rs.next())
            {
             
             if (hm == null)
             {
              hm = new HashMap();
             }
             for (int i = 1; i <= rsmd.getColumnCount(); i++)
             {
              String fieldname = rsmd.getColumnName(i);

              String value = rs.getString(fieldname);
              fieldname = fieldname.toLowerCase();

              hm.put(fieldname, encoding(fieldname, value));
             }
            }
            rs.close();
            return hm;
           }
           //得到結果集 重要之二
           protected HashMap getRS(ResultSet rs) throws SQLException
           {

            HashMap hm = new HashMap();
            int count = 0;
            String[] fieldsname = null ; //新加的 表示是數據庫的字段名字
            ResultSetMetaData rsmd = rs.getMetaData();
            fieldsname = new String[rsmd.getColumnCount()];
            while (rs.next())
            {
             //聲明一個HashMap 對象
             HashMap rec = new HashMap();

             for (int i = 1; i <= rsmd.getColumnCount(); i++) {
              String fieldname = rsmd.getColumnName(i);
             
              String value = rs.getString(fieldname);
              
              fieldname = fieldname.toLowerCase();

              rec.put(fieldname, encoding(fieldname, value));
              
             }
             //把HashMap 又放在了一個HashMap 里 這里的建值0,1,2。。。。存放了rec
             hm.put("" + count, rec);
             count++;
            }
            
            
            //把數據庫的字段名字放到HashMap表里
            for (int i = 1; i <= rsmd.getColumnCount(); i++)
            {
              fieldsname[i-1] = rsmd.getColumnName(i);
            }
               hm.put("fields",fieldsname);
              
              
              
            rs.close();
             //這里的count是記錄一共有多少個HashMap 用固定的count這個建值來表示
            hm.put("count", new Integer(count));
            return hm;
           }
          //重要之三  根據結果集得到 數據庫里有多少條記錄
           protected  int getCount(ResultSet rs) throws SQLException
           {
            int count = 0 ;
            while (rs.next())
            {
             count = rs.getInt(1) ;
            }
            rs.close() ;
            return count ;
           }

           /**
            * 此處插入方法說明。 創建日期:(2004-3-15 15:28:32)
            *
            * @param obj
            *            java.lang.Object
            */
           protected void println(Exception ex)
           {
            ex.printStackTrace(System.out);
            System.out.println(ex);
           }

           private String encoding(String fieldname, String value)
           {
            if ((fieldname.indexOf("name") != -1 || fieldname.indexOf("note") != -1 || fieldname.indexOf("title") != -1)&& !fieldname.equals("servicename")
              && !fieldname.equals("businame")
              && !fieldname.equals("filename")
              && !fieldname.equals("mobname"))
            {
             return value;
            }
            return value;
           }

           protected boolean getConnection(String dbname) throws Exception
           {
            if (conn == null || conn.isClosed())
            {
             conn = Dao_Sql.getConnection(dbname);
            }
            if (conn == null || conn.isClosed())
            {
             return false;
            }
            return true;
           }
          }

          posted on 2006-03-29 15:39 xiaozhi 閱讀(337) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 麻阳| 伊宁市| 游戏| 兴义市| 泾川县| 临城县| 灵寿县| 泌阳县| 邯郸县| 铜陵市| 金昌市| 营口市| 治多县| 金门县| 田东县| 紫阳县| 台南县| 南城县| 冷水江市| 万安县| 浮梁县| 颍上县| 海林市| 宾川县| 东乌珠穆沁旗| 临澧县| 库车县| 会同县| 凯里市| 福州市| 廊坊市| 静海县| 安化县| 沭阳县| 隆子县| 甘洛县| 大冶市| 景谷| 宁乡县| 盐城市| 探索|