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:com.l2jfree.gameserver.util.IdFactory.java

private static void removeLeftover() {
    int removed = 0;

    Connection con = null;//from w  w  w  . j  a  v  a 2s. c om
    try {
        con = L2Database.getConnection();

        final Statement st = con.createStatement();

        for (String query : REMOVE_LEFTOVER_QUERIES) {
            removed += st.executeUpdate(query);
        }

        st.close();
    } catch (SQLException e) {
        _log.warn("", e);
    } finally {
        L2Database.close(con);
    }

    _log.info("IdFactory: Removed " + removed + " leftover entries from database.");
}

From source file:net.pms.database.TableMusicBrainzReleases.java

/**
 * This method <strong>MUST</strong> be updated if the table definition are
 * altered. The changes for each version in the form of
 * <code>ALTER TABLE</code> must be implemented here.
 *
 * @param connection the {@link Connection} to use
 * @param currentVersion the version to upgrade <strong>from</strong>
 *
 * @throws SQLException//from ww w  . j  a v  a2 s.  c  om
 */
private static void upgradeTable(final Connection connection, final int currentVersion) throws SQLException {
    LOGGER.info("Upgrading database table \"{}\" from version {} to {}", TABLE_NAME, currentVersion,
            TABLE_VERSION);
    tableLock.writeLock().lock();
    try {
        for (int version = currentVersion; version < TABLE_VERSION; version++) {
            LOGGER.trace("Upgrading table {} from version {} to {}", TABLE_NAME, version, version + 1);
            switch (version) {
            case 1:
                // Version 2 increases the size of ARTIST; ALBUM, TITLE and YEAR.
                Statement statement = connection.createStatement();
                statement.executeUpdate("ALTER TABLE " + TABLE_NAME + " ALTER COLUMN ARTIST VARCHAR(1000)");
                statement.executeUpdate("ALTER TABLE " + TABLE_NAME + " ALTER COLUMN ALBUM VARCHAR(1000)");
                statement.executeUpdate("ALTER TABLE " + TABLE_NAME + " ALTER COLUMN TITLE VARCHAR(1000)");
                statement.executeUpdate("ALTER TABLE " + TABLE_NAME + " ALTER COLUMN YEAR VARCHAR(20)");
                break;
            default:
                throw new IllegalStateException("Table \"" + TABLE_NAME
                        + "is missing table upgrade commands from version " + version + " to " + TABLE_VERSION);
            }
        }
        setTableVersion(connection, TABLE_NAME, TABLE_VERSION);
    } finally {
        tableLock.writeLock().unlock();
    }
}

From source file:biz.source_code.miniConnectionPoolManager.TestMiniConnectionPoolManager.java

private static void execSql(Connection conn, String sql) throws SQLException {
        Statement st = null;
        try {/*from w w  w. j  a  va2s .com*/
            st = conn.createStatement();
            st.executeUpdate(sql);
        } finally {
            if (st != null)
                st.close();
        }
    }

From source file:com.aurel.track.dbase.Migrate500To502.java

/**
 * Add inline linked link type//from  w w  w.j a  v a 2 s  . c o m
 */
static void addInlineLinkedLinkType() {
    List<TLinkTypeBean> inlinelinkTypeBeans = LinkTypeBL
            .loadByLinkType(InlineLinkType.getInstance().getPluginID(), null);
    if (inlinelinkTypeBeans == null || inlinelinkTypeBeans.isEmpty()) {
        TLinkTypeBean linkTypeBean = LinkTypeBL.loadByPrimaryKey(13);
        if (linkTypeBean == null) {
            LOGGER.info("Add 'inline linked' link type with fixed ID");
            Connection cono = null;
            try {
                cono = InitDatabase.getConnection();
                Statement ostmt = cono.createStatement();
                cono.setAutoCommit(false);
                String inlineLinkTypeStmt = addInlineLinkTypeStmt(13, "is inline linked in",
                        "the filtered items are inline linked to", LINK_DIRECTION.RIGHT_TO_LEFT,
                        InlineLinkType.class.getName(), "0123456789");
                ostmt.executeUpdate(inlineLinkTypeStmt);
                cono.commit();
                cono.setAutoCommit(true);
            } catch (Exception e) {
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            } finally {
                try {
                    if (cono != null) {
                        cono.close();
                    }
                } catch (Exception e) {
                    LOGGER.info("Closing the connection failed with " + e.getMessage());
                    LOGGER.debug(ExceptionUtils.getStackTrace(e));
                }
            }
        } else {
            LOGGER.info("Add 'inline linked' link type at end");
            UpgradeDatabase.addLinkType("is inline linked in", "the filtered items are inline linked to",
                    LINK_DIRECTION.RIGHT_TO_LEFT, InlineLinkType.class.getName());
        }
    }
}

From source file:hnu.helper.DataBaseConnection.java

