Advertisement
Guest User

database hw

a guest
Nov 20th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class FTM
  4. {
  5. public static void main(String[] args) throws SQLException, ClassNotFoundException
  6. {
  7. System.out.println(">>>>Begin program...");
  8. Class.forName("org.postgresql.Driver");
  9. String connectionString = "jdbc:postgresql://localhost:5432/PA2Bank";
  10. String username = args[0];
  11. String password = args[1];
  12. Connection conn = DriverManager.getConnection(connectionString, username, password);
  13. DatabaseMetaData md = conn.getMetaData();
  14.  
  15. Statement stmt1 = conn.createStatement();
  16. Statement stmt2 = conn.createStatement();
  17.  
  18. boolean empty = true;
  19. ResultSet transferEmpty = stmt1.executeQuery("SELECT * FROM transfer");
  20. while (transferEmpty.next())
  21. {
  22. empty = false;
  23. }
  24. if (empty)
  25. {
  26. System.out.println("exiting...");
  27. return;
  28. }
  29.  
  30. boolean deltaEmpty;
  31. String dropInfluence = "DROP TABLE influence";
  32. String dropFunds = "DROP TABLE funds";
  33. String dropDelta = "DROP TABLE delta";
  34. String dropFundsOld = "DROP TABLE funds_old";
  35. String createInfluence = "CREATE TABLE influence (\"from\" varchar(32), \"to\" varchar(32));";
  36. String createFunds = "CREATE TABLE funds (src varchar(32), dest varchar(32))";
  37. String createDelta = "CREATE TABLE delta (src varchar(32), dest varchar(32));";
  38. String createFundsOld = "CREATE TABLE funds_old (src varchar(32), dest varchar(32))";
  39. ResultSet influenceExists = md.getTables(null, null, "influence", null);
  40. ResultSet deltaExists = md.getTables(null, null, "delta", null);
  41. ResultSet fundsOldExists = md.getTables(null,null,"funds_old",null);
  42. ResultSet fundsExists = md.getTables(null,null,"funds",null);
  43.  
  44. // drop tables if they exist and create new ones
  45. while (influenceExists.next())
  46. {
  47. stmt1.executeUpdate(dropInfluence);
  48. }
  49. while (fundsOldExists.next())
  50. {
  51. stmt1.executeUpdate(dropFundsOld);
  52. }
  53. while (deltaExists.next())
  54. {
  55. stmt1.executeUpdate(dropDelta);
  56. }
  57. while (fundsExists.next())
  58. {
  59. stmt1.executeUpdate(dropFunds);
  60. }
  61. stmt1.executeUpdate(createFunds);
  62. stmt1.executeUpdate("INSERT INTO funds (src, dest) SELECT src, tgt FROM transfer");
  63. stmt1.executeUpdate(createFundsOld);
  64. stmt1.executeUpdate("INSERT INTO funds_old (src, dest) SELECT src, tgt FROM transfer");
  65. stmt1.executeUpdate(createDelta);
  66. stmt1.executeUpdate("INSERT INTO delta (src, dest) SELECT src, dest FROM funds");
  67. stmt1.executeUpdate(createInfluence);
  68. deltaEmpty = false;
  69.  
  70. ResultSet delta;
  71. String update;
  72.  
  73. while (!deltaEmpty)
  74. {
  75. // funds = funds + new recursive step
  76. update = "INSERT INTO funds SELECT DISTINCT x.src, y.dest FROM delta x, funds y WHERE x.dest=y.src "
  77. + "AND (x.src, y.dest) NOT IN (SELECT * FROM funds) UNION "
  78. + "SELECT DISTINCT w.src, z.dest FROM funds w, delta z WHERE w.dest=z.src "
  79. + "AND (w.src, z.dest) NOT IN (SELECT * FROM funds)";
  80. stmt1.executeUpdate(update);
  81. stmt1.executeUpdate("DELETE FROM delta");
  82. deltaEmpty = true;
  83. stmt1.executeUpdate("INSERT INTO delta SELECT src, dest FROM funds EXCEPT SELECT src, dest FROM funds_old");
  84. delta = stmt2.executeQuery("SELECT * FROM delta");
  85.  
  86. while (delta.next())
  87. {
  88. deltaEmpty = false;
  89. break;
  90. }
  91. stmt1.executeUpdate("INSERT INTO funds_old SELECT * FROM delta");
  92. System.out.println("==========================");
  93. }
  94.  
  95. // insert names corresponding to acct numbers into influence table
  96. update = "INSERT INTO influence SELECT d1.cname c1, d2.cname c2 FROM depositor d1, depositor d2, funds " +
  97. "WHERE d1.ano=funds.src AND d2.ano=funds.dest";
  98. stmt1.executeUpdate(update);
  99.  
  100. // cleanup
  101. fundsOldExists = md.getTables(null,null,"funds_old",null);
  102. fundsExists = md.getTables(null,null,"funds",null);
  103. deltaExists = md.getTables(null, null, "delta", null);
  104.  
  105. while (fundsOldExists.next())
  106. {
  107. stmt1.executeUpdate(dropFundsOld);
  108. }
  109. while (deltaExists.next())
  110. {
  111. stmt1.executeUpdate(dropDelta);
  112. }
  113. while (fundsExists.next())
  114. {
  115. stmt1.executeUpdate(dropFunds);
  116. }
  117.  
  118. stmt1.close();
  119. stmt2.close();
  120. conn.close();
  121.  
  122. System.out.println("<<<<...Finished successfully");
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement