Java tutorial with CRUD examples
Java CRUD Tutorial with Examples
In this tutorial, we'll cover how to perform CRUD operations (Create, Read, Update, Delete) in Java using JDBC (Java Database Connectivity) and a MySQL database. We'll walk through setting up the database, creating a Java application to interact with the database, and performing the basic CRUD operations.
1. Prerequisites
Before we start, ensure you have the following:
- Java Development Kit (JDK) installed.
- MySQL database running.
- MySQL Connector/J for JDBC (used to connect Java to MySQL).
- IDE like IntelliJ IDEA, Eclipse, or Visual Studio Code for writing Java code.
2. Setting Up the MySQL Database
We'll start by creating a database and a table called Users
to perform CRUD operations.
- Create a MySQL Database:
Open MySQL Workbench or any MySQL client, and run the following SQL commands to create the database and a
Users
table.
This creates a Users
table with the following columns:
id
: A unique identifier for each user (Primary Key).first_name
: The first name of the user.last_name
: The last name of the user.email
: The email address of the user.
- Insert some sample data into the table (optional):
3. Add MySQL JDBC Driver
To connect Java with MySQL, we need to add the MySQL JDBC driver to the project.
- If you're using Maven, add this dependency in your
pom.xml
:
- If you're using IntelliJ IDEA, you can also add the jar file directly or use Maven/Gradle to manage dependencies.
4. Create a Java Program for CRUD Operations
Now, let’s create a Java program to perform CRUD operations on the Users
table.
- Import Required Libraries:
- Database Connection Setup:
To interact with the MySQL database, we'll create a utility method to establish a connection.
5. CRUD Operations in Java
Now, let’s implement the actual CRUD operations.
Create Operation (INSERT)
PreparedStatement
: Used to avoid SQL injection and securely insert data into the database.
Read Operation (SELECT)
ResultSet
: Holds the result set of theSELECT
query.Statement
: Used for executing simple queries.
Update Operation (UPDATE)
executeUpdate()
: Executes queries that modify the database (e.g.,INSERT
,UPDATE
,DELETE
).
Delete Operation (DELETE)
DELETE FROM
: Deletes records from the database based on a condition (here,WHERE id = ?
).
6. Main Method to Test CRUD Operations
Now, let’s add a simple main
method to interact with the user and perform the CRUD operations.
7. Running the Program
- Compile and run the
CRUDApp.java
file. - You can test the CRUD operations by choosing the corresponding menu options:
- Create a new user.
- Read and display all users.
- Update a user’s information.
- Delete a user by ID.
- Set up a MySQL database and a
Users
table. - Implemented Create, Read, Update, and Delete operations using JDBC.
- Created a simple console application in Java to interact with the database.
8. Conclusion
In this tutorial, we:
This basic structure can be expanded to build more complex applications by adding error handling, validation, and more advanced features like transactions or using an ORM (Object-Relational Mapping) tool like Hibernate.
Comments
Post a Comment