5.3 Creating a Java Bean Implementation for a JDBC Connection

The JdbcBeanImpl.java is an implementation class that implements all the methods declared in the JdbcBean.java.

Class Name: src/main/java/com/oracle/jdbc/samples/bean/JdbcBeanImpl.java

Github Location: JdbcBeanImpl.java

Steps to create JdbcBeanImpl.java:

  1. Create a method getConnection() to establish a connection to the database. Ensure that you update the connection string, the database username and the database password to point to your database.
    1. Declare the package for the JavaBean.java class. Import the Employee class as it contains the employee details.
      
      package com.oracle.jdbc.samples.bean;
      import com.oracle.jdbc.samples.entity.Employee;
    2. Import the other dependent classes as shown in the following code snippet. If a particular class is not imported, then IntelliJ displays a message reminding you to import the required package.
      import java.sql.*;
      import java.util.ArrayList;
      import java.util.List;
      import java.util.logging.Level;
      import java.util.logging.Logger;
      
      import java.sql.PreparedStatement;
      import oracle.jdbc.OracleStatement;
      import oracle.jdbc.OracleConnection;
      import oracle.jdbc.driver.OracleDriver;
      import oracle.jdbc.OracleTypes;
      import java.sql.PreparedStatement;
      import oracle.jdbc.OracleStatement;
      import oracle.jdbc.OracleConnection;
      
      import oracle.ucp.jdbc.PoolDataSourceFactory;
      import oracle.ucp.jdbc.PoolDataSource;
      
    3. Declare the JavaBeanImpl class that implements JavaBean.
      public class JavaBeanImpln implements JavaBean {  }
    4. Inside the JavaBeanImpl class, create a logger to log exceptions.
      static final Logger logger = Logger.getLogger("com.oracle.jdbc.samples.bean.JdbcBeanImpl");
    5. Inside the JavaBeanImpl class, declare a static method getConnection(). The getConection() method registers the driver and establishes the database connection by passing the connection string, the database username and the database password as shown in the following code snippet.
      
      public static Connection getConnection() throws SQLException {
        DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
        Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@//myorclhost:5521/myorcldbservice", "hr", "hr");
        return connection;
      }
  2. Create a method getEmployees() to retrieve a list of employees from the EMPLOYEES table. Update the SELECT query to be used by choosing the columns that you want from the EMPLOYEES table.
    1. Inside the JavaBeanImpl class, declare the method getEmployees().
    2. Use try and catch blocks to establish a database connection and fetch the employee details using a SQL SELECT statement.
    3. Store the employee details in an array returnValue.
    4. Catch the SQLException and log the message in logger.
    
    public List<Employee> getEmployees() {
      List<Employee> returnValue = new ArrayList<>();
      try (Connection connection = getConnection()) {
         try (Statement statement = connection.createStatement()) {
             try (ResultSet resultSet = statement.executeQuery("
                  SELECT Employee_Id, First_Name, Last_Name, Email, Phone_Number, Job_Id, Salary 
                   FROM EMPLOYEES")) {
                while(resultSet.next()) {
                  returnValue.add(new Employee(resultSet));
                }
            }
          }
      } catch (SQLException ex) {
    logger.log(Level.SEVERE, null, ex);
        ex.printStackTrace();
      }
    
    return returnValue;
    }

Note:

This topic describes how to add the implementation method of List All functionality. Similarly, you learn to add getEmployee, updateEmployee, getEmployeeByFn and incrementSalary implementation methods for other functionalities of the HR Web Application in the upcoming chapters.