本文整理汇总了Java中org.apache.commons.dbutils.handlers.ScalarHandler类的典型用法代码示例。如果您正苦于以下问题:Java ScalarHandler类的具体用法?Java ScalarHandler怎么用?Java ScalarHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ScalarHandler类属于org.apache.commons.dbutils.handlers包,在下文中一共展示了ScalarHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: migrateOrCreateIfNotExists
import org.apache.commons.dbutils.handlers.ScalarHandler; //导入依赖的package包/类
public static boolean migrateOrCreateIfNotExists(String dbname) {
String dbpath = getDbPath(dbname);
String jdbcurl = "jdbc:h2:file:" + dbpath + ";DB_CLOSE_ON_EXIT=FALSE";
LOG.info("new jdbcurl = {}", jdbcurl);
Connection conn = null;
try {
conn = DriverManager.getConnection(jdbcurl, "sa", "");
DataSource ds = new CustomFlywayDataSource(new PrintWriter(System.out), conn);
Flyway flyway = new Flyway();
flyway.setDataSource(ds);
// explicitly set h2db driver loaded (= including this jar file) class loader
flyway.setClassLoader(conn.getClass().getClassLoader());
flyway.migrate();
LOG.info("db[{}] migration success", dbname);
// may be already closed -> reconnect.
DbUtils.closeQuietly(conn);
conn = DriverManager.getConnection(jdbcurl, "sa", "");
QueryRunner r = new QueryRunner();
Long cnt = r.query(conn, "select count(*) from PROXY_HISTORY", new ScalarHandler<Long>());
LOG.info("db[{}] open/creation success(select count(*) from logtable returns {})", dbname, cnt);
return true;
} catch (SQLException e) {
LOG.error("db[" + dbname + "] open/migrate/creation error", e);
return false;
} finally {
DbUtils.closeQuietly(conn);
}
}
示例2: cachedScalar
import org.apache.commons.dbutils.handlers.ScalarHandler; //导入依赖的package包/类
/**
* Find a single value based on the given model, from arbitrary SQL, and
* use the cache.
*
* @param model
* @param sql
* @param params
* @param <T>
* @return
*/
public <T> T cachedScalar(Class model, String sql, Object ...params) {
String cacheKey = buildCacheKey(model, sql, params);
T result = (T) SmartQueryCache.getSmart(model.getCanonicalName(), cacheKey);
if (result != null) {
return result;
}
QueryRunner runner = new QueryRunner(dataSource);
try {
result = runner.query(sql, new ScalarHandler<T>(), params);
SmartQueryCache.set(model.getCanonicalName(), cacheKey, result);
return result;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
示例3: refillQueue
import org.apache.commons.dbutils.handlers.ScalarHandler; //导入依赖的package包/类
public void refillQueue() {
QueryRunner q = db.newQuery();
//String sql = "insert into users (username) values (?)";
String sql = "REPLACE INTO stallion_tickets (ticket_name) VALUES('a')";
long nextId = 0;
try {
nextId = q.insert(sql, new ScalarHandler<Long>());
} catch (SQLException e) {
throw new RuntimeException(e);
}
Log.fine("Next ticket id {0}", nextId);
nextId = nextId * 1000;
//ScalarHandler<Long> scalar = new ScalarHandler<Long>();
//Long nextId = q.query("SELECT nextval('stallion_tickets_seq')", scalar);
for (int x= 0; x < 1000; x++ ) {
loadedIds.add(nextId + x);
}
}
示例4: executeMultipleInserts
import org.apache.commons.dbutils.handlers.ScalarHandler; //导入依赖的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);
}
}
示例5: addPlayer
import org.apache.commons.dbutils.handlers.ScalarHandler; //导入依赖的package包/类
@Override
public Player addPlayer(Player player) {
try {
long id = getQueryRunner().insert("INSERT INTO Player (savekey, name, level, experience, lastUpdate, email, supporterLevel, relics, isCheater, lastQuestCreation) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", new ScalarHandler<>(),
player.getKey(),
player.getName(),
player.getLevel(),
player.getExperience(),
player.getLastUpdate(),
player.getEmail(),
player.getSupporterLevel(),
player.getRelics(),
player.isCheater(),
player.getLastQuestCreation());
player.setId(id);
return player;
} catch (SQLException e) {
if (e.getErrorCode() == MySqlErrorCode.DUPLICATE_ENTRY) {
throw new KeyAlreadyExists();
} else {
throw new GatewayError("Failed to insert player into database.", e);
}
}
}
示例6: addPurchase
import org.apache.commons.dbutils.handlers.ScalarHandler; //导入依赖的package包/类
@Override
public void addPurchase(Purchase purchase) {
try {
getQueryRunner().insert("INSERT INTO PlayerPurchasedProduct (playerId, productId, store, data, signature, appVersion, purchaseDate) VALUES(?, ?, ?, ?, ?, ?, ?);",
new ScalarHandler<>(),
purchase.getPlayerId(),
purchase.getProductId(),
purchase.getStore(),
purchase.getData(),
purchase.getSignature(),
purchase.getAppVersion(),
purchase.getPurchaseDate());
} catch (SQLException e) {
if (e.getErrorCode() == MySqlErrorCode.DUPLICATE_ENTRY) {
throw new KeyAlreadyExists();
} else {
throw new GatewayError("Failed to create purchase entity.", e);
}
}
}
示例7: addFoilCardToPlayer
import org.apache.commons.dbutils.handlers.ScalarHandler; //导入依赖的package包/类
@Override
public void addFoilCardToPlayer(long playerId, FoilCard foilCard) {
try {
int rows = getQueryRunner().update("UPDATE PlayerFoilCard SET amount=amount+? WHERE playerId=? AND cardId=? AND cardType=?;",
foilCard.getAmount(),
playerId,
foilCard.getCardId(),
foilCard.getCardType());
if (rows < 1) {
getQueryRunner().insert("INSERT INTO PlayerFoilCard (playerId, cardId, cardType, amount) VALUES (?, ?, ?, ?);",
new ScalarHandler<>(),
playerId,
foilCard.getCardId(),
foilCard.getCardType(),
foilCard.getAmount());
}
} catch (SQLException e) {
throw new GatewayError("Failed to add foil card to player.", e);
}
}
示例8: countByExample
import org.apache.commons.dbutils.handlers.ScalarHandler; //导入依赖的package包/类
/**
* 根据表达式统计长度
*
* @param example
* @param classz
* @param <T>
* @return
*/
public <T> long countByExample(Example example, Class<T> classz) {
DBTableMeta dbTableMeta = DBTableMetaFactory.getDBTableMeta(classz);
AnalyzeResult analyzeResult = getSqlAnalyzer().analyzeCountByExample(dbTableMeta, example);
printSQL(analyzeResult.getSql(), analyzeResult.getParams());
Connection conn = getDbConnectionFactory().getConnection();
QueryRunner queryRunner = new QueryRunner();
try {
return queryRunner.query(conn, analyzeResult.getSql(), new ScalarHandler<Long>(1), analyzeResult.getParams());
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
getDbConnectionFactory().releaseConnection(conn);
}
}
示例9: sumByExample
import org.apache.commons.dbutils.handlers.ScalarHandler; //导入依赖的package包/类
@Override
public <T> Object sumByExample(Example example, Class<T> classz) {
DBTableMeta dbTableMeta = DBTableMetaFactory.getDBTableMeta(classz);
AnalyzeResult analyzeResult = getSqlAnalyzer().analyzeSumByExample(dbTableMeta, example);
printSQL(analyzeResult.getSql(), analyzeResult.getParams());
Connection conn = getDbConnectionFactory().getConnection();
QueryRunner queryRunner = new QueryRunner();
try {
T result = queryRunner.query(conn, analyzeResult.getSql(), new ScalarHandler<T>(1), analyzeResult.getParams());
return result == null ? 0D : result;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
getDbConnectionFactory().releaseConnection(conn);
}
}
示例10: test5
import org.apache.commons.dbutils.handlers.ScalarHandler; //导入依赖的package包/类
@Test
public void test5() throws SQLException{//ʹ��ArrayHandler()������������࣬����ʹ�����淽�����ؽ����������
QueryRunner runner=new QueryRunner(JdbcUtils.getDataSource());//ͨ������һ�����ݿ����ӳأ�����һ��dbutils�������Ӳ��������ֶ��ͷţ�dbutils��ܻ�������ͷ�
String sql="select count(*) from users";
/*Object res[]=(Object[]) runner.query(sql,new ArrayHandler());
long totalrecord=(Long) res[0];
int count=(int)totalrecord;
System.out.println(count);
int totalrecord=((Long)res[0]).intValue;
System.out.println(totalrecord);
*/
int totalrecord=((Long)runner.query(sql, new ScalarHandler(1))).intValue();
System.out.println(totalrecord);
}
示例11: resolveAlleleList
import org.apache.commons.dbutils.handlers.ScalarHandler; //导入依赖的package包/类
@Override
public String resolveAlleleList(final String glstring) {
checkNotNull(glstring);
checkArgument(!glstring.isEmpty());
QueryRunner queryRunner = new QueryRunner(dataSource);
try {
String id = (String) queryRunner.query(ALLELE_LIST_ID_SQL, new ScalarHandler(), JdbcUtils.hash(glstring));
if (id != null) {
return id;
}
}
catch (SQLException e) {
logger.warn("could not resolve id for allele list with glstring " + abbrev(glstring), e);
}
return idSupplier.createAlleleListId();
}
示例12: resolveHaplotype
import org.apache.commons.dbutils.handlers.ScalarHandler; //导入依赖的package包/类
@Override
public String resolveHaplotype(final String glstring) {
checkNotNull(glstring);
checkArgument(!glstring.isEmpty());
QueryRunner queryRunner = new QueryRunner(dataSource);
try {
String id = (String) queryRunner.query(HAPLOTYPE_ID_SQL, new ScalarHandler(), JdbcUtils.hash(glstring));
if (id != null) {
return id;
}
}
catch (SQLException e) {
logger.warn("could not resolve id for haplotype with glstring " + abbrev(glstring), e);
}
return idSupplier.createHaplotypeId();
}
示例13: resolveGenotype
import org.apache.commons.dbutils.handlers.ScalarHandler; //导入依赖的package包/类
@Override
public String resolveGenotype(final String glstring) {
checkNotNull(glstring);
checkArgument(!glstring.isEmpty());
QueryRunner queryRunner = new QueryRunner(dataSource);
try {
String id = (String) queryRunner.query(GENOTYPE_ID_SQL, new ScalarHandler(), JdbcUtils.hash(glstring));
if (id != null) {
return id;
}
}
catch (SQLException e) {
logger.warn("could not resolve id for genotype with glstring " + abbrev(glstring), e);
}
return idSupplier.createGenotypeId();
}
示例14: resolveGenotypeList
import org.apache.commons.dbutils.handlers.ScalarHandler; //导入依赖的package包/类
@Override
public String resolveGenotypeList(final String glstring) {
checkNotNull(glstring);
checkArgument(!glstring.isEmpty());
QueryRunner queryRunner = new QueryRunner(dataSource);
try {
String id = (String) queryRunner.query(GENOTYPE_LIST_ID_SQL, new ScalarHandler(), JdbcUtils.hash(glstring));
if (id != null) {
return id;
}
}
catch (SQLException e) {
logger.warn("could not resolve id for genotype list with glstring " + abbrev(glstring), e);
}
return idSupplier.createGenotypeListId();
}
示例15: resolveMultilocusUnphasedGenotype
import org.apache.commons.dbutils.handlers.ScalarHandler; //导入依赖的package包/类
@Override
public String resolveMultilocusUnphasedGenotype(final String glstring) {
checkNotNull(glstring);
checkArgument(!glstring.isEmpty());
QueryRunner queryRunner = new QueryRunner(dataSource);
try {
String id = (String) queryRunner.query(MULTILOCUS_UNPHASED_GENOTYPE_ID_SQL, new ScalarHandler(), JdbcUtils.hash(glstring));
if (id != null) {
return id;
}
}
catch (SQLException e) {
logger.warn("could not resolve id for multilocus unphased genotype with glstring " + abbrev(glstring), e);
}
return idSupplier.createMultilocusUnphasedGenotypeId();
}