blockqueryjdbctemp.java

来自「用jbuilder写的源程序」· Java 代码 · 共 157 行

JAVA
157
字号
/*
 * Copyright 2003-2006 the original author or authors.
 * 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.block;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.log4j.Logger;

import com.jdon.model.query.JdbcUtil;
import com.jdon.model.query.cache.QueryConditonDatakey;
import com.jdon.util.DbUtil;

/**
 * Batch query JDBC Template
 * 
 * this class can be directly by application system.
 * 
 * @author <a href="mailto:banqiao@jdon.com">banq </a>
 *  
 */
public class BlockQueryJDBCTemp implements BlockQueryJDBC {
    private final static Logger logger = Logger.getLogger(BlockQueryJDBCTemp.class);

    private DataSource dataSource;

    private JdbcUtil jdbcUtil;

    public BlockQueryJDBCTemp(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcUtil = new JdbcUtil();
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.jdon.model.query.PageIteratorJDBC#fetchDataAllCount(java.lang.Object,
     *      java.lang.String)
     */
    public int fetchDataAllCount(QueryConditonDatakey qcd) {
        logger.debug("[JdonFramework]--> execute fetch all count for sql sentence: " + qcd.getSqlquery());
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int ret = 0;
        try {
            c = dataSource.getConnection();
            ps = c.prepareStatement(qcd.getSqlquery(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

            jdbcUtil.setQueryParams(qcd.getQueryParams(), ps);

            rs = ps.executeQuery();
            if (rs.first()) {
                ret = rs.getInt(1);
            }
        } catch (SQLException se) {
            logger.error(se);
        } catch (Exception ex) {
            logger.error(ex);
        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (ps != null)
                    ps.close();
                if (c != null)
                    c.close();
            } catch (Exception ex) {
            }
        }
        logger.debug("[JdonFramework]--> fetchDataAllCount is" + ret);
        return ret;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.jdon.model.query.PageIteratorJDBC#fetchDatas(java.util.Collection,
     *      java.lang.String, int, int)
     */
    public List fetchDatas(QueryConditonDatakey qcdk) {
        logger.debug("[JdonFramework]--> fetch the primary key collection, sql sentence: " + qcdk.getSQlKey());
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        int blockStart = qcdk.getBlockStart();
        int blockSize = qcdk.getBlockSize();
        logger.debug("[JdonFramework]--> blockStart=" + blockStart + " blockSize=" + blockSize);
        List items = new ArrayList(blockSize);
        try {
            c = dataSource.getConnection();

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

            jdbcUtil.setQueryParams(qcdk.getQueryParams(), ps);

            rs = ps.executeQuery();
            if (DbUtil.supportsFetchSize)
                rs.setFetchSize(blockSize);
            // Many JDBC drivers don't implement scrollable cursors the real
            // way, but instead load all results into memory. Looping through
            // the results ourselves is more efficient.
            for (int i = 0; i < blockStart; i++) {
                if (!rs.next()) break;
            }
            blockSize ++;
            while(rs.next() && (--blockSize > 0)) {
                Object result = rs.getObject(1);
                logger.debug("[JdonFramework]--> found a primary key = " + result + ",  type:" + result.getClass().getName());
                items.add(result);
            }

            logger.debug("[JdonFramework]--> get a result succefully ..");
        } catch (SQLException se) {
            logger.error(se);

        } catch (Exception ex) {
            logger.error(ex);

        } finally {
            try {
                if (rs != null)
                    rs.close();
                if (ps != null)
                    ps.close();
                if (c != null)
                    c.close();
            } catch (Exception ex) {
            }
        }
        return items;

    }

}

⌨️ 快捷键说明

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