Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.Console;
- import java.sql.*;
- public class bank {
- Connection conn = null;
- Statement sttmnt = null;
- ResultSet rSet = null;
- /* Connects the user to the MTU database pmdunnin in order to
- * use the simple banking system.
- */
- public int connectDB() {
- String username = "";
- String password = "";
- try {
- Console console = System.console();
- if(console == null) {
- System.out.println("There was an issue with the console. Please run the rerun the program in terminal");
- System.out.println("For example: ...");
- return 0;
- }
- // Gets the user's username and password.
- username = console.readLine("Please enter in your username:");
- password = String.valueOf(console.readPassword("Please enter in your password:"));
- } catch (Exception exc1) {
- exc1.printStackTrace();
- }
- try {
- conn = DriverManager.getConnection("jdbc:mysql://classdb.it.mtu.edu/pmdunnin", username, password);
- } catch (SQLException exc2) {
- System.out.println(exc2.getMessage());
- exc2.printStackTrace();
- return 1;
- }
- return 1;
- }
- /* Disconnects the user from the database server. */
- public void disconnect() {
- try {
- conn.close();
- } catch (SQLException exc) {
- System.out.println("SQLException: " + exc.getMessage());
- System.out.println("SQLState: " + exc.getSQLState());
- System.out.println("VendorError: " + exc.getErrorCode());
- }
- }
- public int transfer(String savingAccountNum, String checkingAccountNum, double balance) {
- PreparedStatement statement = null;
- ResultSet rSet = null;
- int rowCount;
- Console transferConsole = System.console();
- System.out.println("Transfer Test 1");
- // Start Transaction #1:
- try {
- conn.setAutoCommit(false);
- conn.setTransactionIsolation(conn.TRANSACTION_SERIALIZABLE);
- } catch (SQLException exc3) {
- exc3.printStackTrace();
- return 0;
- }
- System.out.println("Transfer Test 2");
- // Transfer money attempt
- try {
- statement = conn.prepareStatement("SELECT balance FROM checking WHERE account_number = ?");
- statement.setString( 1, checkingAccountNum);
- rSet = statement.executeQuery();
- if(rSet.getFetchSize() < 1) {
- transferConsole.printf("This checking account does NOT exist!");
- //System.out.println("This checking account does NOT exist!");
- conn.rollback();
- } else {
- transferConsole.printf("This checking account does exist!");
- //System.out.println("This checking account does exist!");
- }
- transferConsole.flush();
- //System.out.println("Test");
- } catch (SQLException exc4) {
- // Handle any errors
- System.out.println("SQLException: " + exc4.getMessage());
- System.out.println("SQLState: " + exc4.getSQLState());
- System.out.println("VendorError: " + exc4.getErrorCode());
- }
- return 1;
- }
- public int display(int customer_id) {
- try {
- conn.setAutoCommit(false);
- conn.setTransactionIsolation(conn.TRANSACTION_SERIALIZABLE);
- } catch (SQLException exc3) {
- exc3.printStackTrace();
- return 0;
- }
- //
- try {
- //first confirm that customer_id exists
- PreparedStatement statement = conn.prepareStatement("SELECT * FROM customer WHERE customer_id = ?");
- statement.setString(1, Integer.toString(customer_id));
- ResultSet rSet = statement.executeQuery();
- if(rSet.getFetchSize() != 1)
- {
- //customer doesn't exist
- System.out.println("Customer ID not found");
- conn.rollback();
- }
- else
- {
- //checking account statement
- statement = conn.prepareStatement("SELECT balance FROM checking WHERE customer_id = ?");
- statement.setString(1, Integer.toString(customer_id));
- rSet = statement.executeQuery();
- if(rSet.getFetchSize() < 1) {
- System.out.printf("No checking accounts found for customer %d", customer_id);
- conn.rollback();
- }
- else {
- //print checking account balances here
- }
- //savings account statement
- statement = conn.prepareStatement("SELECT balance FROM saving WHERE customer_id = ?");
- statement.setString(1, Integer.toString(customer_id));
- rSet = statement.executeQuery();
- if(rSet.getFetchSize() < 1) {
- System.out.printf("No saving accounts found for customer %d", customer_id);
- conn.rollback();
- }
- else {
- //print checking account balances here
- }
- }
- //System.out.println("Test");
- } catch (SQLException exc4) {
- // Handle any errors
- System.out.println("SQLException: " + exc4.getMessage());
- System.out.println("SQLState: " + exc4.getSQLState());
- System.out.println("VendorError: " + exc4.getErrorCode());
- }
- return 1;
- }
- public static void main(String args[]) {
- bank mtuBank = new bank();
- Console mainConsole = System.console();
- int customerID;
- String savingAccount = "";
- String checkingAccount = "";
- String functionSelect = "";
- double balanceAmount;
- if (args.length < 2) {
- System.out.println("Incomplete program arguments. Please run program again.");
- } else {
- mtuBank.connectDB();
- mainConsole.printf("\n Function Check: " + args[0] + "\n\n");
- // Runs the display function based on command line input.
- if ( args[0] == "display") {
- mainConsole.printf("\n Display Check \n");
- mainConsole.flush();
- customerID = Integer.parseInt(args[1]);
- mtuBank.display(customerID);
- // Runs the transfer function based on command line input.
- } else if (args[0] == "transfer" && args.length >= 4) {
- mainConsole.printf("\n Transfer Check \n");
- mainConsole.flush();
- savingAccount = args[1];
- checkingAccount = args[2];
- balanceAmount = Double.parseDouble(args[3]);
- //mtuBank.transfer(savingAccount, checkingAccount, balanceAmount);
- }
- //mainConsole.printf("\n\n" + "args length:" + args.length + "\n\n");
- mainConsole.printf("\nPrint Check\n\n");
- mainConsole.flush();
- mtuBank.disconnect();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement