JDBC Using Model Object and Singleton Class
Last Updated :
12 Jul, 2025
In this article we will perform how to perform JDBC operations using a Model object and a Singleton connection class from a MySQL database. JDBC is an Application Programming Interface or Java which connects a Java application with a database to perform CRUD operations.
Pre-requisite:
Model Class: The model class is highly used in the MVC pattern, where it works as an intermediate medium between business logic and the view. Usually, a model contains some variables and methods of a specific entity.
Singleton Class: Singleton class is a software design pattern that ensures there will be one single instance of that class.There are multiple ways to achieve a singleton class.
Approach
We are going to create an Organization database that contains an Employee table. We will also create a java application that will connect with the Organization database. The connection will be established by a singleton class which will contain all the necessary driver information. From the java application, we will perform some data manipulation tasks like insert, delete, update and retrieve using a model object of Employee.
- Creating a MySQL database and a table:
create database org;
use org;
create table employee(
emp_id int auto_increment,
emp_name varchar(400),
emp_address varchar(400),
primary key (emp_id)
);
- Project setup in eclipse:
- Create a project in eclipse named 'jdbc'
- Create a folder on that project named 'jars' and paste MySQL J Connector on that folder
- Add jars to java build path
- Create 4 package: com.jdbc.util, com.jdbc.dao, com.jdbc.model and com.jdbc.main

- Database connection: Create a singleton connection class DatabaseConnection in com.jdbc.util package. Use your MySQL username and password on the variable user and pass. Look carefully at the last part of the "url" variable. It is mandatory to keep the name the same as the database name. The name of my database is "org" that's why I put "org" in the URL variable.
DatabaseConnection Class
package com.jdbc.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnection {
private static Connection con = null;
static
{
String url = "jdbc:mysql:// localhost:3306/org";
String user = "root";
String pass = "root";
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, user, pass);
}
catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection()
{
return con;
}
}
- Model class: Create a model class named Employee in com.jdbc.model package. It should contain all the attributes as variables.
Model Class
package com.jdbc.model;
public class Employee {
int emp_id;
String emp_name;
String emp_address;
public Employee() {}
public Employee(String emp_name, String emp_address)
{
this.emp_name = emp_name;
this.emp_address = emp_address;
}
public int getEmp_id()
{
return emp_id;
}
public void setEmp_id(int emp_id)
{
this.emp_id = emp_id;
}
public String getEmp_name()
{
return emp_name;
}
public void setEmp_name(String emp_name)
{
this.emp_name = emp_name;
}
public String getEmp_address()
{
return emp_address;
}
public void setEmp_address(String emp_address)
{
this.emp_address = emp_address;
}
@Override
public String toString()
{
return "Employee [emp_id=" + emp_id + ",
emp_name=" + emp_name + ",
emp_address=" + emp_address + "]";
}
}
- Database Access Object(DAO): We will create an EmployeeDao interface and another class EmployeeDaoImplementation which implements EmployeeDao. This implemented class will be used as a DAO to perform CRUD operations. We will use PreparedStatement to execute the query. PreparedStatement has 3 special methods:
- executeQuery(): used to retrieve data
- executeUpdate(): used to insert, update, delete
- execute(): used to create
EmployeeDao Interface
package com.jdbc.dao;
import java.sql.SQLException;
import java.util.List;
import com.jdbc.model.Employee;
public interface EmployeeDao {
public int add(Employee emp)
throws SQLException;
public void delete(int id)
throws SQLException;
public Employee getEmployee(int id)
throws SQLException;
public List<Employee> getEmployees()
throws SQLException;
public void update(Employee emp)
throws SQLException;
}
EmployeeDaoImplementation Class
package com.jdbc.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.jdbc.model.Employee;
import com.jdbc.util.DatabaseConnection;
public class EmployeeDaoImplementation
implements EmployeeDao {
static Connection con
= DatabaseConnection.getConnection();
@Override
public int add(Employee emp)
throws SQLException
{
String query
= "insert into employee(emp_name, "
+ "emp_address) VALUES (?, ?)";
PreparedStatement ps
= con.prepareStatement(query);
ps.setString(1, emp.getEmp_name());
ps.setString(2, emp.getEmp_address());
int n = ps.executeUpdate();
return n;
}
@Override
public void delete(int id)
throws SQLException
{
String query
= "delete from employee where emp_id =?";
PreparedStatement ps
= con.prepareStatement(query);
ps.setInt(1, id);
ps.executeUpdate();
}
@Override
public Employee getEmployee(int id)
throws SQLException
{
String query
= "select * from employee where emp_id= ?";
PreparedStatement ps
= con.prepareStatement(query);
ps.setInt(1, id);
Employee emp = new Employee();
ResultSet rs = ps.executeQuery();
boolean check = false;
while (rs.next()) {
check = true;
emp.setEmp_id(rs.getInt("emp_id"));
emp.setEmp_name(rs.getString("emp_name"));
emp.setEmp_address(rs.getString("emp_address"));
}
if (check == true) {
return emp;
}
else
return null;
}
@Override
public List<Employee> getEmployees()
throws SQLException
{
String query = "select * from employee";
PreparedStatement ps
= con.prepareStatement(query);
ResultSet rs = ps.executeQuery();
List<Employee> ls = new ArrayList();
while (rs.next()) {
Employee emp = new Employee();
emp.setEmp_id(rs.getInt("emp_id"));
emp.setEmp_name(rs.getString("emp_name"));
emp.setEmp_address(rs.getString("emp_address"));
ls.add(emp);
}
return ls;
}
@Override
public void update(Employee emp)
throws SQLException
{
String query
= "update employee set emp_name=?, "
+ " emp_address= ? where emp_id = ?";
PreparedStatement ps
= con.prepareStatement(query);
ps.setString(1, emp.getEmp_name());
ps.setString(2, emp.getEmp_address());
ps.setInt(3, emp.getEmp_id());
ps.executeUpdate();
}
}
- Test the application: Finally, its time to perform the CRUD application using all the methods of EmployeeDaoImplementation. Create a class Application in com.jdbc.main package.
Driver code
package com.jdbc.main;
import java.sql.SQLException;
import java.util.List;
import com.jdbc.dao.EmployeeDaoImplementation;
import com.jdbc.model.Employee;
public class Application {
public static void main(String[] args)
throws SQLException
{
Employee emp = new Employee();
emp.setEmp_name("Haider");
emp.setEmp_address("Mars");
EmployeeDaoImplementation empDao
= new EmployeeDaoImplementation();
// add
empDao.add(emp);
// read
Employee e = empDao.getEmployee(1);
System.out.println(e.getEmp_id() + " "
+ e.getEmp_name() + " "
+ e.getEmp_address());
// read All
List<Employee> ls = empDao.getEmployees();
for (Employee allEmp : ls) {
System.out.println(allEmp.toString());
}
// update
Employee tempEmployee = empDao.getEmployee(1);
tempEmployee.setEmp_address("Asgard");
empDao.update(tempEmployee);
// delete
empDao.delete(1);
}
}
Output:
- For the insert operation, you have to look to your Employee table in the Org database.

