Example usage for java.sql Statement execute

List of usage examples for java.sql Statement execute

Introduction

In this page you can find the example usage for java.sql Statement execute.

Prototype

boolean execute(String sql) throws SQLException;

Source Link

Document

Executes the given SQL statement, which may return multiple results.

Usage

From source file:com.wolvencraft.yasp.db.ScriptRunner.java

/**
 * Executes the specified command//ww w. ja va2s.co m
 *
 * @param command Command to execute
 * @throws SQLException
 * @throws UnsupportedEncodingException
 */
private void executeStatement(String command) throws SQLException, UnsupportedEncodingException {
    Statement statement = this.connection.createStatement();
    String sql = command;
    sql = sql.replaceAll("\r\n", "\n");

    statement.execute(sql);

    try {
        statement.close();
    } catch (Exception e) {
    }
}

From source file:com.uber.hoodie.utilities.HiveIncrementalPuller.java

private void executeStatement(String sql, Statement stmt) throws SQLException {
    log.info("Executing: " + sql);
    stmt.execute(sql);
}

From source file:com.ianzepp.logging.jms.service.BasicDaoTest.java

/**
 * TODO Method description for <code>testSaveEvent()</code>
 * //  ww  w .j  a  v  a 2  s.c o  m
 * @throws Exception
 */
@Test
public final void testSaveEvent() throws Exception {
    // Save the id first
    HashMap<String, Object> paramMap = newInitializedEventMap(UUID.randomUUID());

    // Run the save
    assertTrue(newInitializedInstance().executeQuery("InsertEvent", paramMap) > 0);

    // Test the result
    String statementSql = "SELECT * FROM \"Event\" WHERE \"Id\" = '" + paramMap.get("Id") + "'";
    Statement statement = getConnection().createStatement();

    // Check the query
    assertTrue(statement.execute(statementSql));

    // Check the result set
    ResultSet resultSet = statement.getResultSet();

    assertTrue("No result set data was returned.", resultSet.first());

    for (String columnName : paramMap.keySet()) {
        assertEquals(paramMap.get(columnName), resultSet.getString(columnName));
    }

    assertFalse("Too many results were returned", resultSet.next());
}

From source file:org.silverpeas.components.blankApp.rest.BlankAppRESTTest.java

protected Statement query(String sql) {
    Statement stmt = null;
    try {/*from   w w  w  .  j a  v  a2  s.c o  m*/
        Connection conn = databaseTester.getConnection().getConnection();
        stmt = conn.createStatement();
        stmt.execute(sql);
    } catch (SQLException ex) {
        close(stmt);
        throw new AssertionError(ex);
    } catch (Exception ex) {
        throw new AssertionError(ex);
    }
    return stmt;
}

From source file:com.collective.celos.ci.testing.fixtures.deploy.hive.HiveTableDeployer.java

private void createMockedDatabase(Statement statement, String mockedDatabase) throws Exception {

    String createMockedDb = String.format(CREATE_DB_PATTERN, mockedDatabase);
    statement.execute(createMockedDb);
}

From source file:com.enonic.cms.core.vacuum.VacuumServiceImpl.java

/**
 * Execute statement.//from w w  w .  j a  va2s  . c o m
 */
private void executeStatement(final Connection conn, final String sql) throws Exception {
    Statement stmt = null;

    try {
        stmt = conn.createStatement();
        stmt.execute(sql);
    } finally {
        JdbcUtils.closeStatement(stmt);
    }
}

From source file:com.oracle.tutorial.jdbc.StoredProcedureMySQLSample.java

public void createProcedureShowSuppliers() throws SQLException {
    String createProcedure = null;

    String queryDrop = "DROP PROCEDURE IF EXISTS SHOW_SUPPLIERS";

    createProcedure = "create procedure SHOW_SUPPLIERS() " + "begin "
            + "select SUPPLIERS.SUP_NAME, COFFEES.COF_NAME " + "from SUPPLIERS, COFFEES "
            + "where SUPPLIERS.SUP_ID = COFFEES.SUP_ID " + "order by SUP_NAME; " + "end";
    Statement stmt = null;/*  w w w . j  a v a  2 s.  co  m*/
    Statement stmtDrop = null;

    try {
        System.out.println("Calling DROP PROCEDURE");
        stmtDrop = con.createStatement();
        stmtDrop.execute(queryDrop);
    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmtDrop != null) {
            stmtDrop.close();
        }
    }

    try {
        stmt = con.createStatement();
        stmt.executeUpdate(createProcedure);
    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }
}

From source file:com.oracle.tutorial.jdbc.StoredProcedureMySQLSample.java

