Example usage for java.sql Statement addBatch

List of usage examples for java.sql Statement addBatch

Introduction

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

Prototype

void addBatch(String sql) throws SQLException;

Source Link

Document

Adds the given SQL command to the current list of commands for this Statement object.

Usage

From source file:com.china317.gmmp.gmmp_report_analysis.App.java

private static void PtmOverSpeedRecordsStoreIntoDB(Map<String, PtmOverSpeed> overSpeedRecords,
        ApplicationContext context) {//from w w  w.ja  v a 2 s  .  c o  m
    // INSERT INTO
    // TAB_ALARM_OVERSPEED(LICENCE,BEGIN_TIME,END_TIME,SPEED,IS_END,AREA)
    // SELECT LICENSE,BEGINTIME,ENDTIME,AVGSPEED,'1',FLAG FROM
    // ALARMOVERSPEED_REA WHERE BUSINESSTYPE = '2'
    String sql = "";
    Connection conn = null;
    try {
        SqlMapClient sc = (SqlMapClient) context.getBean("sqlMapClientPtm");
        conn = sc.getDataSource().getConnection();
        conn.setAutoCommit(false);
        Statement st = conn.createStatement();
        Iterator<String> it = overSpeedRecords.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            PtmOverSpeed pos = overSpeedRecords.get(key);
            sql = "insert into TAB_ALARM_OVERSPEED " + " (LICENCE,BEGIN_TIME,END_TIME,SPEED,IS_END,AREA) "
                    + " values ('" + pos.getLicense() + "','" + pos.getBeginTime() + "','" + pos.getEndTIme()
                    + "'," + pos.getAvgSpeed() + "," + "1" + "," + pos.getFlag() + ")";
            log.info(sql);
            st.addBatch(sql);
        }
        st.executeBatch();
        conn.commit();
        log.info("[insertIntoDB OverSpeed success!!!]");
    } catch (Exception e) {
        e.printStackTrace();
        log.error(sql);
    } finally {
        overSpeedRecords.clear();
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

From source file:org.kuali.kra.infrastructure.TestUtilities.java

License:asdf

public static void clearTables(final PlatformTransactionManager transactionManager, final DataSource dataSource,
        final String edenSchemaName, final List<String> dontClear) {
    LOG.info("Clearing tables for schema " + edenSchemaName);
    if (dataSource == null) {
        Assert.fail("Null data source given");
    }//  ww w .  jav a2  s.c o m
    if (edenSchemaName == null || edenSchemaName.equals("")) {
        Assert.fail("Empty eden schema name given");
    }
    new TransactionTemplate(transactionManager).execute(new TransactionCallback() {
        public Object doInTransaction(TransactionStatus status) {
            verifyTestEnvironment(dataSource);
            JdbcTemplate template = new JdbcTemplate(dataSource);
            return template.execute(new StatementCallback() {
                public Object doInStatement(Statement statement) throws SQLException {
                    List<String> reEnableConstraints = new ArrayList<String>();
                    ResultSet resultSet = statement.getConnection().getMetaData().getTables(null,
                            edenSchemaName, null, new String[] { "TABLE" });
                    while (resultSet.next()) {
                        String tableName = resultSet.getString("TABLE_NAME");
                        if (tableName.startsWith("EN_") && !dontClear.contains(tableName)) {
                            ResultSet keyResultSet = statement.getConnection().getMetaData()
                                    .getExportedKeys(null, edenSchemaName, tableName);
                            while (keyResultSet.next()) {
                                String fkName = keyResultSet.getString("FK_NAME");
                                String fkTableName = keyResultSet.getString("FKTABLE_NAME");
                                statement.addBatch(
                                        "ALTER TABLE " + fkTableName + " DISABLE CONSTRAINT " + fkName);
                                reEnableConstraints
                                        .add("ALTER TABLE " + fkTableName + " ENABLE CONSTRAINT " + fkName);
                            }
                            keyResultSet.close();
                            statement.addBatch("DELETE FROM " + tableName.toUpperCase());
                        }
                    }
                    for (String constraint : reEnableConstraints) {
                        statement.addBatch(constraint);
                    }
                    statement.executeBatch();
                    resultSet.close();
                    return null;
                }
            });
        }
    });
    LOG.info("Tables successfully cleared for schema " + edenSchemaName);
}

From source file:com.zionex.t3sinc.util.db.SincDatabaseUtility.java

public int[] executeBatch(Statement statement, List sqlKeys, Map queryItemMap) throws SQLException {
    QueryOrganizerInterface queryOrgernizer = new QueryOrganizerImplUpdate();
    List queryList = getOrganizedQueryList(queryOrgernizer, sqlKeys, queryItemMap);
    for (Iterator iteratorQuery = queryList.iterator(); iteratorQuery.hasNext();) {
        statement.addBatch(((QueryInterface) iteratorQuery.next()).getQuery());
    }//from w w w  . java  2  s .  co  m
    return statement.executeBatch();
}

From source file:com.china317.gmmp.gmmp_report_analysis.App.java

private static void LybcOverSpeedRecordsStoreIntoDB(Map<String, PtmOverSpeed> overSpeedRecords,
        ApplicationContext context) {//from w  w  w.jav a  2  s  . co m
    // INSERT INTO
    // TAB_ALARM_OVERSPEED(LICENCE,BEGIN_TIME,END_TIME,SPEED,IS_END,AREA,MAX_SPEED)
    // SELECT LICENSE,BEGINTIME,ENDTIME,AVGSPEED,'1',FLAG,MAXSPEED FROM
    // ALARMOVERSPEED_REA WHERE BUSINESSTYPE = '3'
    String sql = "";

    Connection conn = null;
    try {
        SqlMapClient sc = (SqlMapClient) context.getBean("sqlMapClientLybc");
        conn = sc.getDataSource().getConnection();
        conn.setAutoCommit(false);
        Statement st = conn.createStatement();
        Iterator<String> it = overSpeedRecords.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            PtmOverSpeed pos = overSpeedRecords.get(key);
            sql = "insert into TAB_ALARM_OVERSPEED "
                    + " (LICENCE,BEGIN_TIME,END_TIME,SPEED,IS_END,AREA,MAX_SPEED) " + " values ('"
                    + pos.getLicense() + "','" + pos.getBeginTime() + "','" + pos.getEndTIme() + "',"
                    + pos.getAvgSpeed() + "," + "1" + "," + pos.getFlag() + "," + pos.getMaxSpeed() + ")";
            log.info(sql);
            st.addBatch(sql);
        }
        st.executeBatch();
        conn.commit();
        log.info("[insertIntoDB OverSpeed success!!!]");
    } catch (Exception e) {
        e.printStackTrace();
        log.error(sql);
    } finally {
        overSpeedRecords.clear();
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

From source file:org.pentaho.reporting.engine.classic.extensions.datasources.sampledata.SampleDataModuleInitializer.java

private void populateDatabase(Driver driver) throws SQLException, IOException {
    Properties p = new Properties();
    p.setProperty("user", "sa");
    p.setProperty("password", "");
    final Connection connection = driver.connect("jdbc:hsqldb:mem:SampleData", p);
    connection.setAutoCommit(false);// ww  w. j  a  v a  2  s . co  m
    try {
        final Configuration config = ClassicEngineBoot.getInstance().getGlobalConfig();
        final String location = config.getConfigProperty(
                "org.pentaho.reporting.engine.classic.extensions.datasources.sampledata.SampleDataLocation");
        final InputStream in = SampleDataModule.class.getResourceAsStream(location);
        if (in == null) {
            logger.warn("Invalid database init-script specified. Sample database will be empty. [" + location
                    + "]");
            return;
        }
        final InputStreamReader inReader = new InputStreamReader(in);
        final BufferedReader bin = new BufferedReader(inReader);
        try {
            final Statement statement = connection.createStatement();
            try {
                String line;
                while ((line = bin.readLine()) != null) {
                    if (line.startsWith("CREATE SCHEMA ") || line.startsWith("CREATE USER SA ")
                            || line.startsWith("GRANT DBA TO SA")) {
                        // ignore the error, HSQL sucks
                    } else {
                        statement.addBatch(StringEscapeUtils.unescapeJava(line));
                    }
                }
                statement.executeBatch();
            } finally {
                statement.close();
            }
        } finally {
            bin.close();
        }

        connection.commit();
    } finally {
        connection.close();
    }
}

From source file:org.hibernatespatial.test.DataSourceUtils.java

public void insertTestData(TestData testData) throws SQLException {
    Connection cn = null;/*from www. ja  v a  2s .  c  om*/
    try {
        cn = getDataSource().getConnection();
        cn.setAutoCommit(false);
        Statement stmt = cn.createStatement();
        for (TestDataElement testDataElement : testData) {
            String sql = sqlExpressionTemplate.toInsertSql(testDataElement);
            LOGGER.debug("adding stmt: " + sql);
            stmt.addBatch(sql);
        }
        int[] insCounts = stmt.executeBatch();
        cn.commit();
        stmt.close();
        LOGGER.info("Loaded " + sum(insCounts) + " rows.");
    } finally {
        try {
            if (cn != null)
                cn.close();
        } catch (SQLException e) {
            // nothing to do
        }
    }
}

From source file:com.china317.gmmp.gmmp_report_analysis.App.java

private static void InOutMoreRecordsStoreIntoDB(Map<String, AlarmMore> iniOutMoreRecords,
        ApplicationContext context) {/*from ww  w.  j  a v a2s  .c o  m*/
    Connection conn = null;
    String sql = "";
    try {
        SqlMapClient sc = (SqlMapClient) context.getBean("sqlMapClientLybc");
        conn = sc.getDataSource().getConnection();
        conn.setAutoCommit(false);
        Statement st = conn.createStatement();
        Iterator<String> it = iniOutMoreRecords.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            AlarmMore pos = iniOutMoreRecords.get(key);
            sql = "insert into TAB_ALARM_MORE " + " (LICENCE,denoter,fisrt_Time,BEGIN_TIME,END_TIME,road) "
                    + " values (" + "'" + pos.getLicense() + "'," + "'" + pos.getDenoter() + "'," + "'"
                    + pos.getFirstTime() + "'," + "'" + pos.getBeginTime() + "'," + "'" + pos.getEndTime()
                    + "'," + "'" + pos.getRoad() + "')";
            log.info(sql);
            st.addBatch(sql);
        }
        st.executeBatch();
        conn.commit();
        log.info("[insertIntoDB TAB_ALARM_MORE success!!!]");
    } catch (Exception e) {
        e.printStackTrace();
        log.error(sql);
    } finally {
        iniOutMoreRecords.clear();
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.dianping.zebra.shard.jdbc.base.MultiDBBaseTestCase.java

private void loadDatas(List<DBDataEntry> datas) throws Exception {
    Class.forName(getDriverClassName());
    for (DBDataEntry entry : datas) {
        Connection conn = null;//  w  ww  .  j  a  v  a  2  s  .c  o m
        Statement stmt = null;
        try {
            conn = DriverManager
                    .getConnection(getDBBaseUrl() + entry.getDbName() + ";MVCC=TRUE;DB_CLOSE_DELAY=-1");
            stmt = conn.createStatement();
            int count = 0;
            for (String script : entry.getScripts()) {
                stmt.addBatch(script);
                count++;
            }

            if (count > 0) {
                stmt.executeBatch();
                stmt.clearBatch();
            }
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception e) {

                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {

                }
            }
        }
    }

}

From source file:com.china317.gmmp.gmmp_report_analysis.App.java

private static void FatigueRecordsStoreIntoDB(Map<String, FatigueAlarmEntity> map, ApplicationContext context) {
    /**//from w  w w.  j  a v a 2s .  c  o  m
     * INSERT INTO TAB_GPSEVENT_FATIGUE SELECT
     * LICENCE,'',ALARMSTARTTIME,ALARMENDTIME,'' FROM ALARMFATIGUE_REA
     */
    Connection conn = null;
    String sql = "";
    try {
        log.info("--------store Fatigue");
        SqlMapClient sc = (SqlMapClient) context.getBean("sqlMapClientDgm");
        conn = sc.getDataSource().getConnection();
        conn.setAutoCommit(false);
        Statement st = conn.createStatement();
        Iterator<String> it = map.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            FatigueAlarmEntity pos = map.get(key);
            sql = "insert into TAB_GPSEVENT_FATIGUE "
                    + " (license,license_color,start_time,end_time,pointcount) " + " values (" + "'"
                    + pos.getLicence() + "'," + "'" + pos.getLicenceColor() + "'," + "'"
                    + pos.getAlarmStartTime() + "'," + "'" + pos.getAlarmEndTime() + "'," + "'"
                    + pos.getPointCount() + "')";
            log.info(sql);
            st.addBatch(sql);
        }
        st.executeBatch();
        conn.commit();
        log.info("[insertIntoDB FatigueAlarmEntity success!!!]");
    } catch (Exception e) {
        log.info(e);
        log.error(sql);
    } finally {
        DgmAnalysisImp.getInstance().getFatigueMap().clear();
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.china317.gmmp.gmmp_report_analysis.App.java

private static void DgmOverSpeedRecordsStoreIntoDB(Map<String, PtmOverSpeed> overSpeedRecords,
        ApplicationContext context) {/* www  . ja  v a2s  . c  o m*/
    // INSERT INTO
    // TAB_GPSEVENT_OVERSPEED(VID,TYPE,,BEGIN_TIME,END_TIME,DETAIL,AREA,MAX_SPEED,MIN_SPEED)
    // SELECT CODE,'overspeed',BEGINTIME,ENDTIME,AVGSPEED,FLAG,MAXSPEED,''
    // FROM ALARMOVERSPEED_REA WHERE BUSINESSTYPE = '1'

    String sql = "";
    Connection conn = null;
    try {
        SqlMapClient sc = (SqlMapClient) context.getBean("sqlMapClientDgm");
        conn = sc.getDataSource().getConnection();
        conn.setAutoCommit(false);
        Statement st = conn.createStatement();
        Iterator<String> it = overSpeedRecords.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            PtmOverSpeed pos = overSpeedRecords.get(key);
            sql = "INSERT INTO TAB_GPSEVENT_OVERSPEED (VID,TYPE,BEGIN_TIME,END_TIME,DETAIL,AREA,MAX_SPEED,MIN_SPEED) "
                    + " values ('" + pos.getCode() + "','" + "overspeed" + "','" + pos.getBeginTime() + "','"
                    + pos.getEndTIme() + "'," + pos.getAvgSpeed() + "," + pos.getFlag() + ","
                    + pos.getMaxSpeed() + "," + 0 + ")";
            log.info(sql);
            st.addBatch(sql);
        }
        st.executeBatch();
        conn.commit();
        log.info("[insertIntoDB OverSpeed success!!!]");
    } catch (Exception e) {
        e.printStackTrace();
        log.error(sql);
    } finally {
        overSpeedRecords.clear();
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}