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. 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

DAO Interface and It’s implementation

Client class

That’s all about CRUD operation using JDBC PreparedStatement.
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 *