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:Main.java

/**
 * DOCUMENT ME!/*from  w  w  w. ja  v  a 2  s .  c  o m*/
 * 
 * @param database
 *            DOCUMENT ME!
 * @param sql
 *            DOCUMENT ME!
 * 
 * @throws SQLException
 *             DOCUMENT ME!
 */
public static void execute(String database, String sql) throws SQLException {
    Connection c = java.sql.DriverManager.getConnection("jdbc:hsqldb:file:" + database);
    Statement st = c.createStatement();
    st.execute(sql);
    st.close();
    c.close();
}

From source file:Main.java

public static void cleanup(Connection conn) throws SQLException {
    Statement stmt = conn.createStatement();
    stmt.execute("UPDATE EMP SET SAL = SAL - 500");
    stmt.execute("COMMIT");
    stmt.close();/*  w w w .  j av a2 s.  c om*/
}

From source file:Main.java

public static void doSomeChanges(Connection conn) throws SQLException {
    Statement otherStmt = conn.createStatement();
    otherStmt.execute("update emp set sal = sal + 500");
    otherStmt.execute("commit");
    otherStmt.close();/*from  w  ww . j a v  a  2s  .  co m*/
}

From source file:com.buddycloud.channeldirectory.commons.db.CreateSchema.java

private static void runScript(ChannelDirectoryDataSource channelDirectoryDataSource, String sqlFile)
        throws IOException, FileNotFoundException, SQLException {
    List<String> readLines = IOUtils.readLines(new FileInputStream(sqlFile));

    Connection connection = channelDirectoryDataSource.getConnection();
    StringBuilder statementStr = new StringBuilder();

    for (String line : readLines) {
        statementStr.append(line);//from   w w  w .  j  a  v  a2 s .  com
        if (line.endsWith(SQL_DELIMITER)) {
            Statement statement = connection.createStatement();
            statement.execute(statementStr.toString());
            statement.close();
            statementStr.setLength(0);
        }
    }

    connection.close();
}

From source file:Main.java

/**
 * Helper method to create a Redshift table
 * /*from   w w  w.  j av a 2s . c om*/
 * @param redshiftURL
 *            The JDBC URL of the Redshift database
 * @param loginProperties
 *            A properties file containing the authentication credentials for the database
 * @param tableName
 *            The table to create
 * @param fields
 *            A list of column specifications that will be comma separated in the create table
 *            statement
 * @throws SQLException
 *             Table creation failed
 */
public static void createRedshiftTable(String redshiftURL, Properties loginProperties, String tableName,
        List<String> fields) throws SQLException {
    Connection conn = DriverManager.getConnection(redshiftURL, loginProperties);
    Statement stmt = conn.createStatement();
    stmt.execute("CREATE TABLE " + tableName + " " + toSQLFields(fields) + ";");
    stmt.close();
    conn.close();
}

From source file:Main.java

/***      Check for  WISH_LIST table    ****/
public static boolean wwdChk4Table(Connection conTst) throws SQLException {
    boolean chk = true;
    boolean doCreate = false;
    try {//w  w w. j  av  a 2  s  . co  m
        Statement s = conTst.createStatement();
        s.execute("update WISH_LIST set ENTRY_DATE = CURRENT_TIMESTAMP, WISH_ITEM = 'TEST ENTRY' where 1=3");
    } catch (SQLException sqle) {
        String theError = (sqle).getSQLState();
        //   System.out.println("  Utils GOT:  " + theError);
        /** If table exists will get -  WARNING 02000: No row was found **/
        if (theError.equals("42X05")) // Table does not exist
        {
            return false;
        } else if (theError.equals("42X14") || theError.equals("42821")) {
            System.out.println(
                    "WwdChk4Table: Incorrect table definition. Drop table WISH_LIST and rerun this program");
            throw sqle;
        } else {
            System.out.println("WwdChk4Table: Unhandled SQLException");
            throw sqle;
        }
    }
    //  System.out.println("Just got the warning - table exists OK ");
    return true;
}

From source file:common.DBTestUtil.java

public static void initDB() throws IOException, SQLException {
    String query = "";
    FileInputStream inputStream = new FileInputStream(TEST_DATA_FILE);

    try {//from  ww w. j  a  v a  2 s  .com
        query = IOUtils.toString(inputStream);
    } finally {
        inputStream.close();
    }

    Connection connection = DB.getConnection();

    try {
        Statement statement = connection.createStatement();
        statement.execute(query);
    } finally {
        connection.close();
    }
}

From source file:com.earldouglas.xjdl.io.JdbcLicenseLoaderTest.java

@BeforeClass
public static void setupDatabase() throws Exception {
    basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName(jdbcDriver.class.getName());
    basicDataSource.setUrl("jdbc:hsqldb:mem:db");
    basicDataSource.setUsername("sa");
    basicDataSource.setPassword("");

    Connection connection = basicDataSource.getConnection();
    Statement statement = connection.createStatement();
    statement.execute("create table licenses (license varchar(512))");
}

From source file:com.invariantproperties.udt.AbstractDatabaseTest.java

/**
 * Unload jar file containing our user-defined types.
 * //from  ww w .  jav a  2s  .  co  m
 * @throws SQLException
 */
@AfterClass
public static void unloadJarFile() throws SQLException {
    Connection conn = ds.getConnection();
    Statement stmt = conn.createStatement();
    stmt.execute("select sqlj.remove_jar('ip_udt', true)");
    stmt.execute("drop schema invariantproperties");
    stmt.close();
    conn.close();
}

From source file:com.invariantproperties.udt.AbstractDatabaseTest.java

/**
 * Load jar file containing our user-defined types.
 * //w ww  .  j a  v a2s.c  o m
 * @throws SQLException
 */
@BeforeClass
public static void loadJarFile() throws SQLException {
    File file = new File("userdefinedtypes.jar");
    Connection conn = ds.getConnection();
    Statement stmt = conn.createStatement();
    stmt.execute("create schema invariantproperties");
    stmt.execute(String.format("select sqlj.install_jar('file://%s', 'ip_udt', true)", file.getAbsolutePath()));
    stmt.execute("select sqlj.set_classpath('invariantproperties', 'ip_udt')");
    stmt.close();
    conn.close();
}