If you are currently working on a Database Design and Development assignment, you know the pain of transforming a database from 1NF to 3NF. But after the normalization nightmare comes another challenge: connecting your Java application to your Oracle database.
When Java developers sit down at their IDEs like JetBrains or NetBeans to write their CRUD operations like Create, Read, Update, Delete, they soon find that their code is a mess. They are copying and pasting their database connection string into a lot of different methods, failing to close database connections that cause timeout exceptions, and concatenating strings to build SQL queries that leave themselves vulnerable to SQL injection attacks.
Today, we’re going to solve this problem once and for all by creating a clean and secure object-oriented programming interface to our Oracle database using standard Java JDBC.
THE PROBLEM: THE COPY-PASTE ANTI-PATTERN
Here is what a standard, messy database call looks like when you put everything in one place:

This code is a major security risk and breaks the principle of DRY (Don't Repeat Yourself). If a user were to input ' OR '1'='1 as their username, then the query would be compromised.
The Solution: A Dedicated Database Utility Class
To ensure that our application interacts with Oracle in a consistent manner, it is necessary to refactor the database connection logic into a single class.
Step 1: The Database Manager
We will create a class called DatabaseHelper. This class centralises the connection string so that any changes to the password or server are required only in this class.

Step 2: Secure CRUD Using Prepared Statements
Let’s now re-implement our INSERT method with our new helper and a prepared statement. Prepared statements will automatically escape our user input and prevent SQL injection entirely.
We’ll use a try-with-resources statement. This is a really nice feature of Java that ensures your database connections and statements are closed right after you finish with them, no matter how you left your method. No more timeout exceptions!

THE ARCHITECTURAL WIN
By structuring your code like this, you have demonstrated your understanding of a Data Access Object (DAO) pattern.
Security: Using a PreparedStatement prevents SQL injection attacks.
Stability: The use of try-with-resources prevents memory leaks and Oracle connection limits from being exceeded.
Maintainability: Your CRUD operations are completely decoupled from your UI layer and your connection string.
HND WARNING: KEEP IT SIMPLE
When researching database connectivity for Java projects, you will most likely come across various Enterprise Java frameworks like Hibernate, JPA, or Spring Data. However, remember that your solution doesn’t need to be more complicated than a standard unit question asks. While ORMs are great tools, creating your own ORM system without understanding the fundamentals of JDBC is a recipe for a complicated mess that is hard to grade. A clean and well-structured JDBC utility class using Prepared Statements is exactly what a Lecturer wants to see to ensure that you know your data access patterns.
Master the basic wrapper first, and you will ace the unit.

