pageiteratorjdbctemp.java
来自「一个非常好的FRAMWRK!是一个外国组织做的!不!」· Java 代码 · 共 154 行
JAVA
154 行
/*
* Copyright 2003-2005 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;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import com.jdon.controller.model.PageIterator;
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 PageIteratorJDBCTemp implements PageIteratorJDBC {
private final static Logger logger = Logger.getLogger(PageIteratorJDBCTemp.class);
private DataSource dataSource;
private JdbcUtil jdbcUtil;
public PageIteratorJDBCTemp(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(Collection queryParams, String sqlquery)
throws Exception {
logger.debug("--> enter fetchDataAllCount ");
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);
jdbcUtil.setQueryParams(queryParams, ps);
rs = ps.executeQuery();
if (rs.first()) {
ret = rs.getInt(1);
}
} catch (SQLException se) {
throw new Exception("SQLException: " + se.getMessage());
} catch (Exception ex) {
logger.error(ex);
throw new Exception(ex);
} finally {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (c != null)
c.close();
}
logger.debug("--> fetchDataAllCount is" + ret);
return ret;
}
/*
* (non-Javadoc)
*
* @see com.jdon.model.query.PageIteratorJDBC#fetchDatas(java.util.Collection,
* java.lang.String, int, int)
*/
public PageIterator fetchDatas(Collection queryParams, 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;
if (count > 200)
count = 200;
try {
c = dataSource.getConnection();
DbUtil.testConnection(c);
ps = c.prepareStatement(sqlquery,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
jdbcUtil.setQueryParams(queryParams, ps);
rs = ps.executeQuery();
if (DbUtil.supportsFetchSize)
rs.setFetchSize(count);
if (start >= 0 && rs.absolute(start + 1)) {
List items = new ArrayList(count);
do {
Object result = rs.getObject(1);
items.add(result);
} while ((hasNext = rs.next()) && (--count > 0));
Object[] itemsA = items.toArray();
pageIterator = new PageIterator(itemsA, start, hasNext);
} else
pageIterator = new PageIterator(PageIterator.EMPTY, start,
hasNext);
logger.debug("--> get PageIterator succefully ..");
} catch (SQLException se) {
logger.error(se);
throw new Exception("SQLException: " + se.getMessage());
} catch (Exception ex) {
logger.error(ex);
throw new Exception(ex);
} finally {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (c != null)
c.close();
}
return pageIterator;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?