| 1 16 package com.jdon.model.query.block; 17 18 import java.sql.Connection ; 19 import java.sql.PreparedStatement ; 20 import java.sql.ResultSet ; 21 import java.sql.SQLException ; 22 import java.util.ArrayList ; 23 import java.util.List ; 24 25 import javax.sql.DataSource ; 26 27 import org.apache.log4j.Logger; 28 29 import com.jdon.model.query.JdbcUtil; 30 import com.jdon.model.query.cache.QueryConditonDatakey; 31 import com.jdon.util.DbUtil; 32 33 41 public class BlockQueryJDBCTemp implements BlockQueryJDBC { 42 private final static Logger logger = Logger.getLogger(BlockQueryJDBCTemp.class); 43 44 private DataSource dataSource; 45 46 private JdbcUtil jdbcUtil; 47 48 public BlockQueryJDBCTemp(DataSource dataSource) { 49 this.dataSource = dataSource; 50 this.jdbcUtil = new JdbcUtil(); 51 } 52 53 59 public int fetchDataAllCount(QueryConditonDatakey qcd) { 60 logger.debug("[JdonFramework]--> execute fetch all count for sql sentence: " + qcd.getSqlquery()); 61 Connection c = null; 62 PreparedStatement ps = null; 63 ResultSet rs = null; 64 int ret = 0; 65 try { 66 c = dataSource.getConnection(); 67 ps = c.prepareStatement(qcd.getSqlquery(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 68 69 jdbcUtil.setQueryParams(qcd.getQueryParams(), ps); 70 71 rs = ps.executeQuery(); 72 if (rs.first()) { 73 ret = rs.getInt(1); 74 } 75 } catch (SQLException se) { 76 logger.error(se); 77 } catch (Exception ex) { 78 logger.error(ex); 79 } finally { 80 try { 81 if (rs != null) 82 rs.close(); 83 if (ps != null) 84 ps.close(); 85 if (c != null) 86 c.close(); 87 } catch (Exception ex) { 88 } 89 } 90 logger.debug("[JdonFramework]--> fetchDataAllCount is" + ret); 91 return ret; 92 } 93 94 100 public List fetchDatas(QueryConditonDatakey qcdk) { 101 logger.debug("[JdonFramework]--> fetch the primary key collection, sql sentence: " + qcdk.getSQlKey()); 102 Connection c = null; 103 PreparedStatement ps = null; 104 ResultSet rs = null; 105 106 int blockStart = qcdk.getBlockStart(); 107 int blockSize = qcdk.getBlockSize(); 108 logger.debug("[JdonFramework]--> blockStart=" + blockStart + " blockSize=" + blockSize); 109 List items = new ArrayList (blockSize); 110 try { 111 c = dataSource.getConnection(); 112 113 DbUtil.testConnection(c); 114 ps = c.prepareStatement(qcdk.getSqlquery(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 115 116 jdbcUtil.setQueryParams(qcdk.getQueryParams(), ps); 117 118 rs = ps.executeQuery(); 119 if (DbUtil.supportsFetchSize) 120 rs.setFetchSize(blockSize); 121 for (int i = 0; i < blockStart; i++) { 125 if (!rs.next()) break; 126 } 127 blockSize ++; 128 while(rs.next() && (--blockSize > 0)) { 129 Object result = rs.getObject(1); 130 logger.debug("[JdonFramework]--> found a primary key = " + result + ", type:" + result.getClass().getName()); 131 items.add(result); 132 } 133 134 logger.debug("[JdonFramework]--> get a result succefully .."); 135 } catch (SQLException se) { 136 logger.error(se); 137 138 } catch (Exception ex) { 139 logger.error(ex); 140 141 } finally { 142 try { 143 if (rs != null) 144 rs.close(); 145 if (ps != null) 146 ps.close(); 147 if (c != null) 148 c.close(); 149 } catch (Exception ex) { 150 } 151 } 152 return items; 153 154 } 155 156 } 157 | Popular Tags |