Advertisement
Guest User

Untitled

a guest
Nov 16th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. import java.io.Console;
  2. import java.sql.*;
  3. public class bank {
  4.  
  5. Connection conn = null;
  6. Statement sttmnt = null;
  7. ResultSet rSet = null;
  8.  
  9. /* Connects the user to the MTU database pmdunnin in order to
  10. * use the simple banking system.
  11. */
  12. public int connectDB() {
  13. String username = "";
  14. String password = "";
  15.  
  16. try {
  17. Console console = System.console();
  18. if(console == null) {
  19. System.out.println("There was an issue with the console. Please run the rerun the program in terminal");
  20. System.out.println("For example: ...");
  21. return 0;
  22. }
  23.  
  24. // Gets the user's username and password.
  25. username = console.readLine("Please enter in your username:");
  26. password = String.valueOf(console.readPassword("Please enter in your password:"));
  27.  
  28. } catch (Exception exc1) {
  29. exc1.printStackTrace();
  30. }
  31.  
  32. try {
  33. conn = DriverManager.getConnection("jdbc:mysql://classdb.it.mtu.edu/pmdunnin", username, password);
  34. } catch (SQLException exc2) {
  35. System.out.println(exc2.getMessage());
  36. exc2.printStackTrace();
  37. return 1;
  38. }
  39.  
  40. return 1;
  41. }
  42.  
  43. /* Disconnects the user from the database server. */
  44. public void disconnect() {
  45.  
  46. try {
  47. conn.close();
  48. } catch (SQLException exc) {
  49. System.out.println("SQLException: " + exc.getMessage());
  50. System.out.println("SQLState: " + exc.getSQLState());
  51. System.out.println("VendorError: " + exc.getErrorCode());
  52. }
  53. }
  54.  
  55.  
  56. public int transfer(String savingAccountNum, String checkingAccountNum, double balance) {
  57.  
  58. PreparedStatement statement = null;
  59. ResultSet rSet = null;
  60. int rowCount;
  61. Console transferConsole = System.console();
  62.  
  63. System.out.println("Transfer Test 1");
  64. // Start Transaction #1:
  65. try {
  66. conn.setAutoCommit(false);
  67. conn.setTransactionIsolation(conn.TRANSACTION_SERIALIZABLE);
  68. } catch (SQLException exc3) {
  69. exc3.printStackTrace();
  70. return 0;
  71. }
  72.  
  73. System.out.println("Transfer Test 2");
  74.  
  75. // Transfer money attempt
  76. try {
  77. statement = conn.prepareStatement("SELECT balance FROM checking WHERE account_number = ?");
  78. statement.setString( 1, checkingAccountNum);
  79. rSet = statement.executeQuery();
  80.  
  81. if(rSet.getFetchSize() < 1) {
  82. transferConsole.printf("This checking account does NOT exist!");
  83. //System.out.println("This checking account does NOT exist!");
  84. conn.rollback();
  85. } else {
  86. transferConsole.printf("This checking account does exist!");
  87. //System.out.println("This checking account does exist!");
  88. }
  89.  
  90. transferConsole.flush();
  91. //System.out.println("Test");
  92.  
  93. } catch (SQLException exc4) {
  94. // Handle any errors
  95. System.out.println("SQLException: " + exc4.getMessage());
  96. System.out.println("SQLState: " + exc4.getSQLState());
  97. System.out.println("VendorError: " + exc4.getErrorCode());
  98. }
  99.  
  100.  
  101. return 1;
  102. }
  103.  
  104. public int display(int customer_id) {
  105. try {
  106. conn.setAutoCommit(false);
  107. conn.setTransactionIsolation(conn.TRANSACTION_SERIALIZABLE);
  108. } catch (SQLException exc3) {
  109. exc3.printStackTrace();
  110. return 0;
  111. }
  112.  
  113. //
  114. try {
  115. //first confirm that customer_id exists
  116. PreparedStatement statement = conn.prepareStatement("SELECT * FROM customer WHERE customer_id = ?");
  117. statement.setString(1, Integer.toString(customer_id));
  118. ResultSet rSet = statement.executeQuery();
  119. if(rSet.getFetchSize() != 1)
  120. {
  121. //customer doesn't exist
  122. System.out.println("Customer ID not found");
  123. conn.rollback();
  124. }
  125. else
  126. {
  127. //checking account statement
  128. statement = conn.prepareStatement("SELECT balance FROM checking WHERE customer_id = ?");
  129. statement.setString(1, Integer.toString(customer_id));
  130. rSet = statement.executeQuery();
  131. if(rSet.getFetchSize() < 1) {
  132. System.out.printf("No checking accounts found for customer %d", customer_id);
  133. conn.rollback();
  134. }
  135. else {
  136. //print checking account balances here
  137. }
  138.  
  139. //savings account statement
  140. statement = conn.prepareStatement("SELECT balance FROM saving WHERE customer_id = ?");
  141. statement.setString(1, Integer.toString(customer_id));
  142. rSet = statement.executeQuery();
  143. if(rSet.getFetchSize() < 1) {
  144. System.out.printf("No saving accounts found for customer %d", customer_id);
  145. conn.rollback();
  146. }
  147. else {
  148. //print checking account balances here
  149. }
  150.  
  151. }
  152.  
  153. //System.out.println("Test");
  154. } catch (SQLException exc4) {
  155. // Handle any errors
  156. System.out.println("SQLException: " + exc4.getMessage());
  157. System.out.println("SQLState: " + exc4.getSQLState());
  158. System.out.println("VendorError: " + exc4.getErrorCode());
  159. }
  160.  
  161. return 1;
  162.  
  163. }
  164.  
  165. public static void main(String args[]) {
  166. bank mtuBank = new bank();
  167. Console mainConsole = System.console();
  168. int customerID;
  169. String savingAccount = "";
  170. String checkingAccount = "";
  171. String functionSelect = "";
  172. double balanceAmount;
  173.  
  174. if (args.length < 2) {
  175. System.out.println("Incomplete program arguments. Please run program again.");
  176.  
  177. } else {
  178.  
  179.  
  180. mtuBank.connectDB();
  181.  
  182. mainConsole.printf("\n Function Check: " + args[0] + "\n\n");
  183.  
  184. // Runs the display function based on command line input.
  185. if ( args[0] == "display") {
  186. mainConsole.printf("\n Display Check \n");
  187. mainConsole.flush();
  188. customerID = Integer.parseInt(args[1]);
  189. mtuBank.display(customerID);
  190.  
  191. // Runs the transfer function based on command line input.
  192. } else if (args[0] == "transfer" && args.length >= 4) {
  193. mainConsole.printf("\n Transfer Check \n");
  194. mainConsole.flush();
  195. savingAccount = args[1];
  196. checkingAccount = args[2];
  197. balanceAmount = Double.parseDouble(args[3]);
  198. //mtuBank.transfer(savingAccount, checkingAccount, balanceAmount);
  199.  
  200. }
  201. //mainConsole.printf("\n\n" + "args length:" + args.length + "\n\n");
  202.  
  203. mainConsole.printf("\nPrint Check\n\n");
  204. mainConsole.flush();
  205.  
  206. mtuBank.disconnect();
  207.  
  208. }
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement