Accessing Database Using Java and MySQL: Part 4

The preceding article begins with the explanation of the administrator module for various transactions.
Before reading further, read the previous part of this article.

Transactions involved for Authorized Administrator

 
Authorized
 
The administrators are the personnel authorized for every type of transaction or process related to employees or customers or about him/herself. Likewise, this administrator module also contains such types of transactions associated with customers or employees.
 
This module is also made by the two separate Java files AdminMain.java and ConnectDB.java.
 
The following are transactions associated with the administrator:
  • Can check details of any customer.
  • Can check details of any employee.
  • Can change the password of any customer.
  • Can change the password of any employee.
  • Can change his/her password.
The database schema table created for the admin is the following:
 
admin schema table
 
For now, we are working with admin schema table, so the following are the contents of the admin table.
 
admin module
 
Now we are in a position to see the code example for the admin module.
 
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.   }  
  10. }  
AdminMain.java 
  1. import java.sql.*;  
  2. import java.io.*;  
  3. import mydb.ConnectDB;  
  4.   
  5. class Admin{  
  6.     BufferedReader in = new BufferedReader (new InputStreamReader (System.in));  
  7. void Dtl_of_Customer(String accnt)  
  8. {  
  9.     try{  
  10.       ConnectDB ob = new ConnectDB();  
  11.       Connection con = ob.c();  
  12.       Statement stm = con.createStatement();  
  13.        ResultSet rst = stm.executeQuery("select * from customer where accno='"+accnt+"'");  
  14.   
  15.       Customer obj1=new Customer();  
  16.       obj1.display_Customer(accnt);  
  17. }  
  18.     catch(Exception e)  
  19.     {  
  20. System.out.println("Admin see customer details method"+e);  
  21. }  
  22.    }  
  23. void Dtl_of_emp(String id)  
  24. {  
  25.     try  
  26.     {  
  27.       ConnectDB ob = new ConnectDB();  
  28.       Connection con = ob.c();  
  29.       Statement stm = con.createStatement();  
  30.        ResultSet rst = stm.executeQuery("select * from employee where EmpID='"+id+"'");  
  31.   
  32.        Employee obj2=new Employee();  
  33.        obj2.Employee_dtl(id);  
  34. }  
  35.     catch(Exception e)  
  36.     {  
  37.         System.out.println("Admin see employee detail method"+e);  
  38.     }  
  39. }  
  40.     void Password_change_cust(String acc)  
  41.     {  
  42.         try  
  43.         {  
  44.         ConnectDB ob = new ConnectDB();  
  45.         Connection con = ob.c();  
  46.         Statement stm = con.createStatement();  
  47.         System.out.print("Enter customer's new password: ");  
  48.         String p1=in.readLine();  
  49.         System.out.print("Confirm password: ");  
  50.         String p2=in.readLine();  
  51.         if(p1.equals(p2))  
  52.       {  
  53.        stm.executeUpdate("update customer set password='"+p1+"' where accno='"+acc+"'");  
  54.        System.out.println("password updated successfully...");  
  55.       }  
  56.       else  
  57.       {  
  58.           System.out.println("password does not match");  
  59.       }  
  60.         }  
  61.         catch(Exception e)  
  62.         {  
  63.             System.out.println("Change cust pass method"+e);  
  64.         }  
  65. }  
  66.     void Password_change_emp(String id)  
  67.     {  
  68.         try  
  69.         {  
  70.         ConnectDB ob = new ConnectDB();  
  71.         Connection con = ob.c();  
  72.         Statement stm = con.createStatement();  
  73.         System.out.print("Enter employee's new password: ");  
  74.         String p1=in.readLine();  
  75.         System.out.print("Confirm password: ");  
  76.         String p2=in.readLine();  
  77.         if(p1.equals(p2))  
  78.       {  
  79.        stm.executeUpdate("update employee set password='"+p1+"' where EmpID='"+id+"'");  
  80.        System.out.println("password updated successfully...");  
  81.       }  
  82.       else  
  83.       {  
  84.           System.out.println("password does not match");  
  85.       }  
  86.         }  
  87.         catch(Exception e)  
  88.         {  
  89.        System.out.println("Change cust emp method"+e);  
  90.     }  
  91.     }  
  92.     void Change_my_pass(String y,String p)  
  93.     {  
  94.         try  
  95.         {  
  96.              ConnectDB ob = new ConnectDB();  
  97.         Connection con = ob.c();  
  98.         Statement stm = con.createStatement();  
  99.         System.out.print("Enter new password: ");  
  100.         String p1=in.readLine();  
  101.         System.out.print("Confirm password: ");  
  102.         String p2=in.readLine();  
  103.         if(p1.equals(p2))  
  104.       {  
  105.        stm.executeUpdate("update employee set password='"+p1+"' where EmpID='"+y+"' and password='"+p+"'");  
  106.        System.out.println("password updated successfully...");  
  107.       }  
  108.       else  
  109.       {  
  110.           System.out.println("password does not match");  
  111.       }  
  112.         }  
  113.         catch(Exception e){  
  114.             System.out.println("Change my pass method"+e);  
  115.         }  
  116.     }  
  117. }  
  118. public class AdminMain {  
  119.     public static void main(String[] args) throws Exception{  
  120.         try  
  121.         {  
  122.           BufferedReader in=new BufferedReader(new InputStreamReader(System.in));  
  123.         System.out.println("Administrator..??");  
  124.         System.out.println("1-Yes or 2-No");  
  125.        int choice0=Integer.parseInt(in.readLine());  
  126.       String acc;  
  127.        switch(choice0)  
  128.        {  
  129.          case 1:  
  130.                    System.out.print("Enter the Username of Admin: ");  
  131.                    String y = in.readLine();  
  132.                    System.out.print("Enter the password: ");  
  133.                    String p = in.readLine();  
  134.                     ConnectDB obm = new ConnectDB();  
  135.                     Connection con = obm.c();  
  136.                     Statement stm = con.createStatement();  
  137.                     ResultSet rst = stm.executeQuery("select * from admin where username='"+y+"' and password = '"+p+"'");  
  138.                     Admin oba = new Admin();  
  139.                     if(rst.next())  
  140.                     {  
  141.                     System.out.println("1-Display details of any customer");  
  142.                     System.out.println("2-Display details of any employee");  
  143.                     System.out.println("3-Change password of any customer");  
  144.                     System.out.println("4-Change password of any employee");  
  145.                     System.out.println("5-Change my password");  
  146.                     System.out.print ("enter your choice:  ");  
  147.                     int choice11=Integer.parseInt(in.readLine());  
  148.                     switch(choice11)  
  149.                     {  
  150.                         case 1:System.out.print("Enter A/C of any customer: ");  
  151.                                String acct =in.readLine();  
  152.                                oba.Dtl_of_Customer(acct);  
  153.                             break;  
  154.                         case 2:System.out.print("Enter Id of any employee: ");  
  155.                         String id =in.readLine();  
  156.                             oba.Dtl_of_emp(id);  
  157.                             break;  
  158.                         case 3:System.out.print("Enter any customer's A/C #: "  );  
  159.                                String accn=in.readLine();  
  160.                             oba.Password_change_cust(accn);  
  161.                             break;  
  162.                         case 4:System.out.print("Enter any employee's Id: ");  
  163.                               String empid=in.readLine();  
  164.                             oba.Password_change_emp(empid);  
  165.                             break;  
  166.                         case 5:oba.Change_my_pass(y, p);  
  167.                             break;  
  168.                         default:System.out.println("Wrong choice");  
  169.                     }  
  170.                     }  
  171.                   else  
  172.                   {  
  173.                    System.out.println("Invalid A/C or password");  
  174.                   }  
  175.                    break;  
  176.                    default:System.out.println("Thank you");  
  177.               }  
  178.           }  
  179.           catch(Exception e)  
  180.           {  
  181.              System.out.println("main method:"+e);  
  182.          }  
  183.     }
  184. }  
Various outputs
 
Output 1: Can check details of any customer.
 
Can check details of any customer
 
Here are the outputs of checking the details of two customers by the two admins “eddys” and “ashish” for “gaurav” and “rahul”.
 
details of two customers
 
Output 2: Can check details of any employee.
 
check details
 
Again here are the outputs of checking the details of the two employees by two admins “avadh” and “ashish” for “Gops” and “Abhi”.
 
outputs
 
Output 3: Can change the password of any customer.
 
 
 
change password of any customer
 
The Customer password is now changed.
 
Customer password got changed
 
Output 4: Can change the password of any employee.
 
employee
 
change password of any employee
  
The Employee password is now changed.
  
Employee password
 
Output 5: Can change his/her password.
  
change password
  
Likewise, you can also add other authorization for an admin or customer or an employee.
 
The next part explains the entire module together in which a person gets the option to choose his status using the authentication process and it will be some other aspect.
 
 
Thank you, keep learning and sharing.