本文整理匯總了Java中org.apache.commons.dbutils.ResultSetHandler類的典型用法代碼示例。如果您正苦於以下問題:Java ResultSetHandler類的具體用法?Java ResultSetHandler怎麽用?Java ResultSetHandler使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ResultSetHandler類屬於org.apache.commons.dbutils包,在下文中一共展示了ResultSetHandler類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import org.apache.commons.dbutils.ResultSetHandler; //導入依賴的package包/類
public static void main(String[] args) throws SQLException {
final String url = "jdbc:h2:./target/test;AUTO_SERVER=TRUE";
final String driver = "org.h2.Driver";
final String usr = "sa";
final String pwd = "";
QueryRunner run = new QueryRunner();
DbUtils.loadDriver(driver);
Connection conn = DriverManager.getConnection(url, usr, pwd);
// -----------------------------------------------------------------------------------
ResultSetHandler<Employee> resultHandler = new BeanHandler<Employee>(
Employee.class);
try {
Employee emp = run.query(conn,
"SELECT * FROM employee WHERE employeename=?",
resultHandler, "Jose");
System.out.println(emp.getEmployeeId());
} finally {
DbUtils.close(conn);
}
}
示例2: createInsertHandler
import org.apache.commons.dbutils.ResultSetHandler; //導入依賴的package包/類
/**
* 創建插入類型ResultSetHandler
*/
public static <T> ResultSetHandler<T> createInsertHandler() {
ResultSetHandler<T> rsh = new ResultSetHandler<T>() {
@Override
@SuppressWarnings("unchecked")
public T handle(ResultSet rs) throws SQLException {
if (rs.next()) {
return (T) rs.getObject(1);
}
return null;
}
};
return rsh;
}
示例3: selectCountBySql
import org.apache.commons.dbutils.ResultSetHandler; //導入依賴的package包/類
public static int selectCountBySql(String countSql) {
Connection conn=getConnection();
QueryRunner runner=new QueryRunner();
ResultSetHandler<Integer> rsh=new ResultSetHandler<Integer>() {
@Override
public Integer handle(ResultSet rs) throws SQLException {
while (rs.next()) {
return rs.getInt(1);
}
return 1;
}
};
int count=0;
try {
count=runner.query(conn,countSql, rsh);
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
示例4: selectSingleBySql
import org.apache.commons.dbutils.ResultSetHandler; //導入依賴的package包/類
public static double selectSingleBySql(String avgSql) {
Connection conn=getConnection();
QueryRunner runner=new QueryRunner();
ResultSetHandler<Double> rsh=new ResultSetHandler<Double>() {
@Override
public Double handle(ResultSet rs) throws SQLException {
while (rs.next()) {
return rs.getDouble(1);
}
return 0.0;
}
};
Double avg=0.0;
try {
avg=runner.query(conn,avgSql, rsh);
} catch (SQLException e) {
e.printStackTrace();
}
if(avg==null) avg=0.0;
return avg;
}
示例5: query
import org.apache.commons.dbutils.ResultSetHandler; //導入依賴的package包/類
public <T> T query(Connection conn, String sql, ResultSetHandler<T> resultSetHandler) {
Statement statement = null;
ResultSet resultSet = null;
try {
statement = conn.createStatement();
if (LOG.isDebugEnabled()) {
LOG.debug("Executing query on {}: {}", displayConnection(conn), sql);
}
resultSet = statement.executeQuery(sql);
return resultSetHandler.handle(resultSet);
} catch (SQLException e) {
throw new DataAccessException(e);
} finally {
DbUtils.closeQuietly(resultSet);
DbUtils.closeQuietly(statement);
}
}
示例6: loadTriggers
import org.apache.commons.dbutils.ResultSetHandler; //導入依賴的package包/類
@Override
public List<Trigger> loadTriggers() throws TriggerLoaderException {
logger.info("Loading all triggers from db.");
Connection connection = getConnection();
QueryRunner runner = new QueryRunner();
ResultSetHandler<List<Trigger>> handler = new TriggerResultHandler();
List<Trigger> triggers;
try {
triggers = runner.query(connection, GET_ALL_TRIGGERS, handler);
} catch (SQLException e) {
logger.error(GET_ALL_TRIGGERS + " failed.");
throw new TriggerLoaderException("Loading triggers from db failed. ", e);
} finally {
DbUtils.closeQuietly(connection);
}
logger.info("Loaded " + triggers.size() + " triggers.");
return triggers;
}
示例7: getScoreData
import org.apache.commons.dbutils.ResultSetHandler; //導入依賴的package包/類
public IRScoreData getScoreData(String hash, int mode) {
IRScoreData result = null;
try {
ResultSetHandler<List<IRScoreData>> rh = new BeanListHandler<IRScoreData>(IRScoreData.class);
List<IRScoreData> score;
score = qr.query("SELECT * FROM score WHERE sha256 = '" + hash + "' AND mode = " + mode, rh);
if (score.size() > 0) {
IRScoreData sc = null;
for (IRScoreData s : score) {
if (sc == null || s.getClear() > sc.getClear()) {
sc = s;
}
}
result = sc;
}
} catch (Exception e) {
Logger.getGlobal().severe("スコア取得時の例外:" + e.getMessage());
}
return result;
}
示例8: getScoreDatas
import org.apache.commons.dbutils.ResultSetHandler; //導入依賴的package包/類
/**
* プレイヤースコアデータを取得する
*
* @param hashes
* スコアを取得する楽曲のhash
* @return <ハッシュ, スコアデータ>のマップ
*/
public Map<String, IRScoreData> getScoreDatas(String[] hashes, int mode) {
Map<String, IRScoreData> result = new HashMap<String, IRScoreData>();
try {
ResultSetHandler<List<IRScoreData>> rh = new BeanListHandler<IRScoreData>(IRScoreData.class);
StringBuilder str = new StringBuilder();
for (String hash : hashes) {
if (str.length() > 0) {
str.append(',');
}
str.append('\'').append(hash).append('\'');
}
List<IRScoreData> scores = qr
.query("SELECT * FROM score WHERE sha256 IN (" + str.toString() + ") AND mode = " + mode, rh);
for (IRScoreData score : scores) {
result.put(score.getSha256(), score);
}
} catch (Exception e) {
Logger.getGlobal().severe("スコア取得時の例外:" + e.getMessage());
}
return result;
}
示例9: executeMultipleInserts
import org.apache.commons.dbutils.ResultSetHandler; //導入依賴的package包/類
/**
*
* @param database
* @param sqlList
* @param parametersList
* @return
*/
public static List<Long> executeMultipleInserts(MetadataDatabase database, List<String> sqlList, List<Object[]> parametersList) {
try (Connection conn = database.getConnection()) {
List<Long> results = new ArrayList<>();
for (int i = 0; i < sqlList.size(); i++) {
Object[] parameters = parametersList.get(i);
String sql = sqlList.get(i);
ResultSetHandler<Long> rsh = new ScalarHandler<>();
Long id = queryRunner.insert(conn, sql, rsh, parameters);
results.add(id);
}
return results;
} catch (SQLException e) {
throw new JVoidDataAccessException(e);
}
}
示例10: find
import org.apache.commons.dbutils.ResultSetHandler; //導入依賴的package包/類
/**
* Return the lock identified by the given identifier.
*/
public JobLock find(String id) throws SQLException {
ResultSetHandler<JobLock> rsh = new ResultSetHandler<JobLock>() {
@Override
public JobLock handle(ResultSet rs) throws SQLException {
if (!rs.next()) {
return new JobLock();
} else {
JobLock lock = new JobLock();
lock.setLocked(true);
Timestamp expiresAt = rs.getTimestamp(2);
lock.setExpiresAt(expiresAt.getTime());
return lock;
}
}
};
return queryExecutor.query("select id,expires_at from sched_job_locks where id=?", rsh, id);
}
示例11: findAll
import org.apache.commons.dbutils.ResultSetHandler; //導入依賴的package包/類
/**
* Return all the existing locks grouped by their identifier.
*/
public Map<String, JobLock> findAll() throws SQLException {
ResultSetHandler<Map<String, JobLock>> rsh = new ResultSetHandler<Map<String, JobLock>>() {
@Override
public Map<String, JobLock> handle(ResultSet rs) throws SQLException {
Map<String, JobLock> map = new HashMap<String, JobLock>();
while (rs.next()) {
JobLock lock = new JobLock();
lock.setLocked(true);
String id = rs.getString(1);
Timestamp expiresAt = rs.getTimestamp(2);
lock.setExpiresAt(expiresAt.getTime());
map.put(id, lock);
}
return map;
}
};
return queryExecutor.query("select id, expires_at from sched_job_locks", rsh);
}
示例12: test_max_retry
import org.apache.commons.dbutils.ResultSetHandler; //導入依賴的package包/類
@Test
public void test_max_retry() throws Exception {
ResultSetHandler<String> rsh = mock(ResultSetHandler.class);
String sql = "sql";
SQLException ex1 = new SQLException("error 1");
SQLException ex2 = new SQLException("error 2");
SQLException ex3 = new SQLException("error 3");
when(queryRunner.query(sql, rsh)).thenThrow(ex1, ex2, ex3);
try {
queryExecutor.query(sql, rsh);
fail("SQLException should be raised");
} catch (SQLException e) {
assertEquals(ex3, e);
}
verify(queryRunner, times(3)).query(sql, rsh);
}
示例13: mkResultSetHandler
import org.apache.commons.dbutils.ResultSetHandler; //導入依賴的package包/類
public static ResultSetHandler<List<Map<String, Object>>>
mkResultSetHandler(String... names) {
return (rs) -> {
try {
int columnCount = rs.getMetaData().getColumnCount();
List<Map<String, Object>> result = new ArrayList<>();
while (rs.next()) {
Object[] data = readOneRow(rs, columnCount);
result.add(arrayToMap(data, names));
}
return result;
} catch (SQLException e) {
throw new RuntimeException(e);
}
};
}
示例14: getTypesOfRecords
import org.apache.commons.dbutils.ResultSetHandler; //導入依賴的package包/類
public List< FieldType > getTypesOfRecords(Connection conn) throws Exception {
List< FieldType > typesOfRecords = null;
try {
this.conn = conn;
String sql = sqlCreator.getTypesOfRecordsSQL();
if( sql != null ){
DebugerLog.log("consulta: " + sql);
QueryRunner qr = new QueryRunner();
ResultSetHandler rsh = new BeanListHandler(FieldType.class);
typesOfRecords = (List< FieldType>) qr.query(conn, sql, rsh);
}
if( typesOfRecords == null || typesOfRecords.isEmpty() ){
typesOfRecords = new ArrayList< FieldType>();
FieldType fieldType = new FieldType("");
typesOfRecords.add(fieldType);
}
return typesOfRecords;
} finally {
ConnectionManager.closeConnection(conn);
}
}
示例15: findFrenteManzana
import org.apache.commons.dbutils.ResultSetHandler; //導入依賴的package包/類
@Override
public FrenteManzana findFrenteManzana(String point, Server server)
throws Exception {
String sql = "select nomvial as vialidad, tipovial as tipoVialidad, nomref1 as entrevialidad1, nomref2 as entrevialidad2,"
+ " tipovr1 as tipoEntrevialidad1, tipovr2 as tipoEntrevialidad2, nombre_loc as localidad, cve_loc as cveloc,"
+ " nombre_mun as municipio, cve_mun as cvemun, nombre_ent as entidad, nomasen as asentamiento, cp as codigoPostal,"
+ " ST_AsText(ST_Line_Interpolate_Point(ST_LineMerge(the_geom), ST_Line_Locate_Point(ST_LineMerge(the_geom), ST_GeomFromText(?, 900913)))) as punto,"
+ " ST_Distance(ST_GeomFromText(?, 900913), the_geom) as distance"
+ " from localidadesmzn.frentesdemanzana_mgn"
+ " where ST_DWithin(ST_GeomFromText(?, 900913), the_geom, 40)"
+ " order by distance" + " limit 1";
Connection conn = ConnectionManager.getConnectionW(server);
ResultSetHandler<FrenteManzana> rsh = new BeanHandler<FrenteManzana>(
FrenteManzana.class);
QueryRunner qr = new QueryRunner();
FrenteManzana fm = null;
try {
fm = qr.query(conn, sql, rsh, point, point, point);
} finally {
ConnectionManager.closeConnection(conn);
}
return fm;
}