Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.*;
- public class Core
- {
- static Connection con;
- static Statement stmt;
- public static void main(String[] args)
- {
- openConnection();
- Start();
- closeConnection();
- }
- public static void Start()
- {
- Scanner keyboard = new Scanner(System.in);
- System.out.println("Login, press 1");
- System.out.println("Create client, press 2");
- int start = keyboard.nextInt();
- if (start == 1)
- {
- login();
- }
- else
- {
- createClient();
- }
- }
- public static void login()
- {
- Scanner keyboardString = new Scanner(System.in);
- System.out.println("Last name: ");
- String lastname = keyboardString.nextLine();
- System.out.println("Password: ");
- String password = keyboardString.nextLine();
- try
- {
- stmt = con.createStatement();
- ResultSet ps = stmt.executeQuery("SELECT password FROM clients WHERE password = '" + password + "';");
- while (ps.next())
- {
- String passworddb = ps.getString("password");
- }
- if (passworddb.equals(password))
- {
- stmt = con.createStatement();
- stmt.executeQuery("UPDATE clients SET 'online' = 1 WHERE 'password' = '" + password + "'");
- System.out.println("You succesfully logged in.");
- menu();
- }
- else
- {
- System.out.println("Wrong login, please try again.");
- }
- }
- catch(SQLException ex)
- {
- System.err.println("SQLException: " + ex.getMessage());
- }
- finally
- {
- stmt.close();
- }
- }
- public static void menu()
- {
- Scanner keyboard = new Scanner(System.in);
- System.out.println("1. Check transaction history.");
- System.out.println("2. Withdraw.");
- System.out.println("3. Deposit.");
- System.out.println("4. Transfer.");
- System.out.println("5. Create account.");
- int menu = keyboard.nextInt();
- if (menu == 1)
- {
- transactionHistory();
- }
- if (menu == 2)
- {
- withdraw();
- }
- if (menu == 3)
- {
- deposit();
- }
- if (menu == 4)
- {
- transfer();
- }
- if (menu == 5)
- {
- createAccount();
- }
- }
- public static Connection openConnection()
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- }
- catch(java.lang.ClassNotFoundException e)
- {
- System.err.print("ClassNotFoundException: ");
- System.err.println(e.getMessage());
- }
- try
- {
- String url = "jdbc:mysql://localhost/bank";
- con = java.sql.DriverManager.getConnection(url, "root", "hoihoi");
- }
- catch(SQLException ex)
- {
- System.err.println("SQLException: " + ex.getMessage());
- }
- return con;
- }
- public static Connection closeConnection()
- {
- try
- {
- con.close();
- }
- catch(Exception e)
- {
- System.err.println("SQLException: " + e.getMessage());
- }
- return con;
- }
- public static void createClient()
- {
- Scanner keyboardString = new Scanner(System.in);
- System.out.println("Please write down your first name: ");
- String firstname = keyboardString.nextLine();
- System.out.println("Please write down your last name: ");
- String lastname = keyboardString.nextLine();
- System.out.println("Please write down your password: ");
- String password = keyboardString.nextLine();
- String newaccount = "INSERT INTO `clients` (`id`, `password`, `first name`, `last name`, `online`) VALUES (null, '" + password + "', '" + firstname + "', '" + lastname + "', 0);";
- try
- {
- stmt = con.createStatement();
- stmt.executeQuery(newaccount);
- stmt.close();
- }
- catch(SQLException ex)
- {
- System.err.println("SQLException: " + ex.getMessage());
- }
- System.out.println("Account created succesfully");
- }
- public static void createAccount()
- {
- Scanner keyboard = new Scanner(System.in);
- System.out.println("Please write down your desired account number: ");
- int accountnumber = keyboard.nextInt();
- try
- {
- stmt = con.createStatement();
- stmt.executeUpdate("INSERT INTO `accounts` (`id`, `number`, `client_id`, `saldo`) VALUES (null, " + accountnumber + ", " + getClientID() + ", 0);");
- stmt.close();
- }
- catch(SQLException ex)
- {
- System.err.println("SQLException: " + ex.getMessage());
- }
- System.out.println("Account created succesfully");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement