public void setUpDb() throws ClassNotFoundException, SQLException { Connection conn = newConnection(); Statement stat = conn.createStatement(); stat.execute(String.format("CREATE TABLE %s (time INTEGER , url VARCHAR(255), type VARCHAR(255))", testTableName)); String data = "(1374100685,'http://huffingtonpost.com/','vanity')," + "(1374100900,'http://huffingtonpost.com/','click')," + "(1374101000,'http://huffingtonpost.com/','ping')"; stat.execute(String.format( "INSERT INTO %s (time, url, type) VALUES %s", testTableName, data)); stat.close(); conn.close(); }
public void execute(String statement) { try { Connection conn = newConnection(); Statement stat = conn.createStatement(); stat.execute(statement); stat.close(); conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } }
private void cleanupDb() { try { Connection conn = newConnection(); for (String sql : new String[] { String.format("DROP TABLE IF EXISTS %s", testTableName), String.format("DROP TABLE IF EXISTS %s", jobRunTableName), String.format("DROP TABLE IF EXISTS %s", queueTableName), String.format("DROP TABLE IF EXISTS %s", jobTableName) }) { Statement stat = conn.createStatement(); stat.execute(sql); stat.close(); } conn.close(); } catch (SQLException ex) { ex.printStackTrace(); // this one we swallow since db might not have cleaned up last time } }
public List<String> showTables() { List<String> tables = new ArrayList<>(); try { Connection conn = newConnection(); Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery("SHOW TABLES"); while (rs.next()) { String table = rs.getString(1); tables.add(table); } rs.close(); stat.close(); conn.close(); } catch (Exception ex) { ex.printStackTrace(); } return tables; }
private List<String> getResults(JobSpec aJob) { List<String> actual = new ArrayList<>(); try { Connection conn = dao.newConnection(); Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery( String.format("SELECT * FROM %s ORDER BY time DESC", aJob.getResultTable())); while (rs.next()) { String time = rs.getString("time"); String url = rs.getString("url"); String type = rs.getString("type"); actual.add(String.format("%s\t%s\t%s", time, url, type)); } rs.close(); stat.close(); conn.close(); } catch (Exception ex) { /*ex.printStackTrace();*/ } return actual; }
public void init() throws BackendException { this.setDataSource(H2TestUtil.getDataSource()); try { conn = newConnection(); cleanupDb(); setUpDb(); } catch (ClassNotFoundException | SQLException ex) { throw new RuntimeException(ex); } super.init(); }