5.1 Creating a Java Bean Entity for an Employee

The Employee class contains the getter and setter methods for all attributes of an employee. For example, the First_name has a getter and a setter method like getFirst_Name and setFirst_Name respectively.

Class Name: src/main/java/com/oracle/jdbc/samples/entity/Employee.java

Github Location: Employee.java

Steps to create Employee.java:

  1. Declare the package for the class Employee.java.
    package com.oracle.jdbc.samples.entity;
  2. Import the following packages required for the Employee class.
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Timestamp;
  3. Declare an Employee class. Add a pair of parenthesis ({ }). Place the cursor in between the parenthesis:
    public class Employee {}
  4. Declare the following variables for each one of the attributes of an employee.
    private int Employee_Id;
    private String First_Name;
    private String Last_Name;
    private String Email;
    private String Phone_Number;
    private String Job_Id;
    private int Salary;
  5. Create a constructor for the Employee class which takes ResultSet as the input and throws a SQLException. In this constructor, set all the values for the attributes of the Employee class.
    public Employee(ResultSet resultSet) throws SQLException {
      this.Employee_Id = resultSet.getInt(1);
      this.First_Name = resultSet.getString(2);
      this.Last_Name = resultSet.getString(3);
      this.Email = resultSet.getString(4);
      this.Phone_Number = resultSet.getString(5);
      this.Job_Id = resultSet.getString(6);
      this.Salary = resultSet.getInt(7);
    }
    
  6. Create the Getter and Setter methods, that is, getX and setX methods to get and set the values for all attributes of the Employee such as Employee_id, first_name, last_name, salary, and so on. For example, the getter and setter methods for the Employee_Id is as follows:
    public int getEmployee_Id() {
       return Employee_Id;
    }
      public void setEmployee_Id(int Employee_Id) {
         this.Employee_Id = Employee_Id;
      }