Accessing Database Using Java and MySQL: Part 3

Before reading further, read the previous parts of this articles.

Now let's explain further about the procedure involved for an employee.

Transactions involved for an Authorized Employee

Authorized Employee

This module also contains two separate Java files, EmployeeMain.java and ConnectDB.java in the same way as explained for the customer module.

The following processes can be done for an employee:

  • Can check customer's details
  • Can check his/her details
  • Can change his/her password

Although there can be much more processes done for an employee like withdrawing money, updating customer details, and so on, but here we are just giving a few examples and all the rest you can try yourself.

The database schema table created for an employee is as in the following:

schema table created

For now we are working with the employee schema table, so the following are the contents of the employee table.

customer table

Now let's move to the coding part for an employee and database.

Code Example

EmployeeMain.java

  1. import java.sql.*;  
  2. import java.io.*;  
  3. import mydb.ConnectDB;  
  4.   
  5. class Employee {  
  6.  void  Employee_dtl(String id){  
  7.     try{  
  8.       ConnectDB ob = new ConnectDB();  
  9.       Connection con = ob.c();  
  10.       Statement stm = con.createStatement();  
  11.       ResultSet rst = stm.executeQuery("select * from employee where EmpID ='"+id+"'");  
  12.       if(rst.next())  
  13.       {  
  14.           System.out.println("EmpID           :"+id);  
  15.           System.out.println("Password        :"+rst.getString("password"));  
  16.           System.out.println("Name            :"+rst.getString("name"));  
  17.           System.out.println("Contact         :"+rst.getString("contact"));  
  18.           System.out.println("Current Balance :"+rst.getDouble("balance"));  
  19.           System.out.println("Address         :"+rst.getString("address"));  
  20.       }  
  21.     }  
  22.     catch(Exception e){  
  23.         System.out.println("Employee class display method"+e);  
  24.     } }  
  25.   void change_pass_emp(String id, String pass){  
  26.       try{  
  27.       BufferedReader in=new BufferedReader(new InputStreamReader(System.in));  
  28.       ConnectDB ob = new ConnectDB();  
  29.       Connection con = ob.c();  
  30.       Statement stm = con.createStatement();  
  31.       System.out.println("Enter new password :");  
  32.       String p1=in.readLine();  
  33.       System.out.println("Confirm password :");  
  34.       String p2=in.readLine();  
  35.   
  36.       if(p1.equals(p2))  
  37.       {  
  38.        stm.executeUpdate("update employee set password='"+p1+"' where EmpID='"+id+"' and password='"+pass+"'");  
  39.        System.out.println("password updated successfully...");  
  40.       }  
  41.       else  
  42.       {  
  43.           System.out.println("password does not match");  
  44.       }  
  45.       }  
  46.       catch(Exception e){  
  47.           System.out.println("Employee class change pass method"+e);  
  48.       }}  
  49. void Cust_Details(String acc){  
  50.      try  
  51.      {  
  52.       ConnectDB ob = new ConnectDB();  
  53.       Connection con = ob.c();  
  54.       Statement stm = con.createStatement();  
  55.      ResultSet rst = stm.executeQuery("select * from customer where accno='"+acc+"'");  
  56.          Customer obj1=new Customer();  
  57.       obj1.display_Customer(acc);  
  58.      }  
  59.      catch(Exception e)  
  60.      {  
  61.       System.out.println("Employee see customer details method"+e);  
  62.      }  
  63. }}  
  64. public class EmployeeMain {  
  65.     public static void main(String[] args) throws Exception{  
  66.         try  
  67.         {  
  68.           BufferedReader in=new BufferedReader(new InputStreamReader(System.in));  
  69.         System.out.println("Employee..??");  
  70.         System.out.println("1-Yes or 2-No");  
  71.        int choice0=Integer.parseInt(in.readLine());  
  72.        switch(choice0)  
  73.        {  
  74.                case 1:  
  75.                     System.out.print("Enter the Employee Id: ");  
  76.                    String id =in.readLine();  
  77.                    System.out.print("Enter the password: ");  
  78.                    String pass = in.readLine();  
  79.                     ConnectDB ob = new ConnectDB();  
  80.                     Connection conx = ob.c();  
  81.                     Statement stmx = conx.createStatement();  
  82.                     ResultSet rstx = stmx.executeQuery("Select * from employee where                      EmpID='"+id+"' and password='"+pass+"'");  
  83.   
  84.                     Employee objE  =new Employee();  
  85.                     if(rstx.next())  
  86.                     {  
  87.                   System.out.println("1-Display my details");  
  88.                   System.out.println("2-Change password");  
  89.                   System.out.println("3-Display customer details");  
  90.                   System.out.print("Enter your choice: ");  
  91.                    int choice1=Integer.parseInt(in.readLine());  
  92.                   switch(choice1)  
  93.                   {  
  94.                       case 1:  
  95.                           System.out.println("Enter the id Again: ");  
  96.                           String idll =in.readLine();  
  97.                          objE.Employee_dtl(idll);  
  98.                           break;  
  99.                       case 2:objE.change_pass_emp(id, pass);  
  100.                           break;  
  101.                       case 3:  
  102.                        System.out.println("Enter Account number of customer: ");  
  103.                        String acct = in .readLine();  
  104.                           objE.Cust_Details(acct);  
  105.                          break;  
  106.                       default: System.out.println("wrong choice");  
  107.                   }  
  108.                  }  
  109.                     else{  
  110.                         System.out.println("Incorrect Match  occur ");  
  111.                     }  
  112.                          break;  
  113.            default: System.out.println("Thank you");  
  114.        }  }  
  115.       catch(Exception e)  
  116.       {  
  117.          System.out.println("main method:"+e);  
  118.       }  
  119.    }

ConnectDB.java

  1. package mydb;  
  2. import java.sql.*;  
  3.   
  4. public class ConnectDB {  
  5. public Connection c() throws Exception{  
  6.     Class.forName ("com.mysql.jdbc.Driver");  
  7.     Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/bankdb""root""toor");  
  8.     return con;  
  9.   }  

We will work for an employee named “Gops” for getting various outputs.

Gops

Here are the following outputs.

Output 1: Display customer details

We will get the details of the following two customers.

customers

The following shows the details of Shubham and Harshit:

Details of Shubham and Harshit

Output 2: Display my details

Display my detail

Output 3: Details of another employee “Abhi”

Details of another employee

Output 4: Change my password

Change my password

Output 5: If EmpID or Password are incorrect

If EmpID or Password are incorrect

In the preceding output we can see that the given EmpID is wrong and that is why the incorrect match occurred.

In the next part, the admin module has been discussed.



Thank you, keep learning and sharing.

Up Next
    Ebook Download
    View all
    Learn
    View all