Advertisement
Guest User

Untitled

a guest
Mar 14th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class Pakollinen2 {
  4. private static final String DRIVER = "org.postgresql.Driver";
  5. private static final String PROTOCOL = "jdbc:postgresql:";
  6. private static final String SERVER = "localhost";
  7. private static final int PORT = 5432;
  8. private static final String DATABASE = "teht2";
  9. private static final String USER = "postgres";
  10. private static final String PASSWORD = "kiiwisato";
  11.  
  12.  
  13. public static void main(String args[]) throws SQLException {
  14. try {
  15. Class.forName(DRIVER);
  16. }
  17. catch (ClassNotFoundException exception) {
  18. System.out.println("Problem loading the driver, exiting.");
  19. return;
  20. }
  21. // Getting inputs using In-class from Laki1, Laki2 and Oope by Jorma Laurikkala
  22. System.out.println("Please enter the account from which the transaction will be funded:");
  23. int account = In.readInt();
  24. System.out.println("Please enter the account which you want to transact the money to:");
  25. int account2 = In.readInt();
  26. System.out.println("Please enter the amount:");
  27. double amount = In.readDouble();
  28. double account1balance = 0;
  29. double account2balance = 0;
  30. boolean foundAccount1 = false;
  31. boolean foundAccount2 = false;
  32. PreparedStatement test = null;
  33. PreparedStatement stmt = null;
  34. PreparedStatement subtract = null;
  35. PreparedStatement stmt2 = null;
  36. PreparedStatement add = null;
  37. Connection con = null;
  38. try {
  39. con = DriverManager.getConnection(PROTOCOL + "//" + SERVER + ":" + PORT +
  40. "/" + DATABASE, USER, PASSWORD);
  41. con.setAutoCommit(false);
  42. // Testing that account exists in database.
  43. test = con.prepareStatement("SELECT tilinumero FROM tilit WHERE tilinumero = ?");
  44. test.clearParameters();
  45. test.setInt(1, account);
  46. ResultSet ts = test.executeQuery();
  47. if(ts.next())
  48. {
  49. foundAccount1 = true;
  50. }
  51. // Getting accounts balance.
  52. stmt = con.prepareStatement("SELECT saldo FROM TILIT WHERE tilinumero = ?");
  53. stmt.clearParameters();
  54. stmt.setInt(1, account);
  55. ResultSet rs = stmt.executeQuery();
  56. while(rs.next())
  57. {
  58. account1balance = rs.getDouble(1);
  59. }
  60. // Updating accounts balance.
  61. account1balance = (account1balance - amount);
  62. subtract = con.prepareStatement("UPDATE tilit SET saldo = ? WHERE tilinumero = ?");
  63. subtract.clearParameters();
  64. subtract.setInt(2, account);
  65. subtract.setDouble(1, account1balance);
  66. subtract.executeUpdate();
  67. // Testing that account exists in database.
  68. test.clearParameters();
  69. test.setInt(1, account2);
  70. ts = test.executeQuery();
  71. if(ts.next())
  72. {
  73. foundAccount2 = true;
  74. }
  75. // Getting accounts balance.
  76. stmt2 = con.prepareStatement("SELECT saldo FROM TILIT WHERE tilinumero = ?");
  77. stmt2.clearParameters();
  78. stmt2.setInt(1, account2);
  79. ResultSet rs2 = stmt2.executeQuery();
  80. while(rs2.next())
  81. {
  82. account2balance = rs2.getDouble(1);
  83. }
  84. // Updating accounts balance.
  85. account2balance = (account2balance + amount);
  86. add = con.prepareStatement("UPDATE tilit SET saldo = ? WHERE tilinumero = ?");
  87. add.clearParameters();
  88. add.setInt(2, account2);
  89. add.setDouble(1, account2balance);
  90. add.executeUpdate();
  91. // If both accounts existed, commiting.
  92. if(foundAccount1 && foundAccount2)
  93. {
  94. con.commit();
  95. System.out.println("Transaction complete!");
  96. }
  97. // Else errormsg and rollback.
  98. else if (!foundAccount1 && !foundAccount2)
  99. {
  100. con.rollback();
  101. System.out.println("Accounts " + account + " and " + account2 + " do not exist!");
  102. }
  103. else if (!foundAccount1)
  104. {
  105. con.rollback();
  106. System.out.println("Account " + account + " does not exist!");
  107. }
  108. else if (!foundAccount2)
  109. {
  110. con.rollback();
  111. System.out.println("Account " + account2 + " does not exist!");
  112. }
  113. test.close();
  114. stmt.close();
  115. stmt2.close();
  116. subtract.close();
  117. add.close();
  118. con.setAutoCommit(true);
  119. }
  120. catch (SQLException exception) {
  121. System.out.println("Error1: " + exception.getMessage());
  122. con.rollback();
  123. }
  124.  
  125.  
  126.  
  127. if(con != null)
  128. {
  129. try {
  130. con.close();
  131. }
  132. catch (SQLException exception) {
  133. System.out.println("Error closing connection to database. Exiting");
  134. return;
  135. }
  136. }
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement