Reading data from database using JDBC Statement

By | July 15, 2018

In previous post CRUD(Create,Read,Update and Delete) Operation using JDBC Statement we learnt how to perform insert,read,update and delete operation using JDBC statement.In this post is about Reading data from database using JDBC Statement

Project structure in eclipse:

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

Suppose we have  records in our database as below:

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 read an employee record or all employee records from database.

If you run method getEmployeeById() to get an employee record from database you will  get below output on eclipse console:

Again If you run method getAllEmployeesInfo() to get all employees record from database you will  get below output on eclipse console:

That’s all about reading data from database 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 *