Example usage for java.sql Statement executeUpdate

List of usage examples for java.sql Statement executeUpdate

Introduction

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

Prototype

int executeUpdate(String sql) throws SQLException;

Source Link

Document

Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.

Usage

From source file:org.easyrec.utils.Benchmark.java

private static void createItems(Statement st) throws Exception {

    for (int j = 1; j <= NUMBER_OF_TENANTS; j++) {

        for (int i = 1; i <= NUMBER_OF_ITEMS; i++) {

            StringBuilder s = new StringBuilder().append("          INSERT INTO ").append("  item").append("(")
                    .append("  tenantId,").append("  itemid,").append("  itemtype,").append("  description,")
                    .append("  url,").append("  imageurl,").append("  active").append(") ").append("VALUE (")
                    .append(j).append("  ,").append(i).append("  ,'ITEM',")
                    .append(" 'The item description of item: " + i + "'")
                    .append("  ,'http://this.is.my.tenant.com/item/url/itemid/" + i + "',")
                    .append("  'http://easyrec.org/img/easyrec_logo.gif?id=" + i + "',").append("  1")
                    .append(")");
            st.executeUpdate(s.toString());
        }/*from   w ww  .j av a 2  s .  c  o m*/
    }
    System.out.println("items for " + NUMBER_OF_TENANTS + " tenant(s) created.");
}

From source file:com.bt.aloha.fitnesse.SetupFixture.java

public int clearDatabaseData() {
    Connection c = getConnection(loadDatabaseProperties());
    Statement statement = null;
    int deletedRows = 0;
    try {//from www  .j  ava  2 s  .c om
        statement = c.createStatement();
        deletedRows = statement.executeUpdate("delete from StateInfo");
        deletedRows += statement.executeUpdate("delete from callinfo");
        deletedRows += statement.executeUpdate("delete from conferenceinfo");
        return deletedRows;
    } catch (SQLException e) {
        throw new IllegalStateException("Unable to delete data", e);
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                log.warn(e);
            }
        }
        if (c != null) {
            try {
                c.close();
            } catch (SQLException e) {
                log.warn(e);
            }
        }
    }
}

From source file:com.taobao.tddl.jdbc.group.integration.TransactionTest.java

@Test
public void springTest() throws Exception {

    Connection conn = ds.getConnection();
    conn.setAutoCommit(false);/* ww  w  .  j a  v a2  s .  c o m*/

    // Statementcrud
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select f1,f2 from crud");
    assertEquals(stmt.executeUpdate("insert into crud(f1,f2) values(10,'str')"), 1);
    assertEquals(stmt.executeUpdate("insert into crud(f1,f2) values(10,'str')"), 1);
    conn.commit();
    conn.setAutoCommit(true);
    rs = stmt.executeQuery("select f1,f2 from crud");
    rs.next();
    assertEquals(rs.getInt(1), 10);
    assertEquals(rs.getString(2), "str");
    rs.close();

    assertEquals(stmt.executeUpdate("delete from crud"), 2);

    stmt.close();
    conn.close();
}

From source file:com.rapidbackend.socialutil.install.dbinstall.SQLScriptRunner.java

/** Run script, logs messages, and optionally throws exception on error */
public void runScript(Connection con, boolean stopOnError) throws SQLException {
    failed = false;//from   w  ww .j a v a 2  s .com
    errors = false;
    for (String command : commands) {

        // run each command
        try {
            Statement stmt = con.createStatement();
            stmt.executeUpdate(command);
            if (!con.getAutoCommit())
                con.commit();

            // on success, echo command to messages
            successMessage(command);

        } catch (SQLException ex) {
            // add error message with text of SQL command to messages
            errorMessage("ERROR: SQLException executing SQL [" + command + "] : " + ex.getLocalizedMessage());
            // add stack trace to messages
            StringWriter sw = new StringWriter();
            ex.printStackTrace(new PrintWriter(sw));
            errorMessage(sw.toString());
            if (stopOnError) {
                failed = true;
                throw ex;
            }
        }
    }
}

From source file:gr.abiss.calipso.config.DataSourceFactoryBean.java

public void destroy() throws Exception {
    if (dataSource instanceof SingleConnectionDataSource) {
        logger.info("attempting to shut down embedded HSQLDB database");
        Connection con = dataSource.getConnection();
        Statement stmt = con.createStatement();
        stmt.executeUpdate("SHUTDOWN");
        stmt.close();/*from w w  w.j  a va 2s  . c  om*/
        con.close();
        logger.info("embedded HSQLDB database shut down successfully");
    } else if (dataSource instanceof BasicDataSource) {
        logger.info("attempting to close Apache DBCP data source");
        ((BasicDataSource) dataSource).close();
        logger.info("Apache DBCP data source closed successfully");
    } else {
        logger.info("context shutting down for JNDI datasource");
    }
}

