CRUD(Create,Read,Update and Delete) Operation using JDBC Statement

By | July 15, 2018

In last below few posts we learnt about JDBC basics
What is Java Database Connectivity (JDBC)?
Types of JDBC drivers in java?
JDBC MySQL database Connection example
JDBC database connection using property resource(property file)

In this post we will learn how to perform CRUD(Create,Read,Update and Delete) Operation using Jdbc Statement.
In this example I have used MySQL database but you are free to choose any database as per your choice.

Project structure in eclipse:

SQL Query to create employee_table in MySQL (jdbcdb schema)
(Note:Make sure you have created employee_table in jdbcdb database schema)

DBUtil.java class which is responsible to connect with MySQL database.

this util class uses getConnection() method of java.sql.DriverManager class
public static Connection getConnection(String url,String user, String password) throws SQLException

This method takes three parameters database URL,username and password.

Here database username =”root”,password  = “root”  what password had supplied during MYSQL database installation time and finally database URL =“jdbc:mysql://localhost:3306/jdbcdb” Where jdbc is the API, mysql is the database, localhost is the server name on which mysql  database server is running, we may also use IP address instead machine name, 3306 is the port number and jdbcdb(Make you you have created this schema in MySQL database) is the database name. You may use any database name, in that case, You need to replace the jdbcdb with your database name.

Now let’s run our ClientTest.java class to create an employee record in database.
(Note:this class has all the methods to perform CRUD operation you have to test every method one by one keeping rest of method calls commented in main method)

After running ClientTest.java program you will look below  output on eclipse console:

now if you check your database you will find below record in employee_table

Comment all methods calls in main method except readEmployeeById() and If you run ClientTest.java to read a row from database

You will get below output on eclipse console:

Now I leaving up to you to test how methods updateEmployeeEmailById()  and deleteEmployeeById() works in client program.

That’s all about CRUD operation using JDBC Statement.
If you have any feedback or suggestion please feel free to drop in blow comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *