List of usage examples for org.apache.commons.dbutils ResultSetHandler ResultSetHandler
ResultSetHandler
From source file:esg.node.filters.AccessLoggingDAO.java
public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.queryRunner = new QueryRunner(dataSource); this.idResultSetHandler = new ResultSetHandler<Integer>() { public Integer handle(ResultSet rs) throws SQLException { if (!rs.next()) { return -1; }//from ww w . ja v a 2 s . c o m return rs.getInt(1); } }; }
From source file:esg.node.components.metrics.MetricsVarsDAO.java
protected void buildResultSetHandler() { log.trace("Setting up result handler"); metricsVarsHandler = new ResultSetHandler<List<MetricsVarsDAO.VarInfo>>() { public List<MetricsVarsDAO.VarInfo> handle(ResultSet rs) throws SQLException { List<MetricsVarsDAO.VarInfo> varInfos = new Vector<VarInfo>(); VarInfo varInfo = new VarInfo(); if (!rs.next()) return varInfos; do {/*from w ww .j av a2 s. c o m*/ varInfo.project = rs.getString(1); varInfo.model = rs.getString(2); varInfo.count = rs.getInt(3); varInfo.sum = rs.getLong(4); varInfo = new VarInfo(); } while (rs.next()); return varInfos; } }; }
From source file:esg.node.components.metrics.MetricsExpDAO.java
protected void buildResultSetHandler() { log.trace("Setting up result handler"); metricsExpHandler = new ResultSetHandler<List<MetricsExpDAO.ExpInfo>>() { public List<MetricsExpDAO.ExpInfo> handle(ResultSet rs) throws SQLException { List<MetricsExpDAO.ExpInfo> expInfos = new Vector<ExpInfo>(); ExpInfo expInfo = new ExpInfo(); if (!rs.next()) return expInfos; do {//from w ww . jav a 2s . c om expInfo.project = rs.getString(1); expInfo.experiment = rs.getString(2); expInfo.count = rs.getInt(3); expInfo.sum = rs.getLong(4); expInfos.add(expInfo); expInfo = new ExpInfo(); } while (rs.next()); return expInfos; } }; }
From source file:hermes.store.schema.DefaultJDBCAdapter.java
public Collection<Destination> getDestinations(Connection connection, String storeId) throws SQLException, JMSException { final Collection<Destination> destinations = new ArrayList<Destination>(); final QueryRunner runner = new QueryRunner(); Hermes.ui.getDefaultMessageSink().add("Getting message store destinations...."); runner.query(connection, "select distinct destination, domain from stores where storeId=? ", new Object[] { storeId }, new ResultSetHandler() { public Object handle(ResultSet rs) throws SQLException { while (rs.next()) { final Domain domain = Domain.getDomain(rs.getInt(2)); if (domain.equals(Domain.QUEUE)) { destinations.add(new MessageStoreQueue(rs.getString(1))); } else if (domain.equals(Domain.TOPIC)) { destinations.add(new MessageStoreTopic(rs.getString(1))); } else if (domain.equals(Domain.FOLDER)) { destinations.add(new MessageStoreFolder(rs.getString(1))); }//ww w . j av a2 s . c o m } return destinations; } }); Hermes.ui.getDefaultMessageSink().add("Getting message store folders.... done."); return destinations; }
From source file:esg.node.components.notification.NotificationDAO.java
public void init() { //TODO: Put a *bound* on cache this to some reasonable length so //it doesn't get crazy large, or even better yet use something //like EHCache, perhaps? (maybe overkill) emailResolverCache = new HashMap<String, String>(); //openid2Email = new OpenId2EmailAddressResolution(); //openid2Email.init(attributeQueryIssuer,yadisPropertiesFilename,attributeServiceClientPropertiesFilename); log.trace("Setting up result handler"); handler = new ResultSetHandler<List<NotificationDAO.NotificationRecipientInfo>>() { public List<NotificationDAO.NotificationRecipientInfo> handle(ResultSet rs) throws SQLException { List<NotificationDAO.NotificationRecipientInfo> nris = new Vector<NotificationDAO.NotificationRecipientInfo>(); NotificationDAO.NotificationRecipientInfo nri = new NotificationDAO.NotificationRecipientInfo(); String lastUserid = null; String lastUserAddress = null; if (!rs.next()) { return null; }//from w w w.j a v a2 s . co m do { String userid = rs.getString(1); String userAddress = rs.getString(2); //Do the email resolution only if email is not available and not already cached //if( (null != userid) && (null == userAddress) && (null == (userAddress = emailResolverCache.get(userid)) ) ) { // try{ // userAddress = openid2email.resolve(userid); //phil's resolver (uses SAML) // emailResolverCache.put(userid,userAddress); // }catch (Throwable e) { // log.warn("Cannot perform openid2email resolution: "+e.getMessage()); // log.error(e); // } //} //Create a new object PER NEW EMAIL ADDRESS... if ((lastUserAddress == null) || (!lastUserAddress.equals(userAddress))) { //NOTE - Memory Optimization: See comment section of ESGNotifier... //(memory optimization place to put callback to notifier.generateNotification(lastUserid,lastUserAddress,nri);) //(and then remove the call to nris.add(nri)) //(also would have to make this object take a [final] 'notifier' obj, prob via the call getNotificationInfo) nri = new NotificationDAO.NotificationRecipientInfo(); nri.withValues(userid, userAddress); } //NOTE: As a refinement take a look at using a //"BeanListHandler" I think it would provide //flexibility with ordering of columns, but has //the additional stipulation of having to have to //name attributes the same as column headings or //alias the result column names from the query. //At the moment I am going with what I //know. (K.I.S.S.) //For more info on bean handling in dbUtil see: http://commons.apache.org/dbutils/examples.html //For reference on jdbc/sql type mapping: //http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html nri.withDatasetInfo(rs.getString(3), rs.getString(4), rs.getLong(5)); nris.add(nri); lastUserid = userid; lastUserAddress = userAddress; } while (rs.next()); return nris; } }; //inserts entry into table for this node registerWithNotificationRunLog(); }
From source file:gr.osmosis.rcpsamples.contact.db.derby.DerbyContactsDAO.java
public int getMaxId() { StringBuffer sbSelect = new StringBuffer(); sbSelect.append("SELECT MAX(ID) AS MAX_ID "); sbSelect.append(" FROM "); sbSelect.append(ContactsConstants.CONTACTS_TABLE_NAME); // Create a QueryRunner that will use connections from // the given DataSource DataSource d = DerbyDAOFactory.getDataSource(); QueryRunner run = new QueryRunner(d); ResultSetHandler h = new ResultSetHandler() { public Object handle(ResultSet rs) throws SQLException { if (!rs.next()) { return null; }/* w w w.jav a2s . c o m*/ int max; max = rs.getInt("MAX_ID"); Integer value = new Integer(max); return value; } }; Object result = null; try { result = run.query(sbSelect.toString(), h); } catch (SQLException sex) { sex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } int max; if (result != null) { max = ((Integer) result).intValue(); } else { max = 0; } return max; }
From source file:esg.node.components.metrics.MetricsDAO.java
private int registerWithMetricsRunLog() { int ret = -1; try {/* w w w.j av a 2 s .c om*/ log.trace("Registering this node [" + getNodeID() + "] into database"); int count = queryRunner.query(regCheckEntryQuery, new ResultSetHandler<Integer>() { public Integer handle(ResultSet rs) throws SQLException { if (!rs.next()) { return -1; } return rs.getInt(1); } }, MetricsDAO.this.getNodeID()); if (count > 0) { log.info("Yes, " + MetricsDAO.this.getNodeID() + " exists in metrics run log table"); } else { log.info("No, " + MetricsDAO.this.getNodeID() + " does NOT exist in metrics run log table"); ret = queryRunner.update(regAddEntryQuery, MetricsDAO.this.getNodeID(), System.currentTimeMillis() / 1000); } } catch (SQLException ex) { log.error(ex); } return ret; }
From source file:de.iritgo.aktario.jdbc.LoadObject.java
/** * Load a list of iobjects./*w w w. j av a2 s . com*/ * * @param dataSource The data source to load from. * @param owner The owner of the list. * @param list the object list to load. */ public void loadList(final DataSource dataSource, DataObject owner, final IObjectList list) { try { QueryRunner query = new QueryRunner(dataSource); query.query("select elemType, elemId from IritgoObjectList where id=? and attribute=?", new Object[] { new Long(owner.getUniqueId()), list.getAttributeName() }, new ResultSetHandler() { public Object handle(ResultSet rs) throws SQLException { while (rs.next()) { DataObject element = load(dataSource, rs.getString("elemType"), rs.getLong("elemId")); if (element != null) { list.add(element); } } return null; } }); } catch (SQLException x) { Log.logError("persist", "LoadObject", "Error while loading the object list " + list.getOwner().getTypeId() + "." + list.getAttributeName() + ": " + x); } }
From source file:com.hangum.tadpole.engine.sql.util.QueryUtils.java
/** * query to csv//from w w w. j av a 2s. c o m * * @param userDB * @param strQuery * @param listParam * @param isAddHead is true add head title * @param strDelimiter if delimite is null default comma(,) */ @SuppressWarnings("deprecation") public static String selectToCSV(final UserDBDAO userDB, final String strQuery, final List<Object> listParam, final boolean isAddHead, final String strDelimiter) throws Exception { final StringWriter stWriter = new StringWriter(); SqlMapClient client = TadpoleSQLManager.getInstance(userDB); QueryRunner qr = new QueryRunner(client.getDataSource()); qr.query(strQuery, listParam.toArray(), new ResultSetHandler<Object>() { @Override public Object handle(ResultSet rs) throws SQLException { ResultSetMetaData metaData = rs.getMetaData(); char strDel; if ("".equals(strDelimiter)) { strDel = ','; } else if (StringUtils.equalsIgnoreCase("\t", strDelimiter)) { strDel = (char) 9; } else { strDel = strDelimiter.charAt(0); } CSVWriter csvWriter = new CSVWriter(stWriter, strDel); if (isAddHead) { String[] arryString = new String[metaData.getColumnCount()]; for (int i = 1; i <= metaData.getColumnCount(); i++) { arryString[i - 1] = metaData.getColumnLabel(i); } csvWriter.writeNext(arryString); } while (rs.next()) { String[] arryString = new String[metaData.getColumnCount()]; for (int i = 1; i <= metaData.getColumnCount(); i++) { arryString[i - 1] = rs.getString(i); } csvWriter.writeNext(arryString); } return stWriter.toString(); } }); return stWriter.toString(); }
From source file:esg.node.components.monitoring.MonitorDAO.java
private int registerWithMonitorRunLog() { int ret = -1; try {//w ww . ja v a2s . c om log.trace("Registering this node [" + getNodeID() + "] into database"); int count = queryRunner.query(regCheckEntryQuery, new ResultSetHandler<Integer>() { public Integer handle(ResultSet rs) throws SQLException { if (!rs.next()) { return -1; } return rs.getInt(1); } }, MonitorDAO.this.getNodeID()); if (count > 0) { log.info("Yes, " + MonitorDAO.this.getNodeID() + " exists in monitor run log table"); } else { log.info("No, " + MonitorDAO.this.getNodeID() + " does NOT exist in monitor run log table"); ret = queryRunner.update(regAddEntryQuery, MonitorDAO.this.getNodeID(), System.currentTimeMillis() / 1000); } } catch (SQLException ex) { log.error(ex); } return ret; }