From source file:log4j.TestCastorAppender.java

public void setUp() throws Exception {
    JDOManager.loadConfiguration(TestCastorAppender.class.getResource(JDO_CONF).toString());
    JDOManager jdo = JDOManager.createInstance("LOGGING");
    Database db = jdo.getDatabase();/*w  w w .j a va2  s .  c  om*/
    db.begin();
    Connection connection = db.getJdbcConnection();
    Statement statement = connection.createStatement();
    statement.executeUpdate("DROP TABLE LOG_EXTENSION");
    statement.executeUpdate("DROP TABLE LOG_EXCEPTION");
    statement.executeUpdate("DROP TABLE LOG");

    statement.executeUpdate("CREATE TABLE LOG (LOG_ID INT NOT NULL, "
            + " LOG_TIMESTAMP   TIMESTAMP        NOT NULL, " + " LOG_CLASS       VARCHAR  ( 100)  NOT NULL, "
            + " LOG_LEVEL       VARCHAR  (  10)  NOT NULL, " + " LOG_THREAD      VARCHAR  ( 100)  NOT NULL, "
            + " LOG_MESSAGE     VARCHAR  (1000)  DEFAULT NULL, "
            + " LOG_COUNT       INT              NOT NULL)");
    statement.executeUpdate("ALTER TABLE LOG ADD PRIMARY KEY (LOG_ID)");
    statement.executeUpdate("CREATE TABLE LOG_EXCEPTION ( " + " LOGE_ID         INT              NOT NULL, "
            + " LOGE_LOG_ID     INT              NOT NULL, " + " LOGE_STACKTRACE BLOB             NOT NULL)");
    statement.executeUpdate("ALTER TABLE LOG_EXCEPTION ADD PRIMARY KEY (LOGE_ID)");
    statement.executeUpdate("ALTER TABLE LOG_EXCEPTION ADD CONSTRAINT FK_LOGE_LOG_ID"
            + " FOREIGN KEY (LOGE_LOG_ID) REFERENCES LOG (LOG_ID)");
    statement.executeUpdate(
            "CREATE TABLE LOG_EXTENSION (LOGX_LOG_ID INT NOT NULL, LOGX_TYPE VARCHAR(100) NOT NULL, LOGX_VALUE VARCHAR(100) NOT NULL)");
    statement.executeUpdate("ALTER TABLE LOG_EXTENSION ADD PRIMARY KEY (LOGX_LOG_ID)");
    statement.executeUpdate("ALTER TABLE LOG_EXTENSION ADD CONSTRAINT FK_LOGX_LOG_ID "
            + " FOREIGN KEY (LOGX_LOG_ID) REFERENCES LOG (LOG_ID)");

    statement.executeQuery("select count(*) from LOG");
    db.commit();
    db.close();
}

From source file:corner.migration.impl.DBMigrationInitializerTest.java

private void executeSchemaStatement(Statement stmt, String sql) throws SQLException {

    try {/*  w  w w.j a  v  a 2 s  .c  o  m*/
        stmt.executeUpdate(sql);
    } catch (SQLException ex) {
        ex.printStackTrace();
    }
}

From source file:geocodingissues.Main.java

public void insertNbhd(int id, String neighborhood) {
    try {//  ww w .  ja v a2 s  .  c  o m
        Statement s = connection.createStatement();

        s.executeUpdate("UPDATE issues " + "SET neighborhood = " + "'" + neighborhood + "'" + " "
                + "WHERE id = " + Integer.toString(id));
    } catch (Exception e) {
        System.out.println("Problem in updating the database neighborhood");
    }
}

From source file:geocodingissues.Main.java

public void insertLocality(int id, String locality) {
    try {/* ww  w.ja  v a2 s.  com*/
        Statement s = connection.createStatement();

        s.executeUpdate("UPDATE issues " + "SET locality = " + "'" + locality + "'" + " " + "WHERE id = "
                + Integer.toString(id));
    } catch (Exception e) {
        System.out.println("Problem in updating the database locality");
    }
}

From source file:geocodingissues.Main.java

public void insertPC(int id, String PC) {
    try {//from  w w w . ja v a2 s.co  m
        Statement s = connection.createStatement();

        s.executeUpdate(
                "UPDATE issues " + "SET postal_code = " + PC + " " + "WHERE id = " + Integer.toString(id));
    } catch (Exception e) {
        System.out.println("Problem in updating the database locality");
    }
}