隨筆-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 閱讀(157) 評論(0)  編輯  收藏 所屬分類: 實踐

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


          網站導航:
           
          主站蜘蛛池模板: 连南| 东山县| 三明市| 饶河县| 革吉县| 博爱县| 同仁县| 虎林市| 常山县| 荣成市| 武夷山市| 慈溪市| 武安市| 永仁县| 始兴县| 日照市| 饶阳县| 高阳县| 勐海县| 鸡东县| 台南县| 隆尧县| 汉寿县| 南和县| 新蔡县| 丰原市| 从化市| 西和县| 静安区| 醴陵市| 青海省| 武功县| 太仆寺旗| 洪泽县| 信阳市| 吉安市| 浦江县| 无锡市| 苏尼特左旗| 凤山县| 昌乐县|