/**
 * Executes a SQL-String and returns true if everything went ok.
 * Can be used for DML and DDL. <br />Pete, do not use with SELECTs.
 *///from www. j ava  2 s  .  c om
public static boolean execute(String sql) {
    DataBaseConnection db = new DataBaseConnection();
    boolean returnValue = false;
    Connection conn = db.getDBConnection();
    Statement stmt = null;

    log.debug("About to execute: " + sql);
    try {
        stmt = conn.createStatement();
        stmt.executeUpdate(sql);
        log.debug(".. executed without exception (hope to return 'true') ");
        returnValue = true;
        //stmt.close();  - why do this here and in finally?
        //conn.close();
    } catch (SQLException ex) {
        log.error("Error executing: '" + sql + "'", ex);
    } finally {
        try {
            stmt.close();
        } catch (Exception ex) {
            log.error("Couldn't close the statement (so returning 'false').", ex);
            returnValue = false;
        } finally {
            // irrespective of whether closing the statement succeeds
            try {
                conn.close();
            } catch (SQLException ex) {
                log.error("Couldn't close the connection (so returning 'false').", ex);
                returnValue = false;
            }
        }
    }

    return returnValue;
}

From source file:com.trackplus.ddl.DataWriter.java

private static void executeUpdate(Connection con, Statement stmt, String s) throws DDLException {
    try {//from w  ww.j  a  v  a 2s.co m
        stmt.executeUpdate(s);
    } catch (SQLException e) {
        LOGGER.error("Error execute script line:" + e.getMessage());
        LOGGER.warn("-------------\n\n");
        LOGGER.warn(s);
        LOGGER.warn("-------------\n\n");
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        try {
            stmt.close();
            con.rollback();
            con.setAutoCommit(true);
        } catch (SQLException ex) {
            throw new DDLException(e.getMessage(), e);
        }
        throw new DDLException(e.getMessage(), e);
    }
}

From source file:ca.sqlpower.persistance.CatNap.java

public static void delete(Connection con, String tableName, String where)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, SQLException {
    Statement stmt = null;
    StringBuffer sql = new StringBuffer();
    try {//from   ww  w.  j  a  v a2s  .c  o m
        sql.append("DELETE FROM " + tableName);
        sql.append(" WHERE " + where);
        sql.append("\n)");

        stmt = con.createStatement();
        stmt.executeUpdate(sql.toString());
    } catch (SQLException ex) {
        System.err.println("Catnap: Insert failed. Statement was:\n" + sql);
        throw ex;
    } finally {
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException ex) {
            System.err.println(
                    "Catnap: Couldn't close the statement.  Damn.  But at least you won a stack trace:");
            ex.printStackTrace();
        }
    }
}

From source file:edu.ku.brc.dbsupport.SchemaUpdateService.java

/**
 * @param conn//w  ww.j a  va  2 s  . c o  m
 * @param fileName
 * @return
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public static boolean createDBTablesFromSQLFile(final Connection conn, final String fileName) throws Exception {
    File outFile = XMLHelper.getConfigDir(fileName);
    if (outFile != null && outFile.exists()) {
        StringBuilder sb = new StringBuilder();
        Statement stmt = conn.createStatement();
        List<?> list = FileUtils.readLines(outFile);

        for (String line : (List<String>) list) {
            String tLine = line.trim();
            sb.append(tLine);

            if (tLine.endsWith(";")) {
                System.out.println(sb.toString());
                stmt.executeUpdate(sb.toString());
                sb.setLength(0);
            }
        }
        stmt.close();
        return true;
    }
    return false;
}

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

public static void createDatabase(Connection connArg, String dbNameArg, String dbmsArg) {

    if (dbmsArg.equals("mysql")) {
        try {//from   ww  w. j a v  a2  s  .co  m
            Statement s = connArg.createStatement();
            String newDatabaseString = "CREATE DATABASE IF NOT EXISTS " + dbNameArg;
            // String newDatabaseString = "CREATE DATABASE " + dbName;
            s.executeUpdate(newDatabaseString);

            System.out.println("Created database " + dbNameArg);
        } catch (SQLException e) {
            printSQLException(e);
        }
    }
}

From source file:Main.java

public static void InsertTable1(Statement stmt, String package_name, String app_name, String decode_app,
        String has_sdk, String has_sdk_pro, String app_version, String app_url, String app_icon) {
    String sql = "INSERT INTO app_info.`pack_only_copy_8.12_copy`(package_name,app_name,decode_app,has_sdk,has_sdk_pro,app_version,app_url,app_icon)\n"
            + "VALUES('" + package_name + "','" + app_name + "','" + decode_app + "','" + has_sdk + "','"
            + has_sdk_pro + "','" + app_version + "','" + app_url + "','" + app_icon + "')";
    try {//from w w  w.ja  v a  2s. c  o m
        stmt.executeUpdate(sql);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}