List of usage examples for org.apache.commons.dbutils QueryRunner query
public <T> T query(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException
From source file:com.demo.db.dao.impl.DriverDaoImpl.java
@Override public Driver get(Long id) { QueryRunner queryRunner = dbHelper.getRunner(); Driver driver = null;/*from w ww .j a v a2 s . c o m*/ try { BeanProcessor beanProcessor = new GenerousBeanProcessor(); RowProcessor rowProcessor = new BasicRowProcessor(beanProcessor); driver = queryRunner.query("select " + DRIVER_COLUMN_STRING + " from demo_driver where id = ? limit 1", new BeanHandler<Driver>(Driver.class, rowProcessor), id); } catch (SQLException e) { String methodName = Thread.currentThread().getStackTrace()[1].getMethodName(); LOGGER.error("{}??{}", methodName, id); throw new RuntimeException(e); } return driver; }
From source file:com.demo.db.dao.impl.DriverDaoImpl.java
@Override public Driver get(String cell) { QueryRunner queryRunner = dbHelper.getRunner(); Driver driver = null;/*www . j av a 2s . c om*/ try { BeanProcessor beanProcessor = new GenerousBeanProcessor(); RowProcessor rowProcessor = new BasicRowProcessor(beanProcessor); driver = queryRunner.query( "select " + DRIVER_COLUMN_STRING + " from demo_driver where cell = ? limit 1", new BeanHandler<Driver>(Driver.class, rowProcessor), cell); } catch (SQLException e) { String methodName = Thread.currentThread().getStackTrace()[1].getMethodName(); LOGGER.error("{}??{}", methodName, cell); throw new RuntimeException(e); } return driver; }
From source file:ch.vorburger.mariadb4j.MariaDB4jSampleTutorialTest.java
@Test public void testEmbeddedMariaDB4j() throws Exception { DB db = DB.newEmbeddedDB(3308);/*from ww w . j av a 2 s .com*/ db.start(); Connection conn = null; try { conn = db.getConnection(); QueryRunner qr = new QueryRunner(); // Should be able to create a new table qr.update(conn, "CREATE TABLE hello(world VARCHAR(100))"); // Should be able to insert into a table qr.update(conn, "INSERT INTO hello VALUES ('Hello, world')"); // Should be able to select from a table List<String> results = qr.query(conn, "SELECT * FROM hello", new ColumnListHandler<String>()); Assert.assertEquals(1, results.size()); Assert.assertEquals("Hello, world", results.get(0)); // Should be able to source a SQL file db.source("ch/vorburger/mariadb4j/testSourceFile.sql"); results = qr.query(conn, "SELECT * FROM hello", new ColumnListHandler<String>()); Assert.assertEquals(3, results.size()); Assert.assertEquals("Hello, world", results.get(0)); Assert.assertEquals("Bonjour, monde", results.get(1)); Assert.assertEquals("Hola, mundo", results.get(2)); } finally { DbUtils.closeQuietly(conn); } }
From source file:jp.gr.java_conf.ka_ka_xyz.processor.JPA20AnnotationProcessorFieldAccessTest.java
@Test public void testBindEmployeeByFieldAccess() throws SQLException { Connection conn = getConnection(); QueryRunner run = new QueryRunner(); RowProcessor rp = new BasicRowProcessor(new JPA20AnnotationProcessor(Jpa20Employee.class)); ResultSetHandler<List<Jpa20Employee>> ersh = new BeanListHandler<Jpa20Employee>(Jpa20Employee.class, rp); String sql = "SELECT * from person INNER JOIN employee ON person.id = employee.person_id ORDER BY id ASC"; List<Jpa20Employee> employees = run.query(conn, sql, ersh); assertEquals(3, employees.size());/*from www . j a v a 2s . c o m*/ assertEquals(new Jpa20Employee(1, "Howard", "Lovecraft", "EMP001", "Weird Tales Div."), employees.get(0)); assertEquals(new Jpa20Employee(2, "August", "Derleth", "EMP002", "Arkham House Div."), employees.get(1)); assertEquals(new Jpa20Employee(3, "Robert", "Bloch", "EMP003", "Weird Tales Div."), employees.get(2)); }
From source file:jp.gr.java_conf.ka_ka_xyz.processor.JPA20AnnotationProcessorPropertyAccessTest.java
@Test public void testBindEmployeeByPropertyAccess() throws SQLException { Connection conn = getConnection(); QueryRunner run = new QueryRunner(); RowProcessor rp = new BasicRowProcessor(new JPA20AnnotationProcessor(Jpa20Employee.class)); ResultSetHandler<List<Jpa20Employee>> ersh = new BeanListHandler<Jpa20Employee>(Jpa20Employee.class, rp); String sql = "SELECT * from person INNER JOIN employee ON person.id = employee.person_id ORDER BY id ASC"; List<Jpa20Employee> employees = run.query(conn, sql, ersh); assertEquals(3, employees.size());//from w ww . j a va2 s. co m assertEquals(new Jpa20Employee(1, "Howard", "Lovecraft", "EMP001", "Weird Tales Div."), employees.get(0)); assertEquals(new Jpa20Employee(2, "August", "Derleth", "EMP002", "Arkham House Div."), employees.get(1)); assertEquals(new Jpa20Employee(3, "Robert", "Bloch", "EMP003", "Weird Tales Div."), employees.get(2)); }
From source file:com.rhino.data.db.TickerDao.java
public List<Ticker> getTickers(String equity, Date fromDate, Date toDate) throws SQLException { QueryRunner run = new QueryRunner(DataSourceFactory.getDataSource()); String sql = "select * from EOD where equity = ? and date between ? and ? order by date"; String from = Util.getDate(fromDate); String to = Util.getDate(toDate); Object[] params = new Object[] { equity, from, to }; ResultSetHandler rsh = new BeanListHandler(Ticker.class); return (List<Ticker>) run.query(sql, rsh, params); }
From source file:azkaban.database.AzkabanDatabaseSetup.java
private void loadTableVersion() throws SQLException { logger.info("Searching for table versions in the properties table"); if (tables.containsKey("properties")) { // Load version from settings QueryRunner runner = new QueryRunner(dataSource); Map<String, String> map = runner.query(FETCH_PROPERTY_BY_TYPE, new PropertiesHandler(), PropertyType.DB.getNumVal()); for (String key : map.keySet()) { String value = map.get(key); if (key.endsWith(".version")) { String tableName = key.substring(0, key.length() - ".version".length()); installedVersions.put(tableName, value); if (tables.containsKey(tableName)) { tables.put(tableName, value); }// w ww . jav a 2 s .co m } } } else { logger.info("Properties table doesn't exist."); } }
From source file:ch.vorburger.mariadb4j.tests.MariaDB4jSampleTutorialTest.java
@Test public void testEmbeddedMariaDB4j() throws Exception { DBConfigurationBuilder config = DBConfigurationBuilder.newBuilder(); config.setPort(0); // 0 => autom. detect free port DB db = DB.newEmbeddedDB(config.build()); db.start();//from www . j a v a 2 s . c o m String dbName = "mariaDB4jTest"; // or just "test" if (!dbName.equals("test")) { // mysqld out-of-the-box already has a DB named "test" // in case we need another DB, here's how to create it first db.createDB(dbName); } Connection conn = null; try { conn = DriverManager.getConnection(config.getURL(dbName), "root", ""); QueryRunner qr = new QueryRunner(); // Should be able to create a new table qr.update(conn, "CREATE TABLE hello(world VARCHAR(100))"); // Should be able to insert into a table qr.update(conn, "INSERT INTO hello VALUES ('Hello, world')"); // Should be able to select from a table List<String> results = qr.query(conn, "SELECT * FROM hello", new ColumnListHandler<String>()); Assert.assertEquals(1, results.size()); Assert.assertEquals("Hello, world", results.get(0)); // Should be able to source a SQL file db.source("ch/vorburger/mariadb4j/testSourceFile.sql", "root", null, dbName); results = qr.query(conn, "SELECT * FROM hello", new ColumnListHandler<String>()); Assert.assertEquals(3, results.size()); Assert.assertEquals("Hello, world", results.get(0)); Assert.assertEquals("Bonjour, monde", results.get(1)); Assert.assertEquals("Hola, mundo", results.get(2)); } finally { DbUtils.closeQuietly(conn); } }
From source file:com.gs.obevo.dbmetadata.impl.dialects.MsSqlMetadataDialect.java
@Override public ImmutableCollection<DaRule> searchRules(final DaSchema schema, Connection conn) throws SQLException { QueryRunner query = new QueryRunner(); // using queryRunner so that we can reuse the connection // Do not use ANSI JOIN as it does not work in Sybase 11.x - the SQL below works across all versions ImmutableList<Map<String, Object>> maps = ListAdapter.adapt(query.query(conn, "SELECT rul.name as RULE_NAME\n" + "FROM " + schema.getName() + "..sysobjects rul\n" + "WHERE rul.type = 'R'\n" + "and not exists (\n" + "\t-- Ensure that the entry is not attached to a table; otherwise, it is a regular table constraint, and will already be dropped when the table is dropped\n" + "\tselect 1 from " + schema.getName() + "..sysconstraints c\n" + "\twhere c.constid = rul.id\n" + ")\n", new MapListHandler())).toImmutable(); return maps.collect(new Function<Map<String, Object>, DaRule>() { @Override//ww w . jav a 2 s . c o m public DaRule valueOf(Map<String, Object> map) { return new DaRuleImpl((String) map.get("RULE_NAME"), schema); } }); }
From source file:it.attocchi.db.DbUtilsConnector.java
public <T> List<T> execute(boolean keepConnOpen, String aQuery, Class<T> clazz) throws Exception { List<T> result = new ArrayList<T>(); // No DataSource so we must handle Connections manually QueryRunner run = new QueryRunner(); try {//w ww .j a v a 2 s . co m /* * Sembra che il like con i parametri ufficiali non funzioni, forse * dovuto al fatto che son tutti object */ logger.debug(aQuery); result = run.query(getConnection(), aQuery, getResultSetHandler(clazz)); } finally { if (!keepConnOpen) close(); } return result; }