List of usage examples for java.sql Statement execute
boolean execute(String sql) throws SQLException;
From source file:com.thinkbiganalytics.ingest.TableRegisterSupportTest.java
/** * Verify dropping a table.//from w ww . jav a 2 s .c om */ @Test public void testDropTable() throws Exception { // Mock SQL objects final Statement statement = Mockito.mock(Statement.class); Mockito.when(statement.execute(Mockito.anyString())).then(invocation -> { final String sql = (String) invocation.getArguments()[0]; if (sql.equals("DROP TABLE IF EXISTS `invalid`")) { throw new SQLException(); } return true; }); Mockito.when(connection.createStatement()).thenReturn(statement); // Test dropping table with success final TableRegisterSupport support = new TableRegisterSupport(connection); Assert.assertTrue(support.dropTable("`feed`")); Mockito.verify(statement).execute("DROP TABLE IF EXISTS `feed`"); // Test dropping table with exception Assert.assertFalse(support.dropTable("`invalid`")); }
From source file:com.thoughtworks.go.server.database.H2Database.java
public void backup(File file) { try (Connection connection = createDataSource().getConnection()) { Statement statement = connection.createStatement(); File dbBackupFile = new File(file, "db.zip"); statement.execute(String.format("BACKUP TO '%s'", dbBackupFile)); } catch (SQLException e) { bomb(e);// w w w . java2 s. c o m } // Ignore }
From source file:net.sf.infrared.collector.impl.persistence.InProcessDataSource.java
public void destroy() throws Exception { Connection con = null;//from w w w. j a va2 s.c o m Statement stmt = null; try { con = ds.getConnection(); stmt = con.createStatement(); stmt.execute("SHUTDOWN"); } catch (Throwable th) { // @TODO log this! } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { // @TODO log this } } if (con != null) { try { con.close(); } catch (SQLException e) { // @TODO log this } } } }
From source file:com.thinkbiganalytics.ingest.TableRegisterSupportTest.java
/** * Verify dropping multiple tables.//from ww w . j av a 2 s. c o m */ @Test public void testDropTables() throws Exception { // Mock SQL objects final Statement statement = Mockito.mock(Statement.class); Mockito.when(statement.execute(Mockito.anyString())).then(invocation -> { final String sql = (String) invocation.getArguments()[0]; if (sql.startsWith("DROP TABLE IF EXISTS `invalid`")) { throw new SQLException(); } return true; }); final Connection connection = Mockito.mock(Connection.class); Mockito.when(connection.createStatement()).thenReturn(statement); // Test dropping tables with success TableRegisterSupport support = new TableRegisterSupport(connection); Assert.assertTrue(support.dropTables("cat", "feed", EnumSet.of(TableType.MASTER, TableType.VALID, TableType.INVALID), ImmutableSet.of("backup.feed"))); Mockito.verify(statement).execute("DROP TABLE IF EXISTS `cat`.`feed`"); Mockito.verify(statement).execute("DROP TABLE IF EXISTS `cat`.`feed_valid`"); Mockito.verify(statement).execute("DROP TABLE IF EXISTS `cat`.`feed_invalid`"); Mockito.verify(statement).execute("DROP TABLE IF EXISTS backup.feed"); // Test dropping tables with exception Assert.assertFalse( support.dropTables("invalid", "feed", EnumSet.allOf(TableType.class), ImmutableSet.of())); Assert.assertFalse(support.dropTables("cat", "feed", ImmutableSet.of(), ImmutableSet.of("`invalid`"))); }
From source file:io.confluent.connect.jdbc.EmbeddedDerby.java
/** * Drop a table.//from www .j a v a 2 s.co m * @param name */ public void dropTable(String name) throws SQLException { Statement stmt = conn.createStatement(); stmt.execute("DROP TABLE \"" + name + "\""); }
From source file:com.predic8.membrane.core.interceptor.statistics.StatisticsJDBCInterceptor.java
private void createTableIfNecessary(Connection con) throws Exception { if (JDBCUtil.tableExists(con, JDBCUtil.TABLE_NAME)) return; Statement st = con.createStatement(); try {/*from w w w .j a v a2 s .c o m*/ if (JDBCUtil.isOracleDatabase(con.getMetaData())) { st.execute(JDBCUtil.getCreateTableStatementForOracle()); st.execute(JDBCUtil.CREATE_SEQUENCE); st.execute(JDBCUtil.CREATE_TRIGGER); } else if (JDBCUtil.isMySQLDatabase(con.getMetaData())) { st.execute(JDBCUtil.getCreateTableStatementForMySQL()); } else if (JDBCUtil.isDerbyDatabase(con.getMetaData())) { st.execute(JDBCUtil.getCreateTableStatementForDerby()); } else { st.execute(JDBCUtil.getCreateTableStatementForOther()); } } finally { closeConnection(st); } }
From source file:com.redhat.victims.database.VictimsSQL.java
/** * Initializes a database by created required tables. * * @throws SQLException/*from w w w.j ava2 s . c o m*/ */ protected void setUp() throws SQLException { Connection connection = getConnection(); try { if (!isSetUp(connection)) { Statement stmt = connection.createStatement(); stmt.execute(Query.CREATE_TABLE_RECORDS); stmt.execute(Query.CREATE_TABLE_FILEHASHES); stmt.execute(Query.CREATE_TABLE_META); stmt.execute(Query.CREATE_TABLE_CVES); stmt.close(); } } finally { connection.close(); } }
From source file:com.anyuan.thomweboss.persistence.jdbcimpl.user.UserDaoJdbcImpl.java
@Override public boolean deleteAll() { boolean result = false; String sql = "delete from t_user;"; try {/* w ww . j av a2 s .com*/ Statement state = getConnection().createStatement(); result = state.execute(sql); } catch (SQLException e) { e.printStackTrace(); } return result; }
From source file:com.splicemachine.derby.test.framework.SpliceGrantWatcher.java
@Override protected void starting(Description description) { LOG.trace("Starting"); Connection connection = null; Statement statement = null; ResultSet rs = null;/* w w w . j a va2 s .c om*/ try { connection = userName == null ? SpliceNetConnection.getConnection() : SpliceNetConnection.getConnectionAs(userName, password); statement = connection.createStatement(); statement.execute(createString); connection.commit(); } catch (Exception e) { LOG.error("Grant statement is invalid "); e.printStackTrace(); throw new RuntimeException(e); } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(statement); DbUtils.commitAndCloseQuietly(connection); } super.starting(description); }
From source file:com.splout.db.engine.JDBCManager.java
public QueryResult query(String query, int maxResults) throws EngineException { long start = System.currentTimeMillis(); Connection connection = null; ResultSet rs = null;//from w w w. j av a2s. c om Statement stmt = null; try { connection = connectionPool.getConnection(); // fetch a connection stmt = connection.createStatement(); QueryResult result = null; if (stmt.execute(query)) { rs = stmt.getResultSet(); result = convertResultSetToQueryResult(rs, maxResults); } else { result = QueryResult.emptyQueryResult(); } long end = System.currentTimeMillis(); log.info(Thread.currentThread().getName() + ": Query [" + query + "] handled in [" + (end - start) + "] ms."); return result; } catch (SQLException e) { throw convertException(e); } finally { try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } connection.close(); } catch (SQLException e) { throw convertException(e); } } }