Defining the JavaBean Properties and Methods

In the JavaBean, you must create one field for each column in the Employees table, and accessor methods (get and set methods) for each field.

  1. Add an import statement for java.sql.Date, which is the field type for one of the fields:

    import java.sql.Date;
    
  2. Add a field to the Employee class for each of the columns in the Employees table. Each field is private, and the field types are as follows:

      private Integer employeeId;
      private String firstName;
      private String lastName;
      private String email;
      private String phoneNumber;
      private Date hireDate;
      private String jobId;
      private Double salary;
      private Double commissionPct;
      private Integer departmentId;
    
  3. Right-click the Source Editor page and select Generate Accessors from the shortcut menu. In the Generate Accessors dialog box, select the top-level Employee node. A check mark is displayed for that node and for all the fields. Click OK. Figure 5-2 shows the Generate Accessors dialog box with all the fields selected.

    Figure 5-2 Generate Accessors Dialog Box

    Description of Figure 5-2 follows
    Description of "Figure 5-2 Generate Accessors Dialog Box"
  4. Save the file. The Employee.java file should now contain the following code:

    Skeleton Code for a Basic Java Bean with Accessor Methods

    package hr;
    import java.sql.Date;
     
    public class Employee {
        public Employee() {
        }
        private Integer employeeId;
        private String firstName;
        private String lastName;
        private String email;
        private String phoneNumber;
        private Date hireDate;
        private String jobId;
        private Double salary;
        private Double commissionPct;
        private Integer departmentId;
    
        public void setEmployeeId(Integer employeeId) {
            this.employeeId = employeeId;
        }
     
        public Integer getEmployeeId() {
            return employeeId;
        }
     
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
     
        public String getFirstName() {
            return firstName;
        }
    ...
    ...
    ...
    ...
    // This list has been shortened and is not comprehensive.
    // The actual code contains accessor methods
    // for all the fields declared in the bean.
    
        public void setDepartmentId(Integer departmentId) {
            this.departmentId = departmentId;
        }
     
        public Integer getDepartmentId() {
            return departmentId;
        }
    }