1 16 package com.jdon.model.query; 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.Collection ; 24 import java.util.List ; 25 26 import javax.sql.DataSource ; 27 28 import org.apache.log4j.Logger; 29 30 import com.jdon.util.DbUtil; 31 32 39 public class JdbcTemp { 40 private final static Logger logger = Logger.getLogger(JdbcTemp.class); 41 42 private DataSource dataSource; 43 44 private JdbcUtil jdbcUtil; 45 46 public JdbcTemp(DataSource dataSource) { 47 this.dataSource = dataSource; 48 this.jdbcUtil = new JdbcUtil(); 49 } 50 51 63 public Object querySingleObject(Collection queryParams, String sqlquery) 64 throws Exception { 65 logger.debug("[JdonFramework]--> enter getSingleObject "); 66 Connection c = null; 67 PreparedStatement ps = null; 68 ResultSet rs = null; 69 Object o = null; 70 try { 71 c = dataSource.getConnection(); 72 ps = c.prepareStatement(sqlquery, 73 ResultSet.TYPE_SCROLL_INSENSITIVE, 74 ResultSet.CONCUR_READ_ONLY); 75 logger.debug(sqlquery); 76 jdbcUtil.setQueryParams(queryParams, ps); 77 78 rs = ps.executeQuery(); 79 if (rs.first()) { 80 o = rs.getObject(1); 81 logger.debug("[JdonFramework]-->in db found it:" + o.getClass().getName()); 82 } 83 } catch (SQLException se) { 84 throw new Exception ("SQLException: " + se.getMessage()); 85 } catch (Exception ex) { 86 logger.error(ex); 87 throw new Exception (ex); 88 } finally { 89 if (rs != null) 90 rs.close(); 91 if (ps != null) 92 ps.close(); 93 if (c != null) 94 c.close(); 95 } 96 97 return o; 98 } 99 100 101 115 public List queryMultiObject(Collection queryParams, String sqlquery) 116 throws Exception { 117 logger.debug("[JdonFramework]--> enter queryMultiObject "); 118 Connection c = null; 119 PreparedStatement ps = null; 120 ResultSet rs = null; 121 List list = new ArrayList (); 122 try { 123 c = dataSource.getConnection(); 124 ps = c.prepareStatement(sqlquery, 125 ResultSet.TYPE_SCROLL_INSENSITIVE, 126 ResultSet.CONCUR_READ_ONLY); 127 logger.debug("[JdonFramework]" + sqlquery); 128 jdbcUtil.setQueryParams(queryParams, ps); 129 130 rs = ps.executeQuery(); 131 list = jdbcUtil.extract(rs); 132 } catch (SQLException se) { 133 throw new Exception ("SQLException: " + se.getMessage()); 134 } catch (Exception ex) { 135 logger.error(ex); 136 throw new Exception (ex); 137 } finally { 138 if (rs != null) 139 rs.close(); 140 if (ps != null) 141 ps.close(); 142 if (c != null) 143 c.close(); 144 } 145 return list; 146 } 147 148 160 public List queryMultiObject(Collection queryParams, String sqlquery, int start, int count) 161 throws Exception { 162 logger.debug("[JdonFramework]--> enter queryMultiObject from:" + start + " size:" + count); 163 Connection c = null; 164 PreparedStatement ps = null; 165 ResultSet rs = null; 166 List items = new ArrayList (count); 167 boolean hasNext = false; 168 try { 169 c = dataSource.getConnection(); 170 DbUtil.testConnection(c); 171 ps = c.prepareStatement(sqlquery, 172 ResultSet.TYPE_SCROLL_INSENSITIVE, 173 ResultSet.CONCUR_READ_ONLY); 174 logger.debug(sqlquery); 175 jdbcUtil.setQueryParams(queryParams, ps); 176 rs = ps.executeQuery(); 177 if (DbUtil.supportsFetchSize) 178 rs.setFetchSize(count); 179 if (start >= 0 && rs.absolute(start + 1)) { 180 do { 181 items = jdbcUtil.extract(rs); 182 } while ((hasNext = rs.next()) && (--count > 0)); 183 } 184 } catch (SQLException se) { 185 throw new Exception ("SQLException: " + se.getMessage()); 186 } catch (Exception ex) { 187 logger.error(ex); 188 throw new Exception (ex); 189 } finally { 190 if (rs != null) 191 rs.close(); 192 if (ps != null) 193 ps.close(); 194 if (c != null) 195 c.close(); 196 } 197 return items; 198 } 199 200 210 public void operate(Collection insertParams, String sql) 211 throws Exception { 212 logger.debug("[JdonFramework]--> enter getSingleObject "); 213 Connection c = null; 214 PreparedStatement ps = null; 215 ResultSet rs = null; 216 Object o = null; 217 try { 218 c = dataSource.getConnection(); 219 ps = c.prepareStatement(sql); 220 logger.debug(sql); 221 jdbcUtil.setQueryParams(insertParams, ps); 222 223 ps.executeUpdate(); 224 } catch (SQLException se) { 225 throw new Exception ("SQLException: " + se.getMessage()); 226 } catch (Exception ex) { 227 logger.error(ex); 228 throw new Exception (ex); 229 } finally { 230 if (rs != null) 231 rs.close(); 232 if (ps != null) 233 ps.close(); 234 if (c != null) 235 c.close(); 236 } 237 } 238 239 } 240 | Popular Tags |