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:io.spring.batch.jsr.listener.DatabaseIntializer.java

@Override
public void beforeJob() throws Exception {

    InputStream resourceAsStream = this.getClass().getResourceAsStream(scriptPath);

    BufferedReader reader = new BufferedReader(new InputStreamReader(resourceAsStream));

    Connection con = dataSource.getConnection();
    Statement stmt = con.createStatement();

    String sql = reader.readLine();

    while (sql != null) {
        stmt.execute(sql);
        sql = reader.readLine();/*from  w  w  w. j av a  2  s  .  c  om*/
    }

    stmt.close();
    con.close();
}

From source file:com.nextep.designer.sqlgen.mysql.impl.MySQLDatabaseConnector.java

@Override
public void doPostConnectionSettings(IConnection conn, Connection sqlConn) throws SQLException {
    Statement stmt = null;
    try {/*from  ww  w. ja va  2 s .  co m*/
        stmt = sqlConn.createStatement();
        stmt.execute("SET CHARACTER SET utf8"); //$NON-NLS-1$
    } catch (SQLException sqle) {
        LOGGER.error("Unable to set the character set: " + sqle.getMessage(), sqle);
        throw sqle;
    } finally {
        CaptureHelper.safeClose(null, stmt);
    }
}

From source file:com.github.brandtg.switchboard.TestMysqlReplicationApplier.java

private void resetMysql() throws Exception {
    // Reset and setup local MySQL
    try (Connection conn = DriverManager.getConnection(jdbc, "root", "")) {
        Statement stmt = conn.createStatement();
        stmt.execute("RESET MASTER");
        stmt.execute("GRANT ALL ON *.* TO 'switchboard'@'localhost' IDENTIFIED BY 'switchboard'");
        stmt.execute("DROP TABLE IF EXISTS simple");
        stmt.execute("CREATE TABLE simple (k INT, v INT)");
    }/*from www. j a  va 2s  . co m*/
}

From source file:net.orpiske.ssps.common.db.version.DbVersionDao.java

/**
 * Creates the tables //from   w w w . j  a v a2s  .  c o  m
 * @throws java.sql.SQLException if unable to create the table
 */
public void createTable() throws SQLException {
    DatabaseManager databaseManager = getDatabaseManager();
    Connection conn = databaseManager.getConnection();

    String query = queries.get("createTable");

    Statement s = null;

    try {
        s = conn.createStatement();
        s.execute(query);
    } finally {
        DbUtils.close(s);
    }
}

From source file:com.manning.junitbook.ch14.ejbs.TestAdministratorEJB.java

/**
 * @see TestCase#setUp()/*from  w  w  w  .j ava  2  s. c o m*/
 */
public void setUp() throws Exception {
    Properties properties = new Properties();
    properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
    properties.put("java.naming.factory.url.pkgs", "org.jboss.naming rg.jnp.interfaces");

    InitialContext ctx = new InitialContext(properties);

    administrator = (IAdministratorLocal) ctx
            .lookup("ch14-cactus-ear-cactified/" + AdministratorBean.class.getSimpleName() + "/local");

    Connection conn = getConnection();

    Statement s = conn.createStatement();

    s.execute("DROP TABLE USERS IF EXISTS");

    s.execute("CREATE TABLE USERS(ID INT, NAME VARCHAR(40))");

    PreparedStatement psInsert = conn.prepareStatement("INSERT INTO USERS VALUES (?, ?)");

    psInsert.setInt(1, 1);
    psInsert.setString(2, "User 1");
    psInsert.executeUpdate();

    psInsert.setInt(1, 2);
    psInsert.setString(2, "User 2");
    psInsert.executeUpdate();
}

From source file:com.amazonaws.services.kinesis.connectors.redshift.RedshiftBasicEmitter.java

private void executeStatement(String statement, Connection conn) throws IOException {
    try {// w w  w  . j  a  v  a 2  s. c om
        Statement stmt = conn.createStatement();
        stmt.execute(statement);
        stmt.close();
        return;
    } catch (SQLException e) {
        LOG.error(e);
        throw new IOException(e);
    }

}

From source file:com.nextep.designer.sqlgen.postgre.impl.PostgreSqlDatabaseConnector.java

@Override
public void doPostConnectionSettings(IConnection conn, Connection sqlConn) throws SQLException {
    final String schema = conn.getSchema();

    if (schema != null && !"".equals(schema.trim())) { //$NON-NLS-1$
        Statement stmt = null;
        try {/*from  ww  w  .j a  va  2  s.c o m*/
            stmt = sqlConn.createStatement();
            stmt.execute("SET search_path TO " + schema + ",public"); //$NON-NLS-1$ //$NON-NLS-2$
        } catch (SQLException sqle) {
            LOGGER.error("Unable to set the search_path variable: " + sqle.getMessage(), sqle);
            throw sqle;
        } finally {
            CaptureHelper.safeClose(null, stmt);
        }
    }
}

From source file:morphy.utils.john.DatabaseConnection.java

public java.sql.ResultSet executeQueryWithRS(String query) {
    try {//from w w w .  j av  a  2s.co m
        if (LOG.isInfoEnabled()) {
            LOG.info("Executed query: " + query);
        }
        Statement s = getConnection().createStatement();
        s.execute(query);
        return s.getResultSet();
    } catch (SQLException se) {
        Morphy.getInstance().onError(se);
        return null;
    }
}

From source file:com.srotya.tau.wraith.silo.sql.TestSQLRulesStore.java

/**
 * Execute an SQL Query on a connection//w  w w  .  ja  v  a2  s.c  om
 * 
 * @param connectionString
 * @param createTable
 * @throws SQLException
 */
public void runSQL(String connectionString, String createTable) throws SQLException {
    Connection conn = DriverManager.getConnection(connectionString);
    Statement st = conn.createStatement();
    System.err.println(createTable);
    st.execute(createTable);
    st.close();
    conn.close();
}

From source file:eu.udig.catalog.teradata.TeradataDatabaseConnectionRunnable.java

public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {

    try {/*w  w w  .ja  v a2 s .co m*/

        Map<String, Serializable> params = new HashMap<String, Serializable>();
        params.put(DBTYPE.key, (Serializable) new TeradataDialect().dbType);
        params.put(HOST.key, host);
        params.put(PORT.key, port);
        params.put(USER.key, username);
        params.put(PASSWD.key, password);
        params.put(DATABASE.key, "dbc");

        BasicDataSource source = TeradataServiceExtension.getFactory().createDataSource(params);
        Connection connection = source.getConnection();
        try {

            Statement statement = connection.createStatement();
            if (statement.execute("SELECT F_TABLE_SCHEMA FROM SYSSPATIAL.GEOMETRY_COLUMNS")) {
                ResultSet resultSet = statement.getResultSet();
                while (resultSet.next()) {
                    databaseNames.add(resultSet.getString(1).trim());
                }
            }
            statement.close();
        } finally {
            if (connection != null) {
                connection.close();
            }
            if (source != null) {
                source.close();
            }
        }
    } catch (SQLException e) {
        checkSqlException(e);
    } catch (Exception e) {
        if (e.getCause() instanceof SQLException) {
            checkSqlException((SQLException) e.getCause());
        } else {
            Activator.log("Error connecting to datasource", e);
            result = "Unrecognized connection failure.  Check parameters and database.";
        }
    }
    ran = true;
}