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:
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. public class ConnectDB {
  4. public Connection c() throws Exception{
  5. Class.forName ("com.mysql.jdbc.Driver");
  6. Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/bankdb", "root", "toor");
  7. return con;
  8. }
  9. }
AdminMain.java
  1. import java.sql.*;
  2. import java.io.*;
  3. import mydb.ConnectDB;
  4. class Admin{
  5. BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
  6. void Dtl_of_Customer(String accnt)
  7. {
  8. try{
  9. ConnectDB ob = new ConnectDB();
  10. Connection con = ob.c();
  11. Statement stm = con.createStatement();
  12. ResultSet rst = stm.executeQuery("select * from customer where accno='"+accnt+"'");
  13. Customer obj1=new Customer();
  14. obj1.display_Customer(accnt);
  15. }
  16. catch(Exception e)
  17. {
  18. System.out.println("Admin see customer details method"+e);
  19. }
  20. }
  21. void Dtl_of_emp(String id)
  22. {
  23. try
  24. {
  25. ConnectDB ob = new ConnectDB();
  26. Connection con = ob.c();
  27. Statement stm = con.createStatement();
  28. ResultSet rst = stm.executeQuery("select * from employee where EmpID='"+id+"'");
  29. Employee obj2=new Employee();
  30. obj2.Employee_dtl(id);
  31. }
  32. catch(Exception e)
  33. {
  34. System.out.println("Admin see employee detail method"+e);
  35. }
  36. }
  37. void Password_change_cust(String acc)
  38. {
  39. try
  40. {
  41. ConnectDB ob = new ConnectDB();
  42. Connection con = ob.c();
  43. Statement stm = con.createStatement();
  44. System.out.print("Enter customer's new password: ");
  45. String p1=in.readLine();
  46. System.out.print("Confirm password: ");
  47. String p2=in.readLine();
  48. if(p1.equals(p2))
  49. {
  50. stm.executeUpdate("update customer set password='"+p1+"' where accno='"+acc+"'");
  51. System.out.println("password updated successfully...");
  52. }
  53. else
  54. {
  55. System.out.println("password does not match");
  56. }
  57. }
  58. catch(Exception e)
  59. {
  60. System.out.println("Change cust pass method"+e);
  61. }
  62. }
  63. void Password_change_emp(String id)
  64. {
  65. try
  66. {
  67. ConnectDB ob = new ConnectDB();
  68. Connection con = ob.c();
  69. Statement stm = con.createStatement();
  70. System.out.print("Enter employee's new password: ");
  71. String p1=in.readLine();
  72. System.out.print("Confirm password: ");
  73. String p2=in.readLine();
  74. if(p1.equals(p2))
  75. {
  76. stm.executeUpdate("update employee set password='"+p1+"' where EmpID='"+id+"'");
  77. System.out.println("password updated successfully...");
  78. }
  79. else
  80. {
  81. System.out.println("password does not match");
  82. }
  83. }
  84. catch(Exception e)
  85. {
  86. System.out.println("Change cust emp method"+e);
  87. }
  88. }
  89. void Change_my_pass(String y,String p)
  90. {
  91. try
  92. {
  93. ConnectDB ob = new ConnectDB();
  94. Connection con = ob.c();
  95. Statement stm = con.createStatement();
  96. System.out.print("Enter new password: ");
  97. String p1=in.readLine();
  98. System.out.print("Confirm password: ");
  99. String p2=in.readLine();
  100. if(p1.equals(p2))
  101. {
  102. stm.executeUpdate("update employee set password='"+p1+"' where EmpID='"+y+"' and password='"+p+"'");
  103. System.out.println("password updated successfully...");
  104. }
  105. else
  106. {
  107. System.out.println("password does not match");
  108. }
  109. }
  110. catch(Exception e){
  111. System.out.println("Change my pass method"+e);
  112. }
  113. }
  114. }
  115. public class AdminMain {
  116. public static void main(String[] args) throws Exception{
  117. try
  118. {
  119. BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
  120. System.out.println("Administrator..??");
  121. System.out.println("1-Yes or 2-No");
  122. int choice0=Integer.parseInt(in.readLine());
  123. String acc;
  124. switch(choice0)
  125. {
  126. case 1:
  127. System.out.print("Enter the Username of Admin: ");
  128. String y = in.readLine();
  129. System.out.print("Enter the password: ");
  130. String p = in.readLine();
  131. ConnectDB obm = new ConnectDB();
  132. Connection con = obm.c();
  133. Statement stm = con.createStatement();
  134. ResultSet rst = stm.executeQuery("select * from admin where username='"+y+"' and password = '"+p+"'");
  135. Admin oba = new Admin();
  136. if(rst.next())
  137. {
  138. System.out.println("1-Display details of any customer");
  139. System.out.println("2-Display details of any employee");
  140. System.out.println("3-Change password of any customer");
  141. System.out.println("4-Change password of any employee");
  142. System.out.println("5-Change my password");
  143. System.out.print ("enter your choice: ");
  144. int choice11=Integer.parseInt(in.readLine());
  145. switch(choice11)
  146. {
  147. case 1:System.out.print("Enter A/C of any customer: ");
  148. String acct =in.readLine();
  149. oba.Dtl_of_Customer(acct);
  150. break;
  151. case 2:System.out.print("Enter Id of any employee: ");
  152. String id =in.readLine();
  153. oba.Dtl_of_emp(id);
  154. break;
  155. case 3:System.out.print("Enter any customer's A/C #: " );
  156. String accn=in.readLine();
  157. oba.Password_change_cust(accn);
  158. break;
  159. case 4:System.out.print("Enter any employee's Id: ");
  160. String empid=in.readLine();
  161. oba.Password_change_emp(empid);
  162. break;
  163. case 5:oba.Change_my_pass(y, p);
  164. break;
  165. default:System.out.println("Wrong choice");
  166. }
  167. }
  168. else
  169. {
  170. System.out.println("Invalid A/C or password");
  171. }
  172. break;
  173. default:System.out.println("Thank you");
  174. }
  175. }
  176. catch(Exception e)
  177. {
  178. System.out.println("main method:"+e);
  179. }
  180. }
  181. }
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.
Accessing Database Using Java and MySQL: Part 5
Thank you, keep learning and sharing.