List of usage examples for java.sql Statement executeBatch
int[] executeBatch() throws SQLException;
From source file:com.npstrandberg.simplemq.MessageQueueImp.java
public boolean delete(List<Message> messages) { try {/*from w w w . j av a 2 s . c om*/ conn.setAutoCommit(false); Statement stmt = conn.createStatement(); for (Message message : messages) { if (!(message instanceof MessageWrapper)) { throw new IllegalArgumentException("This instance of 'Message' is not valid."); } stmt.addBatch("DELETE FROM message WHERE id=" + message.getId()); } int[] updateCounts = stmt.executeBatch(); // Have we deleted them all if (updateCounts.length == messages.size()) { return true; } else { logger.error("Not all Messages was deleted! Only " + updateCounts.length + " out of " + messages.size() + " msg was deleted!"); } } catch (SQLException e) { logger.error(e); } finally { try { conn.setAutoCommit(true); } catch (SQLException e) { logger.error(e); } } return false; }
From source file:ru.tehkode.permissions.backends.hybrid.HybridBackend.java
private void executeStream(SQLConnection conn, InputStream str) throws SQLException, IOException { String deploySQL = StringUtils.readStream(str); Statement s = conn.getStatement(); for (String sqlQuery : deploySQL.trim().split(";")) { sqlQuery = sqlQuery.trim();/* w w w . j ava 2 s. c o m*/ if (sqlQuery.isEmpty()) { continue; } sqlQuery = conn.expandQuery(sqlQuery + ";"); s.addBatch(sqlQuery); } s.executeBatch(); }
From source file:org.freebxml.omar.server.persistence.rdb.EmailAddressDAO.java
/** * Does a bulk update of a Collection of objects that match the type for this persister. * *//*from w ww . j a v a 2 s. co m*/ public void update(String parentId, List emailAddresss) throws RegistryException { log.debug(ServerResourceBundle.getInstance().getString("message.UpdatingEmailAddresss", new Object[] { new Integer(emailAddresss.size()) })); Statement stmt = null; try { stmt = context.getConnection().createStatement(); Iterator iter = emailAddresss.iterator(); while (iter.hasNext()) { EmailAddress emailAddress = (EmailAddress) iter.next(); String address = emailAddress.getAddress(); String type = emailAddress.getType(); if (type != null) { type = "'" + type + "'"; } String str = "UPDATE EmailAddress SET " + //"accesControlPolicy = null, " + "SET address = '" + address + "', " + "SET type = " + type + " WHERE parent = '" + parentId + "' "; log.trace("SQL = " + str); stmt.addBatch(str); } stmt.executeBatch(); } catch (SQLException e) { RegistryException exception = new RegistryException(e); throw exception; } finally { closeStatement(stmt); } }
From source file:org.kuali.kpme.core.util.DatabaseCleanupDataLifecycle.java
public void loadData(final PlatformTransactionManager transactionManager, final DataSource dataSource, final String schemaName) { Assert.assertNotNull("DataSource could not be located.", dataSource); if (schemaName == null || schemaName.equals("")) { Assert.fail("Empty schema name given"); }/*from w w w . j a v a2s .c o m*/ new TransactionTemplate(transactionManager).execute(new TransactionCallback() { public Object doInTransaction(final TransactionStatus status) { verifyTestEnvironment(dataSource); return new JdbcTemplate(dataSource).execute(new StatementCallback() { public Object doInStatement(Statement statement) throws SQLException { List<String> sqlStatements = new ArrayList<String>(); // // djunk - add a per-class special test data loader, // loads <testclassname>.sql from the same directory // as the other SQL loaded. if (callingTestClass != null) { sqlStatements.addAll(getTestDataSQLStatements( "src/test/config/sql/" + callingTestClass.getSimpleName() + "-cleanup.sql")); } for (String sql : sqlStatements) { if (!sql.startsWith("#") && !sql.startsWith("//") && !StringUtils.isEmpty(sql.trim())) { // ignore comment lines in our sql reader. statement.addBatch(sql); } } statement.executeBatch(); return null; } }); } }); }
From source file:org.latticesoft.util.resource.DatabaseUtil.java
/** * Executes the query in batch statement * @param c the collection of SQL statements to execute * @param conn the connection/*from ww w . ja v a 2 s. c om*/ * @param closeConn true to close the connection after use * @return int[] a integer array of the executed result */ public int[] executeBatch(Collection c, Connection conn, boolean closeConn) { if (c == null || conn == null) return null; if (c.size() == 0) return null; Statement stmt = null; String sql = null; Iterator iter = c.iterator(); int[] retVal = null; try { stmt = conn.createStatement(); while (iter.hasNext()) { Object o = iter.next(); if (o == null) continue; sql = o.toString(); stmt.addBatch(sql); } retVal = stmt.executeBatch(); } catch (Exception e) { if (log.isErrorEnabled()) { log.error(e); } } finally { try { stmt.close(); } catch (Exception e) { } if (closeConn) { try { conn.close(); } catch (Exception e) { } } stmt = null; conn = null; } return retVal; }
From source file:org.freebxml.omar.server.persistence.rdb.EmailAddressDAO.java
/** * Does a bulk insert of a Collection of objects that match the type for this persister. * *///from ww w. j a v a2 s .com public void insert(String parentId, List emailAddresss) throws RegistryException { log.debug(ServerResourceBundle.getInstance().getString("message.InsertingEmailAddresss", new Object[] { new Integer(emailAddresss.size()) })); if (emailAddresss.size() == 0) { return; } Statement stmt = null; try { stmt = context.getConnection().createStatement(); Iterator iter = emailAddresss.iterator(); while (iter.hasNext()) { EmailAddressType emailAddress = (EmailAddressType) iter.next(); //Log.print(Log.TRACE, 8, "\tDATABASE EVENT: storing EmailAddress " ); String address = emailAddress.getAddress(); String type = emailAddress.getType(); if (type != null) { type = "'" + type + "'"; } String str = "INSERT INTO EmailAddress " + "VALUES( " + "'" + address + "', " + type + ", " + "'" + parentId + "' )"; log.trace("SQL = " + str); stmt.addBatch(str); } if (emailAddresss.size() > 0) { stmt.executeBatch(); } } catch (SQLException e) { RegistryException exception = new RegistryException(e); throw exception; } finally { closeStatement(stmt); } }
From source file:it.cnr.icar.eric.server.persistence.rdb.EmailAddressDAO.java
/** * Does a bulk update of a Collection of objects that match the type for this persister. * *///w ww . j a va2 s .co m public void update(String parentId, List<?> emailAddresss) throws RegistryException { log.debug(ServerResourceBundle.getInstance().getString("message.UpdatingEmailAddresss", new Object[] { new Integer(emailAddresss.size()) })); Statement stmt = null; try { stmt = context.getConnection().createStatement(); Iterator<?> iter = emailAddresss.iterator(); while (iter.hasNext()) { EmailAddressType ebEmailAddressType = (EmailAddressType) iter.next(); String address = ebEmailAddressType.getAddress(); String type = ebEmailAddressType.getType(); if (type != null) { type = "'" + type + "'"; } String str = "UPDATE EmailAddress SET " + //"accesControlPolicy = null, " + "SET address = '" + address + "', " + "SET type = " + type + " WHERE parent = '" + parentId + "' "; log.trace("stmt = " + str); stmt.addBatch(str); } stmt.executeBatch(); } catch (SQLException e) { RegistryException exception = new RegistryException(e); throw exception; } finally { closeStatement(stmt); } }
From source file:it.cnr.icar.eric.server.persistence.rdb.EmailAddressDAO.java
/** * Does a bulk insert of a Collection of objects that match the type for this persister. * *//*from w w w. j a v a 2 s . c o m*/ public void insert(String parentId, List<?> emailAddresss) throws RegistryException { log.debug(ServerResourceBundle.getInstance().getString("message.InsertingEmailAddresss", new Object[] { new Integer(emailAddresss.size()) })); if (emailAddresss.size() == 0) { return; } Statement stmt = null; try { stmt = context.getConnection().createStatement(); Iterator<?> iter = emailAddresss.iterator(); while (iter.hasNext()) { EmailAddressType emailAddress = (EmailAddressType) iter.next(); //Log.print(Log.TRACE, 8, "\tDATABASE EVENT: storing EmailAddress " ); String address = emailAddress.getAddress(); String type = emailAddress.getType(); if (type != null) { type = "'" + type + "'"; } String str = "INSERT INTO EmailAddress " + "VALUES( " + "'" + address + "', " + type + ", " + "'" + parentId + "' )"; log.trace("stmt = " + str); stmt.addBatch(str); } if (emailAddresss.size() > 0) { stmt.executeBatch(); } } catch (SQLException e) { RegistryException exception = new RegistryException(e); throw exception; } finally { closeStatement(stmt); } }
From source file:org.kuali.rice.kew.test.TestUtilities.java
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"); }/*from w w w.ja v a 2s .com*/ 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); } } 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.hangum.tadpole.importdb.core.dialog.importdb.sql.SQLToDBImportDialog.java
/** * select? execute ./* w ww . j a v a2 s .c o m*/ * * @param listQuery * @throws Exception */ private int runSQLExecuteBatch(List<String> listQuery) throws Exception { java.sql.Connection conn = null; Statement statement = null; int result = 0; try { SqlMapClient client = TadpoleSQLManager.getInstance(userDB); conn = client.getDataSource().getConnection(); conn.setAutoCommit(false); statement = conn.createStatement(); int count = 0; for (String strQuery : listQuery) { if ("".equals(StringUtils.trimToEmpty(strQuery))) //$NON-NLS-1$ continue; statement.addBatch(strQuery); if (++count % batchSize == 0) { try { statement.executeBatch(); } catch (SQLException e) { logger.error("Execute Batch error", e); //$NON-NLS-1$ bufferBatchResult.append(e.getMessage() + "\n"); //$NON-NLS-1$ SQLException ne = e.getNextException(); while (ne != null) { logger.error("NEXT SQLException is ", ne);//$NON-NLS-1$ bufferBatchResult.append(ne.getMessage() + "\n"); ne = ne.getNextException(); } if (btnIgnore.getSelection()) { conn.commit(); continue; } else { conn.rollback(); result = -1; break; } } } } statement.executeBatch(); conn.commit(); conn.setAutoCommit(true); if (result < 0 && !"".equals(bufferBatchResult.toString())) { //$NON-NLS-1$ MessageDialog.openError(null, Messages.CsvToRDBImportDialog_4, bufferBatchResult.toString()); } } catch (SQLException e) { logger.error("Execute Batch error", e); //$NON-NLS-1$ bufferBatchResult.append(e.getMessage() + "\n"); //$NON-NLS-1$ if (btnIgnore.getSelection()) { conn.commit(); } else { conn.rollback(); } SQLException ne = e.getNextException(); while (ne != null) { logger.error("Execute Batch error", e); //$NON-NLS-1$ bufferBatchResult.append(e.getMessage() + "\n"); //$NON-NLS-1$ ne = ne.getNextException(); } } catch (Exception e) { result = -1; logger.error("Execute Batch error", e); //$NON-NLS-1$ bufferBatchResult.append(e.getMessage() + "\n"); //$NON-NLS-1$ conn.rollback(); throw e; } finally { try { if (statement != null) statement.close(); } catch (Exception e) { } try { if (conn != null) conn.close(); } catch (Exception e) { } } return result; }