- For read one item, use id to fetch data and print it to console.

- For displaying all the items, just call the method and print it to console.

- The update operation should change the updated value into the database.

- The delete operation will delete the information of that id from the database.

Similar Reads
Spring Boot - Dependency Injection and Spring Beans Spring Boot is a powerful framework for building RESTful APIs and microservices with minimal configuration. Two fundamental concepts within Spring Boot are Dependency Injection (DI) and Spring Beans. Dependency Injection is a design pattern used to implement Inversion of Control (IoC), allowing the
6 min read
Java Spring - Using @Scope Annotation to Set a POJO's Scope In the Spring Framework, when we declare a POJO (Plain Old Java Object) instance, what we are essentially creating is a template for a bean definition. This means that, just like a class, we can have multiple object instances created from a single template. These beans are managed by the Spring IoC
7 min read
Spring Boot - How to Access Database using Spring Data JPA Spring Data JPA is a robust framework that simplifies the implementation of JPA (Java Persistence API) repositories, making it easy to add a data access layer to the applications. CRUD (Create, Retrieve, Update, Delete) operations are the fundamental actions we can perform on a database. In this art
5 min read
How to Create Your First Model in Spring MVC? Spring MVC is a powerful Web MVC framework for building web applications. It is designed around the Model-View-Controller (MVC) pattern, which separates the application into three main components:Model: Represents the data of the application. It can be a single object or a collection of objects.View
6 min read
Spring MVC and Hibernate CRUD Example In this article, we will be developing CRUD operations in Spring MVC and Hibernate. Hibernate is an object-relational mapping (ORM) framework. Developers use Hibernate to interact with databases using Java objects rather than SQL queries. Spring MVC is a Model-View-Controller (MVC) framework used to
6 min read
How to Create a JPA Project using IntelliJ IDEA Ultimate? The Java Persistence API (JPA) is a Java specification for accessing, persisting, and maintaining data between Java objects and a relational database. IntelliJ IDEA Ultimate provides robust support for the JPA frameworks making it the ideal environment for developing database-driven applications usi
3 min read