public void executeBatch(List<Entity> entities) throws SQLException { try ( Connection connection = dataSource.getConnection(); PreparedStatement statement = connection.prepareStatement(SQL); ) { for (Entity entity : entities) { statement.setObject(1, entity.getSomeProperty()); // ... statement.addBatch(); } statement.executeBatch(); } }
public void save(List<Entity> entities) throws SQLException { try ( Connection connection = database.getConnection(); PreparedStatement statement = connection.prepareStatement(SQL_INSERT); ) { int i = 0; for (Entity entity : entities) { statement.setString(1, entity.getSomeProperty()); // ... statement.addBatch(); i++; if (i % 1000 == 0 || i == entities.size()) { statement.executeBatch(); // Execute every 1000 items. } } } }
PreparedStatement ps = connection.prepareStatement(query); for (Record record : arraylist1) { int index=1; ps.setString(index++,record.getItem()); ps.setString(index++,record.getItem2()); // } ps.executeBatch(); conn.commit();
connection.setAutoCommit(false); PreparedStatement ps = connection.prepareStatement(query); for (Record record : records) { // etc. ps.addBatch(); } ps.executeBatch(); connection.commit();
Connection conn = getConnection(); List items = getItems(); for (ItemType item : items) { String UPDATE_STMT = "UPDATE MY_TABLE SET "; if (item.x != null) {UPDATE_STMT += "col1 = ?, ";} UPDATE_STMT += "col2 = ?, col3 = ?"; PreparedStatement pstmt = conn.preparedStatement(UPDATE_STMT); int count = 0; if (item.x != null) {pstmt.setString(++count, item.x);} pstmt.setString(++count, item.y); pstmt.setString(++count, item.z); pstmt.executeBatch(); }
String myConnectionString = "jdbc:mysql://localhost:3307/mydb?" + "useUnicode=true&characterEncoding=UTF-8"; try (Connection con = DriverManager.getConnection(myConnectionString, "root", "whatever")) { try (PreparedStatement ps = con.prepareStatement("INSERT INTO jdbc (`name`) VALUES (?)")) { for (int i = 1; i <= 5; i++) { ps.setString(1, String.format( "Line %d: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", i)); ps.addBatch(); } ps.executeBatch(); } }
public void executeBatch(List<Entity> entities) throws SQLException { try ( Connection connection = dataSource.getConnection(); PreparedStatement statement = connection.prepareStatement(SQL); ) { int i = 0; for (Entity entity : entities) { statement.setObject(1, entity.getSomeProperty()); // ... statement.addBatch(); i++; if (i % 1000 == 0 || i == entities.size()) { statement.executeBatch(); // Execute every 1000 items. } } } }
"insert into people values (?, ?);"); prep.setString(1, "Gandhi"); prep.setString(2, "politics"); prep.addBatch(); prep.setString(1, "Turing"); prep.setString(2, "computers"); prep.addBatch(); prep.setString(1, "Wittgenstein"); prep.setString(2, "smartypants"); prep.addBatch(); prep.executeBatch(); conn.setAutoCommit(true);
String query1 = "select substr(to_char(max_data),1,4) as year, " + "substr(to_char(max_data),5,6) as month, max_data " + "from dss_fin_user.acq_dashboard_src_load_success " + "where source = 'CHQ PeopleSoft FS'"; String query2 = "....."; String sql = "insert into domo_queries (clob_column) values (?)"; PreparedStatement pstmt = con.prepareStatement(sql); StringReader reader = new StringReader(query1); pstmt.setCharacterStream(1, reader, query1.length()); pstmt.addBatch(); reader = new StringReader(query2); pstmt.setCharacterStream(1, reader, query2.length()); pstmt.addBatch(); pstmt.executeBatch(); con.commit();
statement.setInt(1, i); statement.setFloat(2, i); statement.setString(3, valueOf(i)); statement.execute(); statement.close(); statement.setInt(1, i); statement.setFloat(2, i); statement.setString(3, valueOf(i)); statement.addBatch(); statement.executeBatch(); statement.close();
PreparedStatement pstmt = connection.prepareStatement( "UPDATE table SET field=? WHERE id=?"); for (int i = 0; i < 100; i++) { pstmt.setInt(1, i * i); pstmt.setInt(2, i); pstmt.addBatch(); } pstmt.executeBatch(); pstmt.close();
Connection connection = null; PreparedStatement statement = null; try { connection = database.getConnection(); statement = connection.prepareStatement(SQL); for (int i = 0; i < items.size(); i++) { Item item = items.get(i); statement.setString(1, item.getSomeValue()); // ... statement.addBatch(); if ((i + 1) % 100 == 0) { statement.executeBatch(); // Execute every 100 items. } } statement.executeBatch(); } finally { if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {} if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {} }
public void update(List<Item> items) throws SQLException { Connection connection = null; PreparedStatement statement = null; try { connection = database.getConnection(); statement = connection.prepareStatement(sql); for (Item item : items) { statement.setObject(1, item.getSomething()); statement.addBatch(); } statement.executeBatch(); } finally { if (statement != null) try { statement.close(); } catch (SQLException ignore) {} if (connection != null) try { connection.close(); } catch (SQLException ignore) {} } }
conn = DriverManager.getConnection(getUrl(), props); pstmt = conn.prepareStatement("upsert into " + tableName + " values (?, ?)"); pstmt.setString(1, "a"); pstmt.setInt(2, 1); pstmt.addBatch(); pstmt.setString(1, "b"); pstmt.setInt(2, 2); pstmt.addBatch(); pstmt.executeBatch(); conn.commit(); assertTrue(rs.next()); assertEquals(2, rs.getInt(1)); int[] result = stmt.executeBatch(); assertEquals(3,result.length); assertEquals(result[0], 1);
public void copy(String table, Connection from, Connection to) throws SQLException { try (PreparedStatement s1 = from.prepareStatement("select * from " + table); ResultSet rs = s1.executeQuery()) { ResultSetMetaData meta = rs.getMetaData(); List<String> columns = new ArrayList<>(); for (int i = 1; i <= meta.getColumnCount(); i++) columns.add(meta.getColumnName(i)); try (PreparedStatement s2 = to.prepareStatement( "INSERT INTO " + table + " (" + columns.stream().collect(Collectors.joining(", ")) + ") VALUES (" + columns.stream().map(c -> "?").collect(Collectors.joining(", ")) + ")" )) { while (rs.next()) { for (int i = 1; i <= meta.getColumnCount(); i++) s2.setObject(i, rs.getObject(i)); s2.addBatch(); } s2.executeBatch(); } } }
PreparedStatement ps = conn.prepareStatement(sql); int count = 0; for(loop construct here) { count++; ps.setString(1,someString); // ... and so forth ps.addBatch(); if (count%500 ==0) ps.executeBatch() } ps.executeBatch();
aConn.setAutoCommit(false); PreparedStatement prep = aConn.prepareStatement( "insert into JCNAClasses ('division', 'class', 'description', 'note', 'node') values ( ?, ?, ?, ?, ?);"); while ((nextLine = reader.readNext()) != null) { if (iLine > 0){ // some preparations go here prep.addBatch(); } iLine++; } prep.executeBatch(); aConn.commit();
connection.setAutoCommit(false); PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO USER (firstName, secondName) VALUES(?,?)"); for (int i = 0; i < 1000000; i++) { preparedStatement.setString(1, "firstName"); preparedStatement.setString(2, "secondName"); preparedStatement.addBatch(); if(i % 1000 == 0) preparedStatement.executeBatch(); } preparedStatement.executeBatch(); connection.commit();
PreparedStatement insert1 = con.prepareStatement(...); PreparedStatement insert2 = con.prepareStatement(...); for(DTO dto : dtos) { prepareFirstInsertWithParameters(insert1, dto); prepareSecondInsertWithParameters(insert2, dto); insert1.addBatch(); insert2.addBatch(); } insert1.executeBatch(); insert2.executeBatch(); // cleanup