VOOZH about

URL: https://www.geeksforgeeks.org/java/how-to-enforce-referential-integrity-using-foreign-keys-in-jdbc/

⇱ How to Enforce Referential Integrity using Foreign Keys in JDBC? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Enforce Referential Integrity using Foreign Keys in JDBC?

Last Updated : 26 Apr, 2024

Enforcing referential integrity is the fundamental aspect of maintaining data consistency and integrity in a relational database. Referential integrity ensured that the relationships between the tables remained valid, preventing orphaned or inconsistent data. The use of the foreign key is one of the mechanisms for enforcing referential integrity in a database management system (DBMS). In Java Database Connectivity (JDBC), java API is used for interacting with the databases, enforcing the referential integrity using the foreign keys involved the defining and managing these constraints programmatically in your application.

Prerequisites

The following are prerequisites to enforce referential integrity using foreign keys in JDBC:

  1. Knowledge of Relational Databases
  2. Knowledge of Java Programming
  3. Understanding the fundamentals of JDBC (Java Database Connectivity)
  4. Database Management System
  5. Integrated Development Environment (IDE)

Note: If you met above prerequisites, you can easily enforce referential integrity using foreign keys in JDBC .

Enforce Referential Integrity using Foreign Keys in JDBC

Step 1: Create Database Schema

Create two tables in your database, and name it as "Customers" and "Orders".

The below figure shows the tables of Customers and Orders.

👁 Database Table

The above tables doesn't contain any rows.

Step 2: Set Up Eclipse Project

  1. Open Eclipse and create one new Java Project.
  2. Add JDBC driver library into your Java project's build path. If you are using the MySQL, make sure you would add the MySQL JDBC driver jar file.

Here is the path for JDBC driver library that means MySQL JDBC driver jar file.

👁 path for MySQL JDBC driver jar file

Step 3: Create Java Classes and implement the code

Create a Customer.java class file in src folder in your Java project, write below code in it.


Create a Order.java class file in src folder in your Java project, write below code in it.


Create a DatabaseConnector.java class file in src folder in your Java project, write below code in it.

Note: Make sure that you have to change the database url, username, password according to your database.


Create a CustomerDAO.java class file in src folder in your Java project, write below code in it.


Create a OrderDAO.java class file in src folder in your Java project, write below code in it.


Create a TestApplication.java class file in src folder in your Java project, write below code in it. This code is used to insert the data into the database.


Step 4: Run the Application

  • Run the above code, right click on the Java project in Project Explore.
  • Select Run As > Java Application.

The output will be shown in console window in your Eclipse as shown below:

👁 Output


Step 5: Verification

If you want to check the data is successfully inserted or not. you can check the tables in your database. If the data is present in the table, it is successfully inserted into your table, otherwise it is not inserted into your table.

Example:

👁 Verification

The above two tables are inserted the rows successfully.

Comment
Article Tags: