Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Implementation of question number one
- *
- * Only contains specific code for the question. Handling of statistics gathering
- * is inherited from QuestionRunner
- */
- import java.sql.*;
- public class QuestionOne extends QuestionRunner {
- public QuestionOne(Connection iconn) {
- this.conn = iconn;
- }
- public void runCase() throws SQLException {
- int currentChild = 1;
- PreparedStatement stmtMother = conn.prepareStatement("SELECT id FROM mother WHERE id = ?");
- PreparedStatement stmtChild = conn.prepareStatement("INSERT INTO child VALUES "
- + "(?, 'Foo', 'Bar', 'M', 1999, ?, 'y')");
- ResultSet rsMother;
- for (int currentMother = 1; currentMother <= 50000; currentMother++) {
- stmtMother.setInt(1, currentMother);
- rsMother = stmtMother.executeQuery();
- if (rsMother.next()) {
- stmtChild.setInt(1, currentChild);
- stmtChild.setInt(2, currentMother);
- stmtChild.executeQuery();
- currentChild++;
- }
- rsMother.close();
- rsMother = null;
- }
- stmtChild.close();
- stmtMother.close();
- PreparedStatement stmtCommit = conn.prepareStatement("COMMIT");
- stmtCommit.executeQuery();
- }
- }
- import java.sql.*;
- public class QuestionTwo extends QuestionRunner {
- public QuestionTwo(Connection iconn) {
- this.conn = iconn;
- }
- public void runCase() throws SQLException {
- int currentChild = 1;
- PreparedStatement stmtMother = conn.prepareStatement("SELECT id FROM mother WHERE id = ?");
- PreparedStatement stmtChild = conn.prepareStatement("INSERT INTO child VALUES "
- + "(?, 'Foo', 'Bar', 'M', 1999, ?, 'y')");
- for (int currentMother = 1; currentMother <= 50000; currentMother++) {
- stmtMother.setInt(1, currentMother);
- ResultSet rsMother = stmtMother.executeQuery();
- stmtChild.setInt(1, currentChild);
- stmtChild.setInt(2, currentMother);
- try {
- stmtChild.executeQuery();
- }
- catch (SQLException e) {
- if (!(e.getErrorCode() == 2291))
- throw new SQLException(e);
- }
- finally {
- currentChild++;
- rsMother.close();
- rsMother = null;
- }
- }
- stmtChild.close();
- stmtMother.close();
- PreparedStatement stmtCommit = conn.prepareStatement("COMMIT");
- stmtCommit.executeQuery();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement