隨筆-1  評論-1  文章-2  trackbacks-0

                簡介:該框架吸取IBatis設計思想和方法映射機制,結合模式匹配,將查詢結果直接映射成域對象模型,并支持分頁。
                代碼如下:
          package heroking.db;

          import java.util.List;
          import java.util.ArrayList;
          import java.util.regex.Pattern;
          import java.util.regex.Matcher;
          import java.sql.*;


          public class GeneralDao {
              private Connection connection = null;

              public Connection getConnection() {
                  return connection;
              }

              public void setConnection(Connection connection) {
                  this.connection = connection;
              }

              /**
               * 執行插入,更新,刪除操作,返回值為影響記錄數。
               * @param sql
               * @param parameter
               */
              public int executeUpdate(String sql, Object parameter) {
                  List result = new ArrayList();
                  //按模式匹配轉換sql語句
                  Pattern pattern = Pattern.compile("#[a-zA-Z]+#");   //模式匹配
                  Matcher matcher = pattern.matcher(sql);
                  String formatSql = matcher.replaceAll("?");
                  //連接數據源
                  PreparedStatement st = null;
                  try {
                      st = connection.prepareStatement(formatSql);
                  } catch (Exception e) {
                      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                  }
                  //添加預處理參數
                  int i = 1;
                  while (matcher.find()) {
                      String field = matcher.group();
                      String formatField = field.substring(1, field.length() - 1);
                      try {
                          st.setObject(i++, DataBaseUtil.excuteGetMethod(formatField, parameter));
                      } catch (SQLException e) {
                          e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                      }
                  }
                  try {
                      return st.executeUpdate();
                  } catch (SQLException e) {
                      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                  }
                  return 0;
              }
             

              /**
               * 查詢第pageCount頁面(每個頁面的記錄數為pageSize)的結果
               * @param sql   sql查詢
               * @param parameter 參數對象
               * @param pageSize 頁面結果集大小
               * @param pageCount 頁面數
               * @param resultClass 結果類
               * @return List
               */
              public List executeQuerys(String sql, Object parameter, int pageSize, int pageCount, Class resultClass){
                  List result = new ArrayList();
                  //按模式匹配轉換sql語句
                  Pattern pattern = Pattern.compile("#[a-zA-Z]+#");   //模式匹配
                  Matcher matcher = pattern.matcher(sql);
                  String formatSql = matcher.replaceAll("?");
                  //連接數據源
                  PreparedStatement st = null;
                  try {
                      //設置預處理狀態參數,滾動,只讀。
                      st = connection.prepareStatement(formatSql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
                  } catch (Exception e) {
                      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                  }
                  //添加預處理參數
                  int i = 1;
                  while (matcher.find()) {
                      String field = matcher.group();
                      String formatField = field.substring(1, field.length() - 1);
                      try {
                          st.setObject(i++, DataBaseUtil.excuteGetMethod(formatField, parameter));
                      } catch (SQLException e) {
                          e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                      }
                  }
                  ResultSet rs = null;
                  try {
                      rs = st.executeQuery();
                      //獲取總記錄數
                      rs.last();
                      int totalCount = rs.getRow();

                      //當前頁面第一條記錄位置
                      int curPagePosition = (pageCount -1)*pageSize + 1;
                      if (totalCount < curPagePosition) {
                          return null;
                      }
                      rs.beforeFirst();
                      rs.absolute(curPagePosition);

                      //封裝值對象
                      int k = 0;
                      ResultSetMetaData rsmd = rs.getMetaData();
                      int cols = rsmd.getColumnCount();
                      while (rs.next() && k < pageSize) {
                          Object o = resultClass.newInstance();
                          for (int j = 1; j <= cols; j++) {
                              String name = rsmd.getColumnName(j);
                              Object value = rs.getObject(j);//作通用類型處理,這樣row中的類型都是Object型的。
                              String voName = DataBaseUtil.toInValueName(name);
                              DataBaseUtil.executeSetMethod(voName, value, o);
                          }
                          result.add(o);
                          k++;    //獲取數加1
                      }
                  } catch (SQLException e) {
                      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                  } catch (IllegalAccessException e) {
                      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                  } catch (InstantiationException e) {
                      e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                  }
                  return result;
              }
              /**
               * 查詢所有紀錄
               * @param sql
               * @param parameter
               * @param resultClass
               * @return List
               */
              public List executeQuerys(String sql, Object parameter, Class resultClass){
                  //默認為所有記錄
                  return this.executeQuerys(sql, parameter, Integer.MAX_VALUE, 1, resultClass);
              }

              /**
               * 查詢單個記錄
               * @param sql
               * @param parameter
               * @param resultClass
               * @return Object
               */
              public Object executeQuery(String sql, Object parameter, Class resultClass){
                  List list = executeQuerys(sql, parameter,resultClass);
                  if (list == null || list.size() == 0) {
                      return null;
                  }
                  return list.get(0);
              }
          }

          posted on 2006-01-04 11:15 Hero King 閱讀(158) 評論(0)  編輯  收藏 所屬分類: 實踐

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


          網站導航:
           
          主站蜘蛛池模板: 娄烦县| 汝城县| 精河县| 东港市| 灵石县| 金沙县| 建德市| 忻城县| 台湾省| 永修县| 洛南县| 海淀区| 临清市| 安丘市| 博罗县| 兴山县| 成都市| 临武县| 肥东县| 涿州市| 广西| 易门县| 沅江市| 扶余县| 古交市| 清镇市| 交口县| 永川市| 通州区| 霍山县| 麦盖提县| 札达县| 公安县| 平利县| 焉耆| 姚安县| 正蓝旗| 南靖县| 凌云县| 万源市| 攀枝花市|