public void createProcedureGetSupplierOfCoffee() throws SQLException {

    String createProcedure = null;

    String queryDrop = "DROP PROCEDURE IF EXISTS GET_SUPPLIER_OF_COFFEE";

    createProcedure = "create procedure GET_SUPPLIER_OF_COFFEE(IN coffeeName varchar(32), OUT supplierName varchar(40)) "
            + "begin " + "select SUPPLIERS.SUP_NAME into supplierName " + "from SUPPLIERS, COFFEES "
            + "where SUPPLIERS.SUP_ID = COFFEES.SUP_ID " + "and coffeeName = COFFEES.COF_NAME; "
            + "select supplierName; " + "end";
    Statement stmt = null;//  ww  w. j  av a2 s. c  o  m
    Statement stmtDrop = null;

    try {
        System.out.println("Calling DROP PROCEDURE");
        stmtDrop = con.createStatement();
        stmtDrop.execute(queryDrop);
    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmtDrop != null) {
            stmtDrop.close();
        }
    }

    try {
        stmt = con.createStatement();
        stmt.executeUpdate(createProcedure);
    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }
}

From source file:com.oracle.tutorial.jdbc.StoredProcedureMySQLSample.java

public void createProcedureRaisePrice() throws SQLException {

    String createProcedure = null;

    String queryDrop = "DROP PROCEDURE IF EXISTS RAISE_PRICE";

    createProcedure = "create procedure RAISE_PRICE(IN coffeeName varchar(32), IN maximumPercentage float, INOUT newPrice numeric(10,2)) "
            + "begin " + "main: BEGIN " + "declare maximumNewPrice numeric(10,2); "
            + "declare oldPrice numeric(10,2); " + "select COFFEES.PRICE into oldPrice " + "from COFFEES "
            + "where COFFEES.COF_NAME = coffeeName; "
            + "set maximumNewPrice = oldPrice * (1 + maximumPercentage); " + "if (newPrice > maximumNewPrice) "
            + "then set newPrice = maximumNewPrice; " + "end if; " + "if (newPrice <= oldPrice) "
            + "then set newPrice = oldPrice;" + "leave main; " + "end if; " + "update COFFEES "
            + "set COFFEES.PRICE = newPrice " + "where COFFEES.COF_NAME = coffeeName; " + "select newPrice; "
            + "END main; " + "end";

    Statement stmt = null;//from  w ww.ja  v a  2s. co  m
    Statement stmtDrop = null;

    try {
        System.out.println("Calling DROP PROCEDURE");
        stmtDrop = con.createStatement();
        stmtDrop.execute(queryDrop);
    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmtDrop != null) {
            stmtDrop.close();
        }
    }

    try {
        stmt = con.createStatement();
        stmt.executeUpdate(createProcedure);
    } catch (SQLException e) {
        JDBCTutorialUtilities.printSQLException(e);
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }

}

From source file:com.tacitknowledge.util.migration.jdbc.SqlScriptMigrationTaskTest.java

/**
 * Test that sybase database patches are committed when illegal multi
 * statement transaction commands are used.
 * /* www.j  av  a2  s . c om*/
 * @throws IOException
 *                 if an unexpected error occurs
 * @throws MigrationException
 *                 if an unexpected error occurs
 * @throws SQLException
 *                 if an unexpected error occurs
 */
public void testSybasePatchesCommitsOnEveryStatement() throws IOException, MigrationException, SQLException {
    InputStream is = getClass().getResourceAsStream("test/sybase_tsql.sql");
    assertNotNull(is);
    task = new SqlScriptMigrationTask("sybase_tsql.sql", 1, is);

    MockDatabaseType dbType = new MockDatabaseType("sybase");
    dbType.setMultipleStatementsSupported(false);
    context.setDatabaseType(dbType);
    int numStatements = task.getSqlStatements(context).size();

    // setup mocks to verify commits are called
    MockControl dataSourceControl = MockControl.createControl(DataSource.class);
    DataSource dataSource = (DataSource) dataSourceControl.getMock();
    context.setDataSource(dataSource);

    MockControl connectionControl = MockControl.createControl(Connection.class);
    Connection connection = (Connection) connectionControl.getMock();

    dataSourceControl.expectAndReturn(dataSource.getConnection(), connection);

    MockControl statementControl = MockControl.createControl(Statement.class);
    Statement statement = (Statement) statementControl.getMock();
    statement.execute("");
    statementControl.setMatcher(MockControl.ALWAYS_MATCHER);
    statementControl.setReturnValue(true, MockControl.ONE_OR_MORE);
    statement.close();
    statementControl.setVoidCallable(MockControl.ONE_OR_MORE);

    connectionControl.expectAndReturn(connection.isClosed(), false, MockControl.ONE_OR_MORE);
    connectionControl.expectAndReturn(connection.createStatement(), statement, numStatements);
    connectionControl.expectAndReturn(connection.getAutoCommit(), false, MockControl.ONE_OR_MORE);
    connection.commit();
    /*
     * Magic Number 4 derived from the assumption that the fixture sql
     * contains only one statement that is not allowed in a multi statement
     * transaction: commit at beginning of migrate method commit prior to
     * running the command not allowed in multi statement transaction to
     * clear the transaction state. commit after running the multi statement
     * transaction to clear transaction state for upcoming statements.
     * commit at end of migrate method once all statements executed.
     * 
     * Therefore, if you add more illegal statements to the fixture, add 2
     * more commit call's for each illegal statement.
     */
    connectionControl.setVoidCallable(4);

    dataSourceControl.replay();
    connectionControl.replay();
    statementControl.replay();

    // run tests
    task.migrate(context);
    dataSourceControl.verify();
    connectionControl.verify();
}