private void testException() throws SQLException { deleteDb("batchUpdates"); conn = getConnection("batchUpdates"); stat = conn.createStatement(); stat.execute("create table test(id int primary key)"); prep = conn.prepareStatement("insert into test values(?)"); for (int i = 0; i < 700; i++) { prep.setString(1, "x"); prep.addBatch(); } try { prep.executeBatch(); } catch (BatchUpdateException e) { PrintStream temp = System.err; try { ByteArrayOutputStream buff = new ByteArrayOutputStream(); PrintStream p = new PrintStream(buff); System.setErr(p); e.printStackTrace(); } finally { System.setErr(temp); } } conn.close(); }
public void run() { try { PreparedStatement pstmt = con.prepareStatement( "insert into #testConcurrentBatch (v1, v2, v3, v4, v5, v6) values (?, ?, ?, ?, ?, ?)"); for (int i = 0; i < 64; ++i) { // Make sure we end up with 64 different prepares, use the binary representation of i to // set each // of the 6 parameters to either an int or a string. int mask = i; for (int j = 1; j <= 6; ++j, mask >>= 1) { if ((mask & 1) != 0) { pstmt.setInt(j, i); } else { pstmt.setString(j, String.valueOf(i)); } } pstmt.addBatch(); } int x[]; try { x = pstmt.executeBatch(); } catch (BatchUpdateException e) { e.printStackTrace(); x = e.getUpdateCounts(); } if (x.length != 64) { throw new SQLException("Expected 64 update counts, got " + x.length); } for (int i = 0; i < x.length; ++i) { if (x[i] != 1) { throw new SQLException("Error at position " + i + ", got " + x[i] + " instead of 1"); } } // Rollback the transaction, exposing any race conditions. con.rollback(); pstmt.close(); } catch (SQLException ex) { ex.printStackTrace(); exceptions.add(ex); } }