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
TUTORIALS FIELD
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.
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.
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
How to retrieve data from from database and display it in jtable using java swing – fig 1
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.
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
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
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
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
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
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
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
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
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
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
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
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
You can see the added Jar file as shown in the figure below. Now click on the
Apply and Close button.
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.
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.
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
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
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.
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
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
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.
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
Previous
Java Swing
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.
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
Name
Website
Save my name, email, and website in this browser for the next time I comment.
Post Comment
Java Swing
JFrame
JLabel
JTextField
JButton
JPasswordField
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
Calculator Program
Java
Latest Posts
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
https://www.tutorialsfield.com/how-to-retrieve-data-from-database-and-display-it-in-jtable-using-java-swing/ 21/21