Home » Java programming language

How to display all records using JDBC in Java?

In this article, we are going to learn how to display all records of MYSQL table using JDBC through java program?
Submitted by Manu Jemini, on October 13, 2017


  1. How to create a table using JDBC in Java?
  2. How to insert records through JDBC in Java?

Note: To displaying data from MYSQL table, there should be at least one row of data must be available.

Now, we are going to establish a connection between MYSQL and JAVA using Connection class, for this we are creating an object named cn of this class.

Then, we will prepare a MySQL query statement to display records from table named employee, to execute this query statement, we will create an object of Statement class.

Then we create an object named smt of Statement class, that will be used to execute query by using executeQuery() method.

Database details:

  • Hostname: localhost
  • Port number: 3306
  • Username: root
  • Password: 123
  • Database name: demo
  • Table name: employees

Java program to display all records from a table using JDBC

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DisplayAll {
	public static void main(String[] args) {
			//serverhost = localhost, port=3306, username=root, password=123
			Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","123");
			Statement smt=cn.createStatement();
			//query to display all records from table employee
			String q="Select * from employees";
			//to execute query
			ResultSet rs=smt.executeQuery(q);
			//to print the resultset on console
				System.out.println("Record Not Found...");
		catch(Exception e){

Output (In console)

100, Aman, 10/10/1990, Delhi, 35000

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.