⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pageiteratorinteger.java

📁 Java/J2EE框架Jdon-Framework系统的Sample
💻 JAVA
字号:
/**
 * Copyright 2005 Jdon.com
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */

package com.jdon.model.query;

import java.util.*;
import javax.sql.*;
import java.sql.*;

import com.jdon.controller.model.PageIterator;
import org.apache.log4j.Logger;
import com.jdon.util.DbUtil;

public class PageIteratorInteger extends PageIteratorJDBC {

  private final static Logger logger = Logger.getLogger(PageIteratorInteger.class);

  private DataSource dataSource;

  public PageIteratorInteger(DataSource dataSource) {
    this.dataSource = dataSource;

  }

  /**
   * 获得key下的所有数据总数
   *
   * 适合整数型的数据记录
   * sqlquery 示例如下:
   *
   *  private final static String GET_PRODUCTS_ALLCOUNT =
      "select count(1) from product a where a.catId = ?";
   * @param key
   * @param sqlquery
   * @return
   * @throws java.lang.Exception
   */
  public int fetchDataAllCount(Object key, String sqlquery) throws
      Exception {
    Connection c = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    int ret = 0;
    try {
      c = dataSource.getConnection();
      ps = c.prepareStatement(sqlquery,
                              ResultSet.TYPE_SCROLL_INSENSITIVE,
                              ResultSet.CONCUR_READ_ONLY);

      setSqlKey(key, ps);
      rs = ps.executeQuery();
      if (rs.first()) {
        ret = rs.getInt(1);
      }
    } catch (SQLException se) {
      throw new Exception("SQLException: " + se.getMessage());
    } finally {
      if (rs != null)
        rs.close();
      if (ps != null)
        ps.close();
      if (c != null)
        c.close();
    }
    logger.debug("--> fetchDataAllCount is" + ret);
    return ret;

  }

  /**
   * 获得key下的所有数据的ID集合
   *
   * 适合整数型的数据记录
   *
   * sqkquery示例如下:
   *
   *  "select a.productId from product a where a.catId = ?";
   *
   * @param key
   * @param sqlquery
   * @param start
   * @param count
   * @return 包含Integer的PageIterator
   * @throws java.lang.Exception
   */
  public PageIterator fetchDatas(Object key, String sqlquery, int start,
                               int count) throws
      Exception {
    logger.debug("--> fetchDatas from start=" + start + " size=" + count);
    Connection c = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    PageIterator pageIterator = null;
    boolean hasNext = false;
    try {
      c = dataSource.getConnection();

      DbUtil.testConnection(c);
      ps = c.prepareStatement(sqlquery,
                              ResultSet.TYPE_SCROLL_INSENSITIVE,
                              ResultSet.CONCUR_READ_ONLY);

      setSqlKey(key, ps);

      rs = ps.executeQuery();
      if (DbUtil.supportsFetchSize)
        rs.setFetchSize(count);
      if (start >= 0 && rs.absolute(start + 1)) {
        List items = new ArrayList(count);
        do {
          items.add(new Integer(rs.getInt(1)));
        }
        while ( (hasNext = rs.next()) && (--count > 0));
        Integer[] itemsA = (Integer[]) items.toArray(new Integer[0]);
        pageIterator = new PageIterator(itemsA, start, hasNext);
      } else
        pageIterator = new PageIterator(PageIterator.EMPTY, start, hasNext);
      logger.debug("--> getProducts succefully ..");
    } catch (SQLException se) {
      logger.error(se);
      throw new Exception("SQLException: " + se.getMessage());
    } finally {
      if (rs != null)
        rs.close();
      if (ps != null)
        ps.close();
      if (c != null)
        c.close();
    }
    return pageIterator;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -