List of usage examples for org.apache.commons.dbutils DbUtils closeQuietly
public static void closeQuietly(Connection conn, Statement stmt, ResultSet rs)
Connection
, Statement
and ResultSet
. From source file:org.bml.util.cache.jcs.IntToIntCache.java
/** * Handles going to the database to retrieve values for keys not already in * theCache.//from w w w .j a va2 s. com * * @param theKey Integer Key that can be used in theSQL to retrieve a value * for the cache if it does not already exist in the cache. * @param theComboPooledDataSource C3PO data source. This is passed here so * that implementations can deal with choosing and managing of data sources * @return Integer or NULL as determined by theSQL provided when this object * was built. ReturnS null if theSQL does not return an int value for theKey. */ private Integer getFromDB(final Integer theKey, final ComboPooledDataSource theComboPooledDataSource) { Connection myConnection = null; PreparedStatement myPreparedStatement = null; ResultSet myResultSet = null; Integer myValue = null; try { myConnection = theComboPooledDataSource.getConnection(); myPreparedStatement = myConnection.prepareStatement(theLookupSQL); myPreparedStatement.setInt(1, theKey); myResultSet = myPreparedStatement.executeQuery(); while (myResultSet.next()) { myValue = myResultSet.getInt(1); } } catch (SQLException mySQLException) { if (theLog.isErrorEnabled()) { theLog.error("SQLException caught while attempting retrieval from DB for cache forregion " + theCacheRegionName, mySQLException); } } catch (Exception myException) { if (theLog.isErrorEnabled()) { theLog.error("Exception caught while attempting retrieval from DB for cache forregion " + theCacheRegionName, myException); } } finally { DbUtils.closeQuietly(myConnection, myPreparedStatement, myResultSet); } return myValue; }
From source file:org.bml.util.geo.util.geolite.GeoLiteCityBlock.java
public static Map<Integer, GeoLiteCityBlock> readFromDB(ComboPooledDataSource dataSource) { Map<Integer, GeoLiteCityBlock> mapOut = new TreeMap<Integer, GeoLiteCityBlock>(); Connection con = null;// ww w. j a v a 2 s. c o m Statement st = null; ResultSet rs = null; GeoLiteCityBlock tmp = null; int c = 0; try { con = dataSource.getConnection(); st = con.createStatement(); st.setMaxRows(Integer.MAX_VALUE); st.setQueryTimeout(600000); st.setFetchSize(100000); rs = st.executeQuery(GeoLiteCityBlock.PREPARED_SELECT_SQL); while (rs.next()) { c++; mapOut.put(rs.getInt(FIELD.STARTIP.fieldName), new GeoLiteCityBlock(rs.getInt(FIELD.STARTIP.fieldName), rs.getInt(FIELD.ENDIP.fieldName), rs.getInt(FIELD.LOCID.fieldName))); if ((c % 100000) == 0) { if (LOG.isInfoEnabled()) { LOG.info("Loaded " + c + " IP Block to Location mappings"); } } } } catch (SQLException ex) { if (LOG.isWarnEnabled()) { LOG.warn("SQLException caught while loading GeoLiteCityBlock objects ", ex); } } finally { DbUtils.closeQuietly(con, st, rs); } return mapOut; }
From source file:org.bml.util.geo.util.geolite.GeoLiteCityLocation.java
public static Map<Integer, GeoLiteCityLocation> readFromDB(ComboPooledDataSource dataSource) { Map<Integer, GeoLiteCityLocation> mapOut = new HashMap<Integer, GeoLiteCityLocation>(); Connection con = null;/*from w ww.ja v a 2s. c o m*/ Statement st = null; ResultSet rs = null; GeoLiteCityBlock tmp = null; int c = 0; try { con = dataSource.getConnection(); st = con.createStatement(); st.setMaxRows(Integer.MAX_VALUE); st.setQueryTimeout(600000); st.setFetchSize(100000); rs = st.executeQuery(PREPARED_SELECT_SQL); while (rs.next()) { c++; mapOut.put(rs.getInt(FIELD.LOCID.fieldName), new GeoLiteCityLocation(rs.getInt(FIELD.LOCID.fieldName), rs.getString(FIELD.COUNTRY.fieldName), rs.getString(FIELD.REGION.fieldName), rs.getString(FIELD.CITY.fieldName), rs.getString(FIELD.POSTALCODE.fieldName), rs.getDouble(FIELD.LATITUDE.fieldName), rs.getDouble(FIELD.LONGITUDE.fieldName), rs.getString(FIELD.METROCODE.fieldName), rs.getLong(FIELD.AREACODE.fieldName))); if ((c % 100000) == 0) { if (LOG.isInfoEnabled()) { LOG.info("Loaded " + c + " Location mappings"); } } } } catch (SQLException ex) { if (LOG.isWarnEnabled()) { LOG.warn("SQLException caught while loading GeoLiteCityBlock objects ", ex); } } finally { DbUtils.closeQuietly(con, st, rs); } return mapOut; }