Example usage for java.sql Statement executeBatch

List of usage examples for java.sql Statement executeBatch

Introduction

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

Prototype

int[] executeBatch() throws SQLException;

Source Link

Document

Submits a batch of commands to the database for execution and if all commands execute successfully, returns an array of update counts.

Usage

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

private static void DgmOverSpeedRecordsStoreIntoDB(Map<String, PtmOverSpeed> overSpeedRecords,
        ApplicationContext context) {/*from ww  w  . j  ava  2 s.  c om*/
    // 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();
            }
        }
    }

}

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

public void insertTestData(TestData testData) throws SQLException {
    Connection cn = null;/*from w w w  .j a v a 2s .  c o m*/
    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.taobao.tddl.jdbc.group.TGroupStatement.java

private int[] executeBatchOnConnection(Connection conn, List<String> batchedSqls) throws SQLException {
    Statement stmt = createStatementInternal(conn, true);
    for (String sql : batchedSqls) {
        stmt.addBatch(sql);/*  w  w w  .  j  av  a  2s. co m*/
    }
    return stmt.executeBatch();
}

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 .  j  a  v  a2s . com
    return statement.executeBatch();
}

From source file:org.opencron.server.dao.HibernateDao.java

@Transactional(readOnly = false)
public void executeBatch(final String[] sqlList) {
    getSession().doWork(new Work() {

        public void execute(Connection connection) throws SQLException {
            connection.setAutoCommit(false);
            Statement stmt = connection.createStatement();
            for (String sql : sqlList) {
                stmt.addBatch(sql);/*w  w w  .ja  v  a2 s .co  m*/
            }
            stmt.executeBatch();
            connection.commit();
        }
    });
}

From source file:org.hibernate.spatial.testing.DataSourceUtils.java

public void insertTestData(TestData testData) throws SQLException {
    Connection cn = null;//from w  w  w  . j  a  v  a2 s  . com
    try {
        cn = getDataSource().getConnection();
        cn.setAutoCommit(false);
        Statement stmt = cn.createStatement();
        for (TestDataElement testDataElement : testData) {
            String sql = sqlExpressionTemplate.toInsertSql(testDataElement);
            LOG.debug("adding stmt: " + sql);
            stmt.addBatch(sql);
        }
        int[] insCounts = stmt.executeBatch();
        cn.commit();
        stmt.close();
        LOG.info("Loaded " + sum(insCounts) + " rows.");
    } catch (SQLException e) {
        e.printStackTrace();
        throw e;
    } finally {
        try {
            if (cn != null) {
                cn.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 2s . 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: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;/*from w w w  .j  a v  a2 s  .co  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:org.pentaho.reporting.engine.classic.core.testsupport.TestSetupModule.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);//from  w ww .  ja va  2s . c  o m
    if (isValid(connection)) {
        // both the test-module here and the sample-data module try to initialize the database.
        // lets do it only once.
        return;
    }
    try {
        final InputStream in = new FileInputStream("target/test-classes/sql/sampledata.script");
        final InputStreamReader inReader = new InputStreamReader(in, "ISO-8859-1");
        final BufferedReader bin = new BufferedReader(inReader, 4096);
        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();
    } catch (FileNotFoundException fe) {
        DebugLog.log("Unable to populate test database, no script at ./sql/sampledata.script");
    } finally {
        connection.close();
    }
}

From source file:com.edgenius.wiki.installation.DBLoader.java

public void runSQLFile(String type, String filename, ConnectionProxy con) throws SQLException, IOException {
    Statement stat = null;
    try {//from www  .j a v  a  2 s .c o m
        stat = con.createStatement();
        List<String> lines = loadSQLFile(type, filename);
        for (String sql : lines) {
            stat.addBatch(sql);
        }
        stat.executeBatch();
    } finally {
        if (stat != null)
            stat.close();
    }
}