Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.26 KB | None | 0 0
  1.  
  2. /*
  3.  * Implementation of question number one
  4.  *
  5.  * Only contains specific code for the question. Handling of statistics gathering
  6.  * is inherited from QuestionRunner
  7.  */
  8.  
  9. import java.sql.*;
  10.  
  11. public class QuestionOne extends QuestionRunner {
  12.  
  13.     public QuestionOne(Connection iconn) {
  14.  
  15.         this.conn = iconn;
  16.     }
  17.  
  18.     public void runCase() throws SQLException {
  19.        
  20.         int currentChild = 1;
  21.        
  22.         PreparedStatement stmtMother = conn.prepareStatement("SELECT id FROM mother WHERE id = ?");
  23.         PreparedStatement stmtChild = conn.prepareStatement("INSERT INTO child VALUES "
  24.                                                                 + "(?, 'Foo', 'Bar', 'M', 1999, ?, 'y')");
  25.         ResultSet rsMother;
  26.        
  27.         for (int currentMother = 1; currentMother <= 50000; currentMother++) {
  28.             stmtMother.setInt(1, currentMother);
  29.             rsMother = stmtMother.executeQuery();
  30.            
  31.             if (rsMother.next()) {
  32.                 stmtChild.setInt(1, currentChild);
  33.                 stmtChild.setInt(2, currentMother);
  34.                 stmtChild.executeQuery();
  35.                
  36.                 currentChild++;
  37.             }
  38.  
  39.             rsMother.close();
  40.             rsMother = null;
  41.         }
  42.  
  43.         stmtChild.close();
  44.         stmtMother.close();
  45.         PreparedStatement stmtCommit = conn.prepareStatement("COMMIT");
  46.         stmtCommit.executeQuery();
  47.     }
  48. }
  49.  
  50.  
  51.  
  52. import java.sql.*;
  53.  
  54. public class QuestionTwo extends QuestionRunner {
  55.  
  56.     public QuestionTwo(Connection iconn) {
  57.         this.conn = iconn;
  58.     }
  59.  
  60.     public void runCase() throws SQLException {
  61.         int currentChild = 1;
  62.        
  63.         PreparedStatement stmtMother = conn.prepareStatement("SELECT id FROM mother WHERE id = ?");
  64.         PreparedStatement stmtChild = conn.prepareStatement("INSERT INTO child VALUES "
  65.                                                             + "(?, 'Foo', 'Bar', 'M', 1999, ?, 'y')");
  66.        
  67.         for (int currentMother = 1; currentMother <= 50000; currentMother++) {
  68.             stmtMother.setInt(1, currentMother);
  69.             ResultSet rsMother = stmtMother.executeQuery();
  70.             stmtChild.setInt(1, currentChild);
  71.             stmtChild.setInt(2, currentMother);
  72.            
  73.             try {
  74.                 stmtChild.executeQuery();
  75.             }
  76.             catch (SQLException e) {
  77.                 if (!(e.getErrorCode() == 2291))
  78.                     throw new SQLException(e);
  79.             }
  80.             finally {
  81.                 currentChild++;
  82.                 rsMother.close();
  83.                 rsMother = null;
  84.             }
  85.         }
  86.  
  87.         stmtChild.close();
  88.         stmtMother.close();
  89.         PreparedStatement stmtCommit = conn.prepareStatement("COMMIT");
  90.         stmtCommit.executeQuery();
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement