Example usage for java.sql Statement clearBatch

List of usage examples for java.sql Statement clearBatch

Introduction

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

Prototype

void clearBatch() throws SQLException;

Source Link

Document

Empties this Statement object's current list of SQL commands.

Usage

From source file:bizlogic.Sensors.java

public static void del(Connection DBcon, String deleteSensors) throws SQLException {

    Statement st = null;
    ResultSet rs = null;/*ww w  .  j a v a2s .c o  m*/

    String sql_statement;

    String _deleteSensors = deleteSensors.replace(";", ",");

    st = DBcon.createStatement();
    //for(int i = 0; i<30; i++) {
    sql_statement = "DELETE FROM USERCONF.SENSORLIST WHERE SENSOR_ID IN(" + _deleteSensors + ")";
    System.out.println(sql_statement);
    st.clearBatch();
    st = DBcon.createStatement();
    DBcon.createStatement();
    st.executeUpdate(sql_statement);

}

From source file:bizlogic.Records.java

public static void del(Connection DBcon, String deleteRecords) throws SQLException {

    Statement st;

    String sql_statement;/* www.j  a v  a2  s.  c o  m*/

    String _deleteRecords = deleteRecords.replace(";", ",");

    st = DBcon.createStatement();
    //for(int i = 0; i<30; i++) {
    sql_statement = "DELETE FROM USERCONF.LOG_LIST WHERE LOG_ID IN(" + _deleteRecords + ")";
    System.out.println(sql_statement);
    st.clearBatch();
    st = DBcon.createStatement();
    DBcon.createStatement();
    st.executeUpdate(sql_statement);

}

From source file:bizlogic.Sensors.java

public static void add(Connection DBcon, String name, String IP, String operation, String operand, String unit,
        String port) throws SQLException {

    String _operation = operation;
    Statement st = null;
    ResultSet rs = null;/*ww w .  ja va  2  s.c  o  m*/

    switch (operation) {
    case "Add":
        _operation = "+";
        break;
    case "Subtract":
        _operation = "-";
        break;
    case "Multiply":
        _operation = "*";
        break;
    case "Divide":
        _operation = "/";
        break;
    default:
        break;

    }

    String sql_statement;

    st = DBcon.createStatement();
    //for(int i = 0; i<30; i++) {
    sql_statement = "INSERT INTO USERCONF.SENSORLIST(OPERATION, OPERAND, NAME, \"IP\", UNIT, PORT) "
            + "VALUES (" + "'" + _operation + "'" + ", " + "'" + operand + "'" + ", " + "'" + name + "'" + ", "
            + "'" + IP + "'" + ", " + "'" + unit + "'" + ", " + "'" + port + "'" + " );";
    System.out.println(sql_statement);
    st.clearBatch();
    st = DBcon.createStatement();
    DBcon.createStatement();
    st.executeUpdate(sql_statement);

}

From source file:bizlogic.Records.java

public static void add(Connection DBcon, String sensor_name, String smpl_interval, String running, String name)
        throws SQLException {

    String isRunning;//from   w  w  w .  j  a  v  a  2  s  . c  om
    Statement st;
    ResultSet rs = null;

    try {
        st = DBcon.createStatement();
        rs = st.executeQuery("SELECT * FROM USERCONF.SENSORLIST WHERE NAME = '" + sensor_name + "' ");

    } catch (SQLException ex) {
        Logger lgr = Logger.getLogger(Records.class.getName());
        lgr.log(Level.SEVERE, ex.getMessage(), ex);
    }
    rs.next();
    int id = rs.getInt("sensor_id");

    String sql_statement;
    if (running.equals("true")) {
        isRunning = "B'1'";
    } else {
        isRunning = "B'0'";
    }

    st = DBcon.createStatement();
    sql_statement = "INSERT INTO USERCONF.LOG_LIST(SENSOR_ID, SMPL_INTERVAL, RUNNING, NAME) " + "VALUES (" + id
            + ", " + smpl_interval + ", " + isRunning + ", " + "'" + name + "'" + " );";
    System.out.println(sql_statement);
    st.clearBatch();
    st = DBcon.createStatement();
    DBcon.createStatement();
    st.executeUpdate(sql_statement);

}

From source file:org.rti.zcore.dar.utils.DatabaseUtils.java

/**
 * Execute sql batch file with line-break-separated statements
 * @param conn/*from www . j  av a  2 s .  com*/
 * @param statements
 * @return
 * @throws Exception
 */
public static int[] executeQueryBatch(Connection conn, String statements) throws Exception {

    int[] updateCounts;
    try {
        Statement s = conn.createStatement();
        String[] sqlArray = statements.split("\n");
        for (int i = 0; i < sqlArray.length; i++) {
            String sql = sqlArray[i];
            s.addBatch(sql);
        }
        updateCounts = s.executeBatch();
        s.clearBatch();
        s.close();
    } catch (Exception ex) {
        log.info("Sql: " + statements);
        log.error(ex);
        throw new ServletException("Cannot retrieve results:", ex);
    }
    return updateCounts;
}

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 w  w .  ja  v  a 2  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:com.dianping.zebra.shard.jdbc.base.MultiDBBaseTestCase.java

private void createTables() throws Exception {
    Class.forName(getDriverClassName());
    for (MultiCreateTableScriptEntry entry : createdTableList) {
        Connection conn = null;/*from   www.  java2 s . c  om*/
        Statement stmt = null;
        try {
            conn = DriverManager
                    .getConnection(getDBBaseUrl() + entry.getDbName() + ";MVCC=TRUE;DB_CLOSE_DELAY=-1");
            stmt = conn.createStatement();
            int count = 0;
            for (Map.Entry<String, String> table : entry.getTableNameScriptMapping().entrySet()) {
                stmt.addBatch(table.getValue());
                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.dianping.zebra.shard.jdbc.base.MultiDBBaseTestCase.java

@After
public void tearDown() throws Exception {
    Class.forName(getDriverClassName());
    for (MultiCreateTableScriptEntry entry : createdTableList) {
        Connection conn = null;/*from   ww w.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 (Map.Entry<String, String> table : entry.getTableNameScriptMapping().entrySet()) {
                stmt.addBatch(" drop table " + table.getKey());
                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.thinkmore.framework.orm.hibernate.SimpleHibernateDao.java

/**
 * ?/*from  w ww.  ja  v  a 2  s  . c  o  m*/
 * @param list sql?
 */
public void executeBatch(final List<String> list) {
    Connection conn = null;
    Statement st = null;
    try {
        conn = SessionFactoryUtils.getDataSource(getSessionFactory()).getConnection();
        conn.setAutoCommit(false); // ??
        st = conn.createStatement();
        for (int i = 1, j = list.size(); i < j; i++) {
            String sql = list.get(i);
            st.addBatch(sql);
            if (i % 240 == 0) {//?240?sql???
                st.executeBatch();
                conn.commit();
                st.clearBatch();
            } else if (i % j == 0) {//??
                st.executeBatch();
                conn.commit();
                st.clearBatch();
            }
        }
    } catch (Exception e) {
        try {
            conn.rollback();
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        e.printStackTrace();
    } finally {
        closeAll(st, conn);
    }
}

From source file:edu.ku.brc.specify.tasks.RecordSetTask.java

/**
 * Delete a record set//from   w w  w .  jav a2  s.  co  m
 * @param rsController the recordSet to be deleted
 */
public static void deleteItems(final Integer rsId, final List<Integer> rsiIds, final Integer rsiId) {
    // Deleting this manually because the RecordSet may not be loaded (with Hibernate)
    // and the items are loaded EAGER, and there is not reason to take all the time (and memory)
    // to load them all just to delete them.
    // So doing this manually with JDBC is the faster way.
    Connection connection = null;
    Statement updateStatement = null;
    try {
        connection = DBConnection.getInstance().createConnection();
        StringBuilder sb = new StringBuilder("DELETE FROM recordsetitem WHERE RecordSetID = ");
        sb.append(rsId);
        sb.append(" AND RecordID ");
        if (rsiIds != null) {
            sb.append("in (");
            for (int i = 0; i < rsiIds.size(); i++) {
                if (i > 0)
                    sb.append(',');
                sb.append(rsiIds.get(i));
            }
            sb.append(")");
        } else {
            sb.append("= " + rsiId);
        }
        updateStatement = connection.createStatement();
        log.debug(sb.toString());
        int numItems = updateStatement.executeUpdate(sb.toString());
        log.debug(numItems + "  " + sb.toString());
        updateStatement.clearBatch();
        updateStatement.close();

    } catch (Exception ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(RecordSetTask.class, ex);
        ex.printStackTrace();

    } finally {
        try {
            if (updateStatement != null) {
                updateStatement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (Exception e) {
            edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
            edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(RecordSetTask.class, e);
            e.printStackTrace();
        }
    }
}