List of usage examples for java.sql Statement addBatch
void addBatch(String sql) throws SQLException;
Statement
object. 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);// ww w .ja v a 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:org.hibernate.spatial.testing.DataSourceUtils.java
public void insertTestData(TestData testData) throws SQLException { Connection cn = null;/*from ww w . j av a2s. c o m*/ 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.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 a 2 s .c o m*/ stmt.executeBatch(); connection.commit(); } }); }
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); }/*from www. java2 s . c o m*/ return stmt.executeBatch(); }
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 ww w . j a v a 2s. 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(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;/* w w 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:org.freebxml.omar.server.persistence.rdb.UsageParameterDAO.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 usageParams) throws RegistryException { Statement stmt = null; if (usageParams.size() == 0) { return; } try { stmt = context.getConnection().createStatement(); Iterator iter = usageParams.iterator(); while (iter.hasNext()) { String value = (String) iter.next(); String str = "INSERT INTO UsageParameter " + "VALUES( " + "'" + value + "', " + "'" + parentId + "' )"; log.trace("SQL = " + str); stmt.addBatch(str); } if (usageParams.size() > 0) { int[] updateCounts = 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.UsageParameterDAO.java
/** * Does a bulk insert of a Collection of objects that match the type for this persister. * *//*from w ww . j a v a 2 s. com*/ public void insert(String parentId, List<?> usageParams) throws RegistryException { Statement stmt = null; if (usageParams.size() == 0) { return; } try { stmt = context.getConnection().createStatement(); Iterator<?> iter = usageParams.iterator(); while (iter.hasNext()) { String value = (String) iter.next(); String str = "INSERT INTO UsageParameter " + "VALUES( " + "'" + value + "', " + "'" + parentId + "' )"; log.trace("stmt = " + str); stmt.addBatch(str); } if (usageParams.size() > 0) { @SuppressWarnings("unused") int[] updateCounts = stmt.executeBatch(); } } catch (SQLException e) { RegistryException exception = new RegistryException(e); throw exception; } finally { closeStatement(stmt); } }
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 {//ww w .jav a2 s.com stat = con.createStatement(); List<String> lines = loadSQLFile(type, filename); for (String sql : lines) { stat.addBatch(sql); } stat.executeBatch(); } finally { if (stat != null) stat.close(); } }
From source file:org.kuali.kpme.core.util.LoadDatabaseDataLifeCycle.java
public void loadData(final PlatformTransactionManager transactionManager, final DataSource dataSource, final String schemaName) { LOG.info("Populating tables for schema " + schemaName); Assert.assertNotNull("DataSource could not be located.", dataSource); if (schemaName == null || schemaName.equals("")) { Assert.fail("Empty schema name given"); }// w w w. java 2 s. c om new TransactionTemplate(transactionManager).execute(new TransactionCallback<Object>() { public Object doInTransaction(final TransactionStatus status) { verifyTestEnvironment(dataSource); return new JdbcTemplate(dataSource).execute(new StatementCallback<Object>() { public Object doInStatement(Statement statement) throws SQLException { if (callingTestClass != null) { List<String> sqlStatements = getTestDataSQLStatements( "src/test/config/sql/" + callingTestClass.getSimpleName() + ".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; } }); } }); }