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.aurel.track.dbase.Migrate502To503.java

private static void addDuration(Integer durationField, String name, String fieldType, String label,
        String tooltip) {/*  w w w  .j  a  v  a 2 s.c  o m*/
    TFieldBean durationFieldBean = FieldBL.loadByPrimaryKey(durationField);
    if (durationFieldBean == null) {
        LOGGER.info("Adding duration field " + durationField);
        Connection cono = null;
        try {
            cono = InitDatabase.getConnection();
            Statement ostmt = cono.createStatement();
            cono.setAutoCommit(false);
            ostmt.executeUpdate(addField(durationField, name, fieldType));
            ostmt.executeUpdate(addFieldConfig(durationField, durationField, label, tooltip));
            cono.commit();
            cono.setAutoCommit(true);
        } catch (Exception e) {
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        } finally {
            try {
                if (cono != null) {
                    cono.close();
                }
            } catch (Exception e) {
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
    }
}

From source file:com.espertech.esperio.db.SupportDatabaseService.java

public static void truncateTable(String tableName) throws SQLException {
    Connection connection = getConnection(PARTURL, DBUSER, DBPWD);
    connection.setAutoCommit(true);/*from   w  w w .ja va 2 s. c om*/
    Statement stmt = connection.createStatement();
    String sql = "delete from " + tableName;
    log.info("Executing sql : " + sql);
    stmt.executeUpdate(sql);
    stmt.close();
    connection.close();
}

From source file:com.funambol.pushlistener.util.DBHelper.java

/**
 * This method is used to execute the given script as statement on the
 * given connection. After the execution of the script, the connection
 * will be closed.//from   w  w w.  java 2s . co m
 * @param connection is the connection to use while executing statement.
 * @param script is the statement to execute.
 * @return the number of statements properly executed.
 * @throws java.lang.Exception if any error occurs.
 */
protected static int executeStatement(Connection connection, String script) throws Exception {
    Statement stmt = null;
    try {
        stmt = connection.createStatement();
        return stmt.executeUpdate(script);
    } catch (Exception e) {
        throw new Exception("An error occurred while executing the statement [" + script + "].", e);
    } finally {
        DBTools.close(connection, stmt, null);
    }

}

From source file:libepg.util.db.AboutDB.java

/**
* ?DB?????//from w  w  w . j  a va2s.c  om
* @param src 
* @param conn DB??
* @throws java.sql.SQLException 
* @see libepg.util.db.AboutDB#CRATE_TABLE
* @see java.sql.Connection#createStatement() 
*/
public static synchronized void convertToTable(List<TsPacket> src, Connection conn) throws SQLException {
    Statement stmt = conn.createStatement();
    //?
    stmt.executeUpdate(AboutDB.CRATE_TABLE);
    //?PID??????
    for (TsPacket tsp : src) {
        PreparedStatement insertStatement = conn.prepareStatement(INSERT_SQL);
        insertStatement.setInt(1, tsp.getPid());
        insertStatement.setInt(2, tsp.getContinuity_counter());
        insertStatement.setInt(3, 0);
        insertStatement.setBytes(4, tsp.getData());
        insertStatement.executeUpdate();
    }
}

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

/**
 * Add the document and document section
 * Use JDBC because negative objectIDs should be added
 *//*  ww w. j a  va2s .  com*/
public static void addNewItemTypes() {
    LOGGER.info("Add new item types");
    List<String> itemTypeStms = new ArrayList<String>();
    TListTypeBean docIssueTypeBean = IssueTypeBL.loadByPrimaryKey(-6);
    if (docIssueTypeBean == null) {
        LOGGER.info("Add 'Document folder' item type");
        itemTypeStms.add(addItemtype(-6, "Document folder", 6, -6, "documentFolder.png", "2009"));
    }

    Connection cono = null;
    try {
        cono = InitDatabase.getConnection();
        Statement ostmt = cono.createStatement();
        cono.setAutoCommit(false);
        for (String filterClobStmt : itemTypeStms) {
            ostmt.executeUpdate(filterClobStmt);
        }
        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));
        }
    }
    //children types
    List<TChildIssueTypeBean> childIssueTypeBeans = ChildIssueTypeAssignmentsBL
            .loadByChildAssignmentsByParent(-6);
    if (childIssueTypeBeans == null || childIssueTypeBeans.isEmpty()) {
        //document may have only document section children
        ChildIssueTypeAssignmentsBL.save(-6, -4);
    }
}

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

