7 Update an Employee Record

Create an Employee Java Bean:

Class Name:

src/main/java/com/oracle/jdbc/samples/entity/Employee.java

Use the Employee.java file that you created, earlier in the example.

7.1 Create a Method in Java Bean for Search by Employee’s First Name

Use the Employee.java that you created in the earlier exercise.

Class Name:

src/main/java/com/oracle/jdbc/samples/bean/JdbcBean.java

Create a new method getEmployeeByFn(String fn); in the bean

Following is the code to create a method for search by employee’s first name:

/**
* Get List of employees by First Name pattern
* @param fn
* @return List of employees with given beginning pattern
*/
public List<Employee> get EmployeeByFn(String fn);

7.2 Implement a Method getEmployeebyFn() for Search by Employee name

Class Name:

src/main/java/com/oracle/jdbc/samples/bean/JdbcBeanImpl.java

Use the getConnection() method that you created in the first step, to connect to the same database.

Then, create a method getEmployeeByFn() employee data based on employee’s first name.

SELECT Employee_Id, First_Name, Last Name, Email, Phone_Number, Job_Id, Salary FROM EMPLOYEES
WHERE First_Name LIKE ?
public List<Employee> getEmployeeByFn(String fn) {
List<Employee> returnValue = new ArrayList<>();
public List<Employee> getEmployeeByFn(String fn) {
List<Employee> returnValue = new ArrayList<>();
try (Connection connection = getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(
"SELECT Employee_Id, First_Name, Last_Name, Email, Phone_Number, Job_Id, Salary FROM 
EMPLOYEES WHERE First_Name LIKE ?")) {
preparedStatement.setString(1, fn + '%');
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while(resultSet.next()) {
returnValue.add(new Employee(resultSet));
}
}
}
} catch (SQLException ex) {
logger.log(Level.SEVERE, null, ex);
ex.printStackTrace();
}
return returnValue;
}