Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 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.         System.out.println("6. Logout.");
  73.         int menu = keyboard.nextInt();
  74.         if (menu == 1)
  75.         {
  76.             transactionHistory();
  77.         }
  78.         if (menu == 2)
  79.         {
  80.             withdraw();
  81.         }
  82.         if (menu == 3)
  83.         {
  84.             deposit();
  85.         }
  86.         if (menu == 4)
  87.         {
  88.             transfer();
  89.         }
  90.         if (menu == 5)
  91.         {
  92.             createAccount();
  93.         }
  94.         if (menu == 6)
  95.         {
  96.             logout();
  97.         }
  98.     }
  99.     public static Connection openConnection()
  100.     {
  101.         try
  102.         {
  103.             Class.forName("com.mysql.jdbc.Driver");
  104.         }
  105.         catch(java.lang.ClassNotFoundException e)
  106.         {
  107.             System.err.print("ClassNotFoundException: ");
  108.             System.err.println(e.getMessage());
  109.         }
  110.         try
  111.         {
  112.             String url = "jdbc:mysql://localhost/bank";
  113.             con = java.sql.DriverManager.getConnection(url, "root", "hoihoi");
  114.         }
  115.         catch(SQLException ex)
  116.         {
  117.             System.err.println("SQLException: " + ex.getMessage());
  118.         }
  119.         return con;
  120.     }
  121.     public static Connection closeConnection()
  122.     {
  123.         try
  124.         {
  125.             con.close();
  126.         }
  127.         catch(Exception e)
  128.         {
  129.             System.err.println("SQLException: " + e.getMessage());
  130.         }
  131.         return con;
  132.     }
  133.     public static void createClient()
  134.     {
  135.         Scanner keyboardString = new Scanner(System.in);
  136.         System.out.println("Please write down your first name: ");
  137.         String firstname = keyboardString.nextLine();
  138.         System.out.println("Please write down your last name: ");
  139.         String lastname = keyboardString.nextLine();
  140.         System.out.println("Please write down your password: ");
  141.         String password = keyboardString.nextLine();
  142.        
  143.         String newaccount = "INSERT INTO `clients` (`id`, `password`, `first name`, `last name`, `online`) VALUES (null, '" + password + "', '" + firstname + "', '" + lastname + "', 0);";
  144.         try
  145.         {
  146.             stmt = con.createStatement();
  147.             stmt.executeQuery(newaccount);
  148.             stmt.close();
  149.         }
  150.         catch(SQLException ex)
  151.         {
  152.             System.err.println("SQLException: " + ex.getMessage());
  153.         }
  154.     System.out.println("Account created succesfully");
  155.     }
  156.     public static void createAccount()
  157.     {
  158.         Scanner keyboard = new Scanner(System.in);
  159.         System.out.println("Please write down your desired account number: ");
  160.         int accountnumber = keyboard.nextInt();
  161.         try
  162.         {
  163.             stmt = con.createStatement();
  164.             stmt.executeUpdate("INSERT INTO `accounts` (`id`, `number`, `client_id`, `saldo`) VALUES (null, " + accountnumber + ", " + getClientID() + ", 0);");
  165.             stmt.close();
  166.         }
  167.         catch(SQLException ex)
  168.         {
  169.             System.err.println("SQLException: " + ex.getMessage());
  170.         }
  171.     System.out.println("Account created succesfully");
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement