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:
- 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.- Declare the package for the
JavaBean.java
class. Import theEmployee
class as it contains the employee details.package com.oracle.jdbc.samples.bean; import com.oracle.jdbc.samples.entity.Employee;
- 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;
- Declare the
JavaBeanImpl
class that implementsJavaBean
.public class JavaBeanImpln implements JavaBean { }
- Inside the
JavaBeanImpl
class, create a logger to log exceptions.static final Logger logger = Logger.getLogger("com.oracle.jdbc.samples.bean.JdbcBeanImpl");
- Inside the
JavaBeanImpl
class, declare a static methodgetConnection()
. ThegetConection()
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; }
- Declare the package for the
- Create a method
getEmployees()
to retrieve a list of employees from theEMPLOYEES
table. Update theSELECT
query to be used by choosing the columns that you want from theEMPLOYEES
table.- Inside the
JavaBeanImpl
class, declare the methodgetEmployees()
. - Use
try
andcatch
blocks to establish a database connection and fetch the employee details using a SQLSELECT
statement. - Store the employee details in an array
returnValue
. - 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; }
- Inside the
Note:
This topic describes how to add the implementation method of List All functionality. Similarly, you learn to addgetEmployee
, updateEmployee
,
getEmployeeByFn
and incrementSalary
implementation
methods for other functionalities of the HR Web Application in the upcoming chapters.