12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
TUTORIALS FIELD
How to Retrieve Data From Database and Display it in JTable
Using Java Swing
by Mehtab Hameed
Hello Friends, Welcome to my new tutorial and in this tutorial, we will learn how to
retrieve data from database and display it in JTable using Java Swing with simple and
easy steps. A Java JTable is a class that is used to display data in the form of rows and
columns.
In my previous tutorial, we learned how to install MySQL Workbench on Windows,
Mac, and Ubuntu Operating System, and Now we will learn how to fetch data from
the database and populate it in JTable.
I have used Eclipse IDE in this tutorial for the program development, but You can use
any IDE like NetBeans or Intellij IDEA because the programming logic will be the
same.
For the database, I have used MySQL Workbench, which is nothing but a GUI
version of the MySQL console, where we can visually design, model, and generates
databases.
I have also given the program’s source code at the end of this tutorial from where
you can download it.
So let’s start our tutorial on how to retrieve data from database and display it in
JTable using Java Swing.
How to Retrieve Data From Database and Display it in JTable
Using Java Swing
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 1/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
First of all, I want to summarize what I am going to do in this tutorial. I am going to
create a database in the back-end with a table in it named student and In the front
end, I will set up the GUI, and when I will input the name of the student and click on
the “Fetch Data” button, the table should show up with the details of that particular
student.
How to Install MySQL WorkBench Full Tutorial
Creating Database in MySQL Workbench
open MySQL WorkBench and then go to Database>Connect to Database.
How to retrieve data from from database and display it in jtable using java swing – fig 1
A pop-up window will be opened just click on ok.
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 2/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
How to retrieve data from from database and display it in jtable using java swing – fig 2
Once you click on “ok” it will ask you for a password. It is the same password
which you have used during the installation process. So enter the correct
password and then click on the “ok” button.
How to retrieve data from from database and display it in jtable
using java swing – fig 3
Now create a database (studentDatabse in this example), and inside that
database, create a table (student in this example) with the following fields.
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 3/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
USERNAME
ROLLNO
DEPARTMENT
How to retrieve data from from database and display it in jtable using java swing – fig 4
1 create database studentDatabase;
2 use studentDatabase;
3 create table student(USERNAME varchar(30) not null, ROLLNO varchar(10),
4 DEPARTMENT varchar(30));
Now insert some values into it.
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 4/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
How to retrieve data from from database and display it in jtable using java swing – fig 5
1 insert into student values('Mehtab','1','Cse');
2 insert into student values('Jack','2','Phy');
Setting Up the GUI
The next thing you have to do is set up the GUI to add the components to it.
Go to Eclipse IDE and create a new Java Project.
For this, you click on the Java Project under the new section of File Menu
(File>>New>>Java Project).
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 5/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
Fetch data from database and display in JTable – fig 6
Now give a name to your project (FetchDataExample in this example) and
click on “Finish”.
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 6/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
Fetch data from database and display in JTable – fig 7
Now right click on the project and create a new Java class (New>>Class).
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 7/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
Fetch data from database and display in JTable – fig 8
Now give a name to your class(FetchData in this Example), tick mark on the
public static void main(String[] args), and then click on the Finish button as
shown in the figure below.
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 8/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
Fetch data from database and display in JTable – fig 9
Now Implement the ActionListener interface to our class so that we will be
able to do some button click event in our program. And if we are
implementing the ActionListener interface in our class, we have to override it’s
method actionPerformed() into our class.
1 import java.awt.event.ActionEvent;
2 import java.awt.event.ActionListener;
3
4 public class FetchData implements ActionListener {
5
6 public static void main(String[] args) {
7
8 }
9
10 @Override
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 9/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
11 public void actionPerformed(ActionEvent e) {
12 }
13
14 }
15
16
17
Now create the window using the JFrame class of swing, and it’s methods then
add components to it.
The components are,
one JLabel (username label)
one JTextField ( For entering the username)
Two JButtons ( one for fetching data and one for resetting the username
text field)
Below is the code, and I have also explained the code in the comments.
1 import java.awt.event.ActionEvent;
2 import java.awt.event.ActionListener;
3
4 import javax.swing.*;
5 import java.awt.*;
6
7 public class FetchData implements ActionListener {
8 JFrame frame1;//creating object of first JFrame
9 JLabel nameLabel;//creating object of JLabel
10 JTextField nameTextField;//creating object of JTextfield
11 JButton fetchButton; //creating object of JButton
12 JButton resetButton;//creating object of JButton
13
14
15 FetchData(){
16
17 frame1 = new JFrame();
18 frame1.setTitle("Search Database");//setting the title of first JF
19 frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//setting de
20 GridBagLayout bagLayout = new GridBagLayout();//creating object of
21 GridBagConstraints bagConstraints = new GridBagConstraints();//cre
22 frame1.setSize(500, 300);//setting the size of first JFrame
23 frame1.setLayout(bagLayout);//setting the layout to GridBagLayout
24
Now run your program
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 10/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
Fetch data from database and display in JTable – fig 10
As you can see, the window is successfully created, and components are
added to it.
Connecting to Database
In order to connect your Java program with MySQL database, you need to
include MySQL JDBC driver which is a JAR file, namely mysql-connector-
java-5.1.49-bin.jar. The version number in the Jar file can be different.
You can download the latest version of MySQL connector from this link
(MySQL Connector Java download) As shown in the figure below.
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 11/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
Fetch data from database and display in JTable – fig 11
Extract the zip archive and you will get the jar file.
Next, you have to include this jar file into your program so that you will be
able to connect your java program with MySQL database.
Right-click on the project, go to the properties section then select Java Build
Path and click on the Add External JARs button.
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 12/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
Fetch data from database and display in JTable – fig 12
After clicking on the button a pop-up window will appear where you have to
select and open the jar file.
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 13/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
Fetch data from database and display in JTable – fig 13
You can see the added Jar file as shown in the figure below. Now click on the
Apply and Close button.
Fetch data from database and display in JTable – fig 14
Now we want that when we enter the username and click on the Fetch data
Button, that particular student’s details should show up in a new window
inside the JTable. And if we Enter the wrong username and click on the Fetch
data button, then the message dialog box should show a specific message.
If we click on the Reset Button, then it should clear the username text field.
Setting up JTable and Retrieving Data From the Database to JTable
In this section, you will learn how to fetch data from database in Java and display in
table, which means we will search and display data from database in Java.
For this, you need to create objects of,
A JFrame
A JTable
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 14/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
A DefaultTableModel (this is used when no model is specifically defined)
A Connection object
A Statement object
Now when someone enters the username and clicks on the “Fetch Data”
button, the student’s details should show up on another JFrame inside the
JTable.
And If we enter the wrong username, then message dialog box should show
us the message “No such username found”.
And If we click on the Reset Button, then username text field should be
cleared.
The Final programming code is given below, and I have also explained the
code in the comments.
1 import javax.swing.*;
2 import javax.swing.table.DefaultTableModel;
3 import java.awt.*;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.sql.*;
7
8 public class FetchData implements ActionListener {
9
10 JFrame frame1;//creating object of first JFrame
11 JLabel nameLabel;//creating object of JLabel
12 JTextField nameTextField;//creating object of JTextfield
13 JButton fetchButton;//creating object of JButton
14 JButton resetButton;//creating object of JButton
15
16 JFrame frame2;//creating object of second JFrame
17 DefaultTableModel defaultTableModel;//creating object of DefaultTable
18 JTable table;//Creating object of JTable
19 Connection connection;//Creating object of Connection class
20 Statement statement;//Creating object of Statement class
21 int flag=0;
22
23
24 FetchData() {
Now run your program, Enter the username and click on the “Fetch Data”
button.
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 15/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
Fetch data from database and display in JTable – fig 15
As soon as you click on the “Fetch Data” button, the new JFrame will show up
with the table inside it and the details of the particular student as shown in the
figure below.
Fetch data from database and display in JTable – fig 16
Now again Run the program and click an invalid username then the message
dialog box will show up with the message ” No such username found”.
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 16/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
Fetch data from database and display in JTable – fig-17
In the same way, you can also use the Reset Button, and it will work perfectly
fine.
How to Retrieve Data From Database and Display it in JTable Using Java
Swing Source Code
Download Source Code
So this was all from this tutorial, Feel free to comment down below if you have any
query regarding this post. Thank You
and if you are willing to learn Java Programming using the books, then you can
download the Java Books for Beginners Free Pdf files from the Below Link.
Download Java Books Free PDF
People are also Reading…..
Best Java Books For Beginners with Free Pdf Download
Menu Driven Program in Java Using Switch Case
How to Create Calculator in Java Swing
How to Create Tic Tac Toe Game in Java
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 17/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
How to Create Login Form in Java Swing
Registration Form In Java with Database Connectivity
How to Create Splash Screen In Java
How to Create Mp3 Player in Java
Previous
Java Swing
1 thought on “How to Retrieve Data From Database and Display it in
JTable Using Java Swing”
ALI MOULA SHABANDRI
July 20, 2020 at 11:49 am
Dear Mehtab Hameed,
Assalamualaikum Rahmatullahi vabarakatahu!
Your tutorial to retrieve data from database and display in jtable is excellent.
Thank you very much. May Allah bless you with success in this world and in
the Aakhirat.
I am trying to make namaz timetable in java and MS Access. My Syntax select
* from table salat
where date = Date().i.e.Todays date is not working. Please help me to use
correct date() syntax.
My email is [email protected]. My No +91 98450 37286.
Jazakallahu Khair
Reply
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 18/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
Leave a Comment
b i link b-quote del ins img ul ol li
code more close tags crayon
Name
Email
Website
Save my name, email, and website in this browser for the next time I comment.
Post Comment
Java Swing
Java Environment Setup
JFrame
JLabel
JTextField
JButton
JButton Click Event
JPasswordField
JTable with Database
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 19/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
Registration Form
Splash Screen
Login Form
Text to Speech
Mp3 Player
MS Access Database Connection
Calculator Program
Java
Sentinel Value Java
MySQL Database Connection
Java Books Free PDF
Menu Driven Program in Java
Latest Posts
Object Oriented Programming In Java Questions And Answers PDF
Menu Driven Program In Java Using Switch Case | while | do-while
Tic Tac Toe Java Code Against Computer With Source Code
Home
Privacy Policy
About
Contact Us
Disclaimer
Java Swing
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 20/21
12/10/2021 07:38 How to Retrieve Data From Database and Display it in JTable Using Java Swing
Java
Android
MySQL
Sponsored Posts
2021 © Tutorials Field . All Rights Reserved. Sitemap
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 21/21