Skip to content

Instantly share code, notes, and snippets.

@reschke
Last active February 2, 2016 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reschke/226be61331653164b80a to your computer and use it in GitHub Desktop.
Save reschke/226be61331653164b80a to your computer and use it in GitHub Desktop.
pgdjbc BatchUpdateException test
import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
public class PostgresBatchUpdate {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("org.postgresql.Driver");
String jdbcurl = "jdbc:postgresql:test";
String user = "postgres";
String passwd = "geheim";
Connection con = DriverManager.getConnection(jdbcurl, user, passwd);
con.setAutoCommit(true);
DatabaseMetaData dmd = con.getMetaData();
System.out.println("DB: " + dmd.getDatabaseProductName() + " " + dmd.getDatabaseProductVersion());
System.out.println("Driver: " + dmd.getDriverName() + " " + dmd.getDriverVersion());
// create table when absent
Statement s = con.createStatement();
s.execute("create table if not exists TEST (ID varchar(512) not null primary key, DATA varchar(512))");
s.close();
// clean up
PreparedStatement st = con.prepareStatement("DELETE FROM test WHERE ID in (?, ?)");
st.setString(1, "key-1");
st.setString(2, "key-2");
st.executeUpdate();
st.close();
// insert "key-2"
st = con.prepareStatement("INSERT INTO test (id) VALUES (?)");
st.setString(1, "key-2");
st.executeUpdate();
st.close();
// update as batch
PreparedStatement batchSt = con.prepareStatement("INSERT INTO test (id) VALUES (?)");
batchSt.setString(1, "key-1");
batchSt.addBatch();
batchSt.setString(1, "key-2");
batchSt.addBatch();
boolean wasKey1Inserted = false;
try {
int[] batchResult = batchSt.executeBatch();
batchSt.close();
System.out.println("update unexpectedly succeeded: " + Arrays.toString(batchResult));
}
catch (BatchUpdateException ex) {
int[] batchResult = ex.getUpdateCounts();
System.out.println("update failed as expected: " + Arrays.toString(batchResult));
wasKey1Inserted = batchResult.length >= 1 && batchResult[0] == 1;
if (wasKey1Inserted) {
System.out.println("batch result implies that first operation (insert of key-1) suceeded");
}
}
con.close();
if (wasKey1Inserted) {
con = DriverManager.getConnection(jdbcurl, user, passwd);
con.setAutoCommit(true);
boolean foundKey1 = false;
PreparedStatement q = con.prepareStatement("SELECT ID FROM test");
ResultSet r = q.executeQuery();
while (r.next()) {
System.out.println("Found: " + r.getString(1));
foundKey1 |= ("key-1".equals(r.getString(1)));
}
if (!foundKey1) {
System.out.println("Error: 'key-1' not found");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment