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