How to Retrieve Data From Database and Display it in JTable Using Java Swing

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

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
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.
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.
    • USERNAME
    • ROLLNO
    • DEPARTMENT
How to retrieve data from from database and display it in jtable using java swing – fig 4
  • Now insert some values into it.
How to retrieve data from from database and display it in jtable using java swing – fig 5
 

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).
How to retrieve data from 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”.
How to retrieve data from 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).
How to retrieve data from 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.
How to retrieve data from 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.
  • 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.
  • Now run your program
How to retrieve data from 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.
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.
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.
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
    • 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.
  • Now run your program, Enter the username and click on the “Fetch Data” button.
How to retrieve data from 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.
How to retrieve data from from database and display it in jtable using java swing
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”.
How to retrieve data from 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

So this was all from this tutorial, Feel free to comment down below if you have any query regarding this post. Thank You

People are also Reading…..

2 thoughts on “How to Retrieve Data From Database and Display it in JTable Using Java Swing”

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

Leave a Comment