List of usage examples for org.apache.commons.dbutils QueryRunner query
public <T> T query(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException
From source file:org.dbmfs.DatabaseAccessor.java
/** * ??????Map????.<br>/*from w w w.j a v a 2 s. com*/ * ??????null?<br> * * @param targetTableName ?? * @param pKeyConcatStr (?) * @return ? */ public List<Map<String, Object>> getDataList(String targetTableName, String pKeyConcatStr) throws Exception { List<Map<String, Object>> queryResult = null; try { // ? StringBuilder cacheKeyBuf = new StringBuilder(50); cacheKeyBuf.append(targetTableName).append(tableNameSep).append(pKeyConcatStr); Object cacheDataMap = dataCacheFolder.get(cacheKeyBuf.toString()); if (cacheDataMap != null) { if (cacheDataMap instanceof List) { return (List<Map<String, Object>>) cacheDataMap; } else if (cacheDataMap instanceof Map) { List<Map<String, Object>> cacheList = new ArrayList(); cacheList.add((Map<String, Object>) cacheDataMap); return cacheList; } } // ? List<String> primaryKeyColumnNames = getPrimaryKeyColumnNames(targetTableName); Map<String, Map<String, Object>> allColumnMeta = getAllColumnMeta(targetTableName, true); if (primaryKeyColumnNames == null || primaryKeyColumnNames.size() == 0) return null; // ? String[] keyStrSplit = pKeyConcatStr.split(primaryKeySep); // ?????????????? if (primaryKeyColumnNames.size() != keyStrSplit.length) return null; // ?? StringBuilder queryBuf = new StringBuilder(); queryBuf.append("select * from "); queryBuf.append(targetTableName); queryBuf.append(" where "); // ()? Object[] params = new Object[primaryKeyColumnNames.size()]; String whereSep = ""; for (int idx = 0; idx < primaryKeyColumnNames.size(); idx++) { Map<String, Object> meta = allColumnMeta.get(primaryKeyColumnNames.get(idx)); params[idx] = DbmfsUtil.deserializeType(keyStrSplit[idx], (String) meta.get("javaTypeName")); queryBuf.append(whereSep); queryBuf.append(primaryKeyColumnNames.get(idx)); queryBuf.append(" = ? "); whereSep = " and "; } ResultSetHandler<?> resultSetHandler = new MapListHandler(); QueryRunner qr = new QueryRunner(); // queryResult = (List<Map<String, Object>>) qr.query(injectConn, queryBuf.toString(), resultSetHandler, params); if (queryResult == null || queryResult.size() < 1) return null; // ????? String metaSerializeString = serializeMetaInfomation(allColumnMeta); for (Map resultData : queryResult) { resultData.put(tableMetaInfoKey, metaSerializeString); } dataCacheFolder.put(cacheKeyBuf.toString(), queryResult); } catch (Exception e) { e.printStackTrace(); throw e; } return queryResult; }
From source file:org.dbmfs.DatabaseAccessor.java
/** * ??Map????.<br>/*www . j a v a 2 s.c o m*/ * ??????null?<br> * * @param targetTableName ?? * @param pKeyConcatStr (?) * @return ? */ public List<Map<String, Object>> getDataList(String query, List<String> primaryKeyColumnNames, String pKeyConcatStr) throws Exception { List<Map<String, Object>> queryResult = null; try { // ? String[] keyStrSplit = pKeyConcatStr.split(primaryKeySep); if (keyStrSplit.length != primaryKeyColumnNames.size()) return null; // ?? StringBuilder queryBuf = new StringBuilder(); queryBuf.append("select * from ("); queryBuf.append(query); queryBuf.append(") t1 where "); // ()? Object[] params = new Object[primaryKeyColumnNames.size()]; String whereSep = ""; // ???bindquery?????????String?? // (TODONo1)??select???? for (int idx = 0; idx < primaryKeyColumnNames.size(); idx++) { params[idx] = keyStrSplit[idx]; queryBuf.append(whereSep); queryBuf.append("t1."); queryBuf.append(primaryKeyColumnNames.get(idx)); queryBuf.append(" = ? "); whereSep = " and "; } ResultSetHandler<?> resultSetHandler = new MapListHandler(); QueryRunner qr = new QueryRunner(); // queryResult = (List<Map<String, Object>>) qr.query(injectConn, queryBuf.toString(), resultSetHandler, params); if (queryResult == null || queryResult.size() < 1) return null; for (Map resultData : queryResult) { resultData.put(tableMetaInfoKey, ""); } } catch (Exception e) { e.printStackTrace(); throw e; } return queryResult; }
From source file:org.moneta.dao.SqlSelectExecutor.java
public SearchResult call() { SearchResult result = new SearchResult(); Connection topicConnection = null; QueryRunner runner = new QueryRunner(); try {//from ww w. ja va2s.c o m topicConnection = MonetaEnvironment.getConfiguration().getConnection(topic.getDataSourceName()); RecordResultSetHandler handler = new RecordResultSetHandler(); handler.setMaxRows(this.getMaxRows()); handler.setStartRow(this.getStartRow()); handler.getAliasMap().putAll(topic.getAliasMap()); result.setResultData(runner.query(topicConnection, sqlStmt.getSqlText(), handler, sqlStmt.getHostVariableValueList().toArray())); if (topicConnection.getAutoCommit()) { DbUtils.closeQuietly(topicConnection); } else { DbUtils.commitAndCloseQuietly(topicConnection); } } catch (Exception e) { result.setErrorCode(500); result.setErrorMessage(ExceptionUtils.getStackTrace(e)); DbUtils.rollbackAndCloseQuietly(topicConnection); } return result; }
From source file:org.okinawaopenlabs.orientdb.client.ConnectionUtilsJdbcImpl.java
@Override public <T> T query(Connection conn, String sql, ResultSetHandler<T> handler, Object... params) throws SQLException { if (logger.isDebugEnabled()) { logger.trace(String.format("query(conn=%s, sql=%s, handler=%s, params=%s) - start ", conn, sql, handler, params.toString()));//www . j a v a 2s. co m } QueryRunner qRunner = new QueryRunner(); T records = qRunner.query(conn, sql, handler, params); if (logger.isDebugEnabled()) { logger.trace(String.format("query(records=%s) - end ", records)); } return records; }
From source file:org.openlogics.gears.jdbc.DataStore.java
/** * Executes the given statement using the {@link org.apache.commons.dbutils.QueryRunner} class * * @param query/*from w ww.ja v a 2 s. c o m*/ * @param handler * @param data * @param <E> * @return * @throws SQLException */ private <E> E select(String query, ResultSetHandler<E> handler, List data) throws SQLException { try { QueryRunner qr = new QueryRunner(); return qr.query(getConnection(), query, handler, data.toArray()); } finally { closeDBConn(); } }
From source file:org.openmrs.contrib.databaseexporter.ExportContext.java
public <T> T executeQuery(String sql, ResultSetHandler<T> handler, Object... params) { Connection connection = null; try {/*from w w w.j a v a2 s. co m*/ if (getConfiguration().getLogSql() == Boolean.TRUE) { log("SQL: " + sql + (params != null && params.length > 0 ? " [" + Util.toString(params) + "]" : "")); } QueryRunner runner = new QueryRunner() { protected PreparedStatement prepareStatement(Connection conn, String sql) throws SQLException { PreparedStatement ps = super.prepareStatement(conn, sql); ps.setFetchSize(Integer.MIN_VALUE); return ps; } }; connection = DbUtil.openConnection(configuration); T result = runner.query(connection, sql, handler, params); if (getConfiguration().getLogSql() == Boolean.TRUE) { log("RESULT: " + result); } return result; } catch (Exception e) { throw new RuntimeException("Unable to execute query: " + sql, e); } finally { DbUtil.closeConnection(connection); } }
From source file:org.rti.zcore.dar.utils.DatabaseUtils.java
/** * Returns an array//from w ww . j av a 2 s. c om * @param conn * @param sql * @param values * @return * @throws ServletException * @throws SQLException */ public static Object getObject(Connection conn, String sql, ArrayList values) throws ServletException, SQLException { ResultSetHandler h = new ArrayHandler(); //DataSource dataSource = null; //dataSource = DatabaseUtils.getZEPRSDataSource(); QueryRunner run = new QueryRunner(); Object[] result = (Object[]) run.query(conn, sql, values.toArray(), h); return result; }
From source file:org.rti.zcore.dar.utils.DatabaseUtils.java
/** * Fetch a single value; Support for adding value array * * @param conn/*from w w w . j a v a 2 s. co m*/ * @param sql * @param values * @return * @throws SQLException */ public static Object getScalar(Connection conn, String sql, ArrayList values) throws SQLException { ResultSetHandler h = new ScalarHandler(); QueryRunner run = new QueryRunner(); Object result = run.query(conn, sql, values.toArray(), h); return result; }
From source file:org.rti.zcore.dar.utils.DatabaseUtils.java
/** * Return the first resultset row stuffed into a bean * Be aware that getBean initialises null Objects such as Integer to 0. If you don't like that, use getZEPRSBean instead. * * @param clazz/* w w w. j a v a 2 s .co m*/ * @param sql * @param values * @return * @throws ServletException * @throws SQLException */ public static Object getBean(Connection conn, Class clazz, String sql, ArrayList values) throws ServletException, SQLException, ObjectNotFoundException { QueryRunner run = new QueryRunner(); ResultSetHandler h = new BeanHandler(clazz); // return the results in a new object generated by the BeanHandler. Object result = null; try { result = run.query(conn, sql, h, values.toArray()); } catch (SQLException e) { log.error("SQL - params: " + values + "Error: " + e); } if (result == null) { throw new ObjectNotFoundException(); } return result; }
From source file:org.rti.zcore.dar.utils.DatabaseUtils.java
/** * Fetches in object and does a better job at populating the bean than getBean, which initialises null Objects * such as Integer to 0. getZEPRSBean lets nulls stay null. * * @param conn//from w w w. j av a2 s. com * @param clazz * @param sql * @param values * @return * @throws ServletException * @throws SQLException * @throws ObjectNotFoundException */ public static Object getZEPRSBean(Connection conn, Class clazz, String sql, ArrayList values) throws ServletException, SQLException, ObjectNotFoundException { QueryRunner run = new QueryRunner(); ResultSetHandler h = new BeanHandler(clazz, new ZEPRSRowProcessor(new ZEPRSBeanProcessor())); // return the results in a new object generated by the BeanHandler. Object result = null; try { result = run.query(conn, sql, values.toArray(), h); // log.info("Getting the bean " + clazz.toString()); } catch (SQLException e) { //log.error(e); e.printStackTrace(); } if (result == null) { throw new ObjectNotFoundException(); } return result; }