Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.*;
  3. public class Core
  4. {
  5.     static Connection con;
  6.     static Statement stmt;
  7.     public static void main(String[] args)
  8.     {
  9.         openConnection();
  10.         Start();
  11.         closeConnection();
  12.     }
  13.     public static void Start()
  14.     {
  15.         Scanner keyboard = new Scanner(System.in);
  16.         System.out.println("Login, press 1");
  17.         System.out.println("Create client, press 2");
  18.         int start = keyboard.nextInt();
  19.         if (start == 1)
  20.         {
  21.             login();
  22.         }
  23.         else
  24.         {
  25.             createClient();
  26.         }
  27.     }
  28.     public static void login()
  29.     {
  30.         Scanner keyboardString = new Scanner(System.in);
  31.         System.out.println("Last name: ");
  32.         String lastname = keyboardString.nextLine();
  33.         System.out.println("Password: ");
  34.         String password = keyboardString.nextLine();
  35.         try
  36.         {
  37.             stmt = con.createStatement();
  38.             ResultSet ps = stmt.executeQuery("SELECT password FROM clients WHERE password = '" + password + "';");
  39.             while (ps.next())
  40.             {
  41.                 String passworddb = ps.getString("password");
  42.             }
  43.             if (passworddb.equals(password))
  44.             {
  45.                 stmt = con.createStatement();
  46.                 stmt.executeQuery("UPDATE clients SET 'online' = 1 WHERE 'password' = '" + password + "'");
  47.                 System.out.println("You succesfully logged in.");
  48.                 menu();
  49.             }
  50.             else
  51.             {
  52.                 System.out.println("Wrong login, please try again.");
  53.             }
  54.         }
  55.         catch(SQLException ex)
  56.         {
  57.             System.err.println("SQLException: " + ex.getMessage());
  58.         }
  59.         finally
  60.         {
  61.         stmt.close();
  62.         }
  63.     }
  64.     public static void menu()
  65.     {
  66.         Scanner keyboard = new Scanner(System.in);
  67.         System.out.println("1. Check transaction history.");
  68.         System.out.println("2. Withdraw.");
  69.         System.out.println("3. Deposit.");
  70.         System.out.println("4. Transfer.");
  71.         System.out.println("5. Create account.");
  72.         int menu = keyboard.nextInt();
  73.         if (menu == 1)
  74.         {
  75.             transactionHistory();
  76.         }
  77.         if (menu == 2)
  78.         {
  79.             withdraw();
  80.         }
  81.         if (menu == 3)
  82.         {
  83.             deposit();
  84.         }
  85.         if (menu == 4)
  86.         {
  87.             transfer();
  88.         }
  89.         if (menu == 5)
  90.         {
  91.             createAccount();
  92.         }
  93.     }
  94.     public static Connection openConnection()
  95.     {
  96.         try
  97.         {
  98.             Class.forName("com.mysql.jdbc.Driver");
  99.         }
  100.         catch(java.lang.ClassNotFoundException e)
  101.         {
  102.             System.err.print("ClassNotFoundException: ");
  103.             System.err.println(e.getMessage());
  104.         }
  105.         try
  106.         {
  107.             String url = "jdbc:mysql://localhost/bank";
  108.             con = java.sql.DriverManager.getConnection(url, "root", "hoihoi");
  109.         }
  110.         catch(SQLException ex)
  111.         {
  112.             System.err.println("SQLException: " + ex.getMessage());
  113.         }
  114.         return con;
  115.     }
  116.     public static Connection closeConnection()
  117.     {
  118.         try
  119.         {
  120.             con.close();
  121.         }
  122.         catch(Exception e)
  123.         {
  124.             System.err.println("SQLException: " + e.getMessage());
  125.         }
  126.         return con;
  127.     }
  128.     public static void createClient()
  129.     {
  130.         Scanner keyboardString = new Scanner(System.in);
  131.         System.out.println("Please write down your first name: ");
  132.         String firstname = keyboardString.nextLine();
  133.         System.out.println("Please write down your last name: ");
  134.         String lastname = keyboardString.nextLine();
  135.         System.out.println("Please write down your password: ");
  136.         String password = keyboardString.nextLine();
  137.        
  138.         String newaccount = "INSERT INTO `clients` (`id`, `password`, `first name`, `last name`, `online`) VALUES (null, '" + password + "', '" + firstname + "', '" + lastname + "', 0);";
  139.         try
  140.         {
  141.             stmt = con.createStatement();
  142.             stmt.executeQuery(newaccount);
  143.             stmt.close();
  144.         }
  145.         catch(SQLException ex)
  146.         {
  147.             System.err.println("SQLException: " + ex.getMessage());
  148.         }
  149.     System.out.println("Account created succesfully");
  150.     }
  151.     public static void createAccount()
  152.     {
  153.         Scanner keyboard = new Scanner(System.in);
  154.         System.out.println("Please write down your desired account number: ");
  155.         int accountnumber = keyboard.nextInt();
  156.         try
  157.         {
  158.             stmt = con.createStatement();
  159.             stmt.executeUpdate("INSERT INTO `accounts` (`id`, `number`, `client_id`, `saldo`) VALUES (null, " + accountnumber + ", " + getClientID() + ", 0);");
  160.             stmt.close();
  161.         }
  162.         catch(SQLException ex)
  163.         {
  164.             System.err.println("SQLException: " + ex.getMessage());
  165.         }
  166.     System.out.println("Account created succesfully");
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement