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

By | July 16, 2018

In previous couple of posts CRUD(Create,Read,Update and Delete) Operation using JDBC Statement
and Reading data from database using JDBC Statement we have learnt how to work with JDBC Statement.
When You want to execute literal or hard coded SQL then we do go for JDBC Statement.
But most of scenarios/times in application you would like to deal with dynamic SQL Query with place holder(?) then we will have to use JDBC PreparedStatement.

In this post we will learn how to perform CRUD operation using JDBC PreparedStatement.

Project structure in eclipse:

SQL Query to create employee_table in MySQL (jdbcdb schema)
If you run below SQL Script it will create employee_table with single record/row.

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.

Model class Employee.java

DAO Interface and It’s implementation

Client class ClientTest.java

That’s all about CRUD operation using JDBC PreparedStatement.

You May Also Like:

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)
CRUD(Create,Read,Update and Delete) Operation using JDBC Statement
Reading data from database using JDBC Statement

If you have any feedback or suggestion please feel free to drop in below comment box.

One thought on “CRUD(Create,Read,Update and Delete) Operation using JDBC PreparedStatement

  1. Komal

    how to validate in update that if id exists then only ask for name, dept etc
    if id does not exist then say no such id

    Reply

Leave a Reply

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