public static void addTaskIsMilestone() {
    TFieldBean taskIsMilestoneFieldBean = FieldBL.loadByPrimaryKey(SystemFields.INTEGER_TASK_IS_MILESTONE);
    if (taskIsMilestoneFieldBean == null) {
        LOGGER.info("Adding task is milestone field");
        Connection cono = null;// ww  w . j av a 2s . c om
        try {
            cono = InitDatabase.getConnection();
            Statement ostmt = cono.createStatement();
            cono.setAutoCommit(false);
            ostmt.executeUpdate(addField(SystemFields.INTEGER_TASK_IS_MILESTONE, "TaskIsMilestone",
                    "com.aurel.track.fieldType.types.system.check.SystemTaskIsMilestone"));
            ostmt.executeUpdate(addFieldConfig(SystemFields.INTEGER_TASK_IS_MILESTONE,
                    SystemFields.INTEGER_TASK_IS_MILESTONE, "Task is milestone", "Is milestone"));
            cono.commit();
            cono.setAutoCommit(true);
        } catch (Exception e) {
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        } finally {
            try {
                if (cono != null) {
                    cono.close();
                }
            } catch (Exception e) {
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
    }
}

From source file:net.robyf.dbpatcher.util.MySqlUtil.java

/**
 * Creates the table tracking the current version in a database.
 * /*from   w ww  .  j  a  v  a2  s. c  o m*/
 * @param connection Connection to the database
 */
public static void createDatabaseVersionTable(final Connection connection) {
    Statement stmt = null;
    try {
        stmt = connection.createStatement();
        stmt.executeUpdate("create table DATABASE_VERSION (" + "  VERSION bigint(20) not null primary key"
                + ") ENGINE=InnoDB DEFAULT CHARSET=latin1");
    } catch (SQLException sqle) {
        throw new UtilException("Error creating the database version table", sqle);
    } finally {
        DBUtil.closeStatement(stmt);
    }
}

From source file:massbank.svn.MSDBUpdateUtil.java

/**
 *
 *///from w w  w .j a v a 2s  . c o  m
public static boolean restoreDatabase(String dbName, String workCopyPath) {
    Connection con1 = SVNUtils.connectDB("");
    if (con1 == null) {
        return false;
    }
    try {
        Statement stmt = con1.createStatement();
        String sql = "CREATE DATABASE IF NOT EXISTS " + dbName;
        stmt.executeUpdate(sql);
        stmt.close();
        con1.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    String dumpFilePath = workCopyPath + File.separator + "other" + File.separator + "massbank_backup.sql";
    File file = new File(dumpFilePath);
    if (!file.exists()) {
        logger.severe("[SVNService] File not found:" + dumpFilePath);
        return false;
    }
    boolean ret = FileUtil.execSqlFile("localhost", dbName, dumpFilePath);
    if (!ret) {
        return false;
    }
    Connection con2 = SVNUtils.connectDB(dbName);
    if (con2 == null) {
        return false;
    }
    try {
        Statement stmt = con2.createStatement();
        String sql1 = "DROP TABLE IF EXISTS PEAK_HEAP";
        stmt.executeUpdate(sql1);
        String sql2 = "CREATE TABLE PEAK_HEAP(INDEX(ID),INDEX(MZ),INDEX(RELATIVE)) "
                + "ENGINE=HEAP SELECT ID,MZ,RELATIVE FROM PEAK";
        stmt.executeUpdate(sql2);
        stmt.close();
        con2.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:gridool.db.DBInsertOperation.java

private static void executeDDL(@Nonnull final Connection conn, @Nonnull final String sql) throws SQLException {
    final Statement st = conn.createStatement();
    try {/* w w w. j a va  2 s. c o m*/
        st.executeUpdate(sql);
        conn.commit();
    } finally {
        st.close();
    }
}

From source file:gridool.db.DBInsertOperation.java

private static void truncateTable(@Nonnull final Connection conn, @Nonnull final String table)
        throws SQLException {
    final Statement st = conn.createStatement();
    try {//from w  ww . jav a  2  s  .c o  m
        st.executeUpdate("DELETE FROM " + table);
        conn.commit();
    } finally {
        st.close();
    }
}