use of org.apache.commons.dbutils.QueryRunner in project notes by KevinBlandy.
the class UtilsDemo method find.
public static void find() throws SQLException {
// ����QueryRunner�������ӳض���
QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
// ����SQLģ��
String sql = "select * from users where userName=?";
// ��������
Object[] params = { "Kevin" };
// ����SQLģ��Ͳ���,ִ�в�ѯ��䷵�ض���/�����ϸ��ݵڶ�����������
User user = qr.query(sql, new BeanHandler(User.class), params);
}
use of org.apache.commons.dbutils.QueryRunner in project testcontainers-java by testcontainers.
the class JDBCDriverTest method performSimpleTestWithCharacterSet.
private void performSimpleTestWithCharacterSet(String jdbcUrl) throws SQLException {
try (HikariDataSource dataSource = getDataSource(jdbcUrl, 1)) {
boolean result = new QueryRunner(dataSource).query("SHOW VARIABLES LIKE 'character\\_set\\_connection'", rs -> {
rs.next();
String resultSetInt = rs.getString(2);
assertEquals("Passing query parameters to set DB connection encoding is successful", "utf8", resultSetInt);
return true;
});
assertTrue("The database returned a record as expected", result);
}
}
use of org.apache.commons.dbutils.QueryRunner in project testcontainers-java by testcontainers.
the class JDBCDriverTest method performSimpleTest.
private void performSimpleTest(String jdbcUrl) throws SQLException {
try (HikariDataSource dataSource = getDataSource(jdbcUrl, 1)) {
boolean result = new QueryRunner(dataSource).query("SELECT 1", rs -> {
rs.next();
int resultSetInt = rs.getInt(1);
assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
return true;
});
assertTrue("The database returned a record as expected", result);
}
}
use of org.apache.commons.dbutils.QueryRunner in project metacat by Netflix.
the class HiveConnectorFastPartitionService method getsubhandlerresults.
private <T> List<T> getsubhandlerresults(final String databaseName, final String tableName, final String filterExpression, final List<String> partitionIds, final String sql, final ResultSetHandler<List<T>> resultSetHandler, final String joinSql, final String filterSql, final List<Object> filterParams, final Sort sort, final Pageable pageable) {
// Get data source
final DataSource dataSource = DataSourceManager.get().get(catalogName);
// Create the sql
final StringBuilder queryBuilder = new StringBuilder(sql);
if (joinSql != null) {
queryBuilder.append(joinSql);
}
queryBuilder.append(" where d.NAME = ? and t.TBL_NAME = ?");
if (filterSql != null) {
queryBuilder.append(filterSql);
}
if (partitionIds != null && !partitionIds.isEmpty()) {
queryBuilder.append(" and p.PART_NAME in ('").append(Joiner.on("','").skipNulls().join(partitionIds)).append("')");
}
if (sort != null && sort.hasSort()) {
queryBuilder.append(" order by ").append(sort.getSortBy()).append(" ").append(sort.getOrder().name());
}
if (pageable != null && pageable.isPageable() && Strings.isNullOrEmpty(filterExpression)) {
if (sort == null || !sort.hasSort()) {
queryBuilder.append(" order by p.part_id");
}
queryBuilder.append(" limit ").append(pageable.getOffset()).append(',').append(pageable.getLimit());
}
List<T> partitions = Lists.newArrayList();
try (Connection conn = dataSource.getConnection()) {
final List<Object> params = Lists.newArrayList(databaseName, tableName);
if (filterSql != null) {
params.addAll(filterParams);
}
final Object[] oParams = new Object[params.size()];
partitions = new QueryRunner().query(conn, queryBuilder.toString(), resultSetHandler, params.toArray(oParams));
} catch (SQLException e) {
Throwables.propagate(e);
}
//
if (pageable != null && pageable.isPageable() && !Strings.isNullOrEmpty(filterExpression)) {
int limit = pageable.getOffset() + pageable.getLimit();
if (partitions.size() < limit) {
limit = partitions.size();
}
if (pageable.getOffset() > limit) {
partitions = Lists.newArrayList();
} else {
partitions = partitions.subList(pageable.getOffset(), limit);
}
}
return partitions;
}
use of org.apache.commons.dbutils.QueryRunner in project metacat by Netflix.
the class HiveConnectorFastPartitionService method getparameters.
private Map<Long, Map<String, String>> getparameters(final List<Long> ids, final String sql, final String idName) {
// Get data source
final DataSource dataSource = DataSourceManager.get().get(catalogName);
// Create the sql
final StringBuilder queryBuilder = new StringBuilder(sql);
if (!ids.isEmpty()) {
queryBuilder.append(" and ").append(idName).append(" in ('").append(Joiner.on("','").skipNulls().join(ids)).append("')");
}
final ResultSetHandler<Map<Long, Map<String, String>>> handler = rs -> {
final Map<Long, Map<String, String>> result = Maps.newHashMap();
while (rs.next()) {
final Long id = rs.getLong(idName);
final String key = rs.getString("param_key");
final String value = rs.getString("param_value");
Map<String, String> parameters = result.get(id);
if (parameters == null) {
parameters = Maps.newHashMap();
result.put(id, parameters);
}
parameters.put(key, value);
}
return result;
};
try (Connection conn = dataSource.getConnection()) {
return new QueryRunner().query(conn, queryBuilder.toString(), handler);
} catch (SQLException e) {
Throwables.propagate(e);
}
return Maps.newHashMap();
}
Aggregations