JTable Display Data From MySQL Database

This section will describe you the displaying of data of a database table into a JTable. Here you will read about how to create a table in Java swing, how can you add column header's name, how can you show data into the table.

JTable Display Data From MySQL Database

JTable Display Data From MySQL Database

In this section we will discuss about how to display data of a database table into javax.swing.JTable

This section will describe you the displaying of data of a database table into a JTable. Here you will read about how to create a table in Java swing, how can you add column header's name, how can you show data into the table.

How to create table in Java ?

To create a table in Java Swing we will use the JTable and many more APIs provided by Oracle. javax.swing.JTable class is used to create an instance of table in Java. javax.swing.table.DefaultTableModel provides the model for table. To get a better table visibility it is recommended that the table should be kept into JScrollPane.

How to add column header's name ?

However, you can set the column header's name in various ways but, the simplest way to set the header name is by defining an array. See the example, discussed later.

How can you add data into the table ?

As the column header's name you can set the data into table rows in various way but, the simplest way to set the data of table row is by defining an array. See the example, discussed later.


Here I am giving a simple example which will demonstrate you how to add table column header's name and how to add data into the rows of table from database table. In this example we will create a GUI for taking input through user, and then we will fetch the record according to the given input and then we will displayed the fetched record into the table. In this example the record will be fetched after giving input in the textbox and clicked on the search button. We will display the fetched data into a table which is added inside a frame. To create a table inside a frame we will create a table model using DefaultTableModel class and then we will add the column headers using setColumnIdentifiers() method and then we will set the model to default model using setModel() method of JTable class. Then we will add the table into JScrollPane. Then we will fetch the record from database table, and added these values using model.addRow(Object[]).

Database Table


CREATE TABLE `student` (                
           `rollno` varchar(15) NOT NULL,        
           `name` varchar(20) DEFAULT NULL,      
           `class` varchar(5) DEFAULT NULL,      
           `section` varchar(5) DEFAULT NULL,    
           PRIMARY KEY (`rollno`)                
         ) ENGINE=InnoDB DEFAULT CHARSET=latin1  

Source Code


package net.roseindia.jtableExample;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.*;
import java.sql.*;
import java.awt.event.*;

public class SearchResult implements ActionListener{
JFrame frame, frame1;
JTextField textbox;
JLabel label;
JButton button;
JPanel panel;
static JTable table;

String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/record";
String userName = "root";
String password = "root";
String[] columnNames = {"Roll No", "Name", "Class", "Section"};

public void createUI()
frame = new JFrame("Database Search Result");
textbox = new JTextField();
label = new JLabel("Enter your roll no");
label.setBounds(10, 30, 100, 20);
button = new JButton("search");

frame.setSize(500, 400); 

public void actionPerformed(ActionEvent ae)
button = (JButton)ae.getSource();
System.out.println("Showing Table Data.......");

public void showTableData()

frame1 = new JFrame("Database Search Result");
frame1.setLayout(new BorderLayout()); 
//TableModel tm = new TableModel();
DefaultTableModel model = new DefaultTableModel();
//DefaultTableModel model = new DefaultTableModel(tm.getData1(), tm.getColumnNames()); 
//table = new JTable(model);
table = new JTable();
JScrollPane scroll = new JScrollPane(table);
String textvalue = textbox.getText();
String roll= "";
String name= "";
String cl = "";
String sec = "";
Connection con = DriverManager.getConnection(url, userName, password);
String sql = "select * from student where rollno = "+textvalue;
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
int i =0;
roll = rs.getString("rollno");
name = rs.getString("name");
cl = rs.getString("class");
sec = rs.getString("section"); 
model.addRow(new Object[]{roll, name, cl, sec});
if(i <1)
JOptionPane.showMessageDialog(null, "No Record Found","Error",
if(i ==1)
System.out.println(i+" Record Found");
System.out.println(i+" Records Found");
catch(Exception ex)
JOptionPane.showMessageDialog(null, ex.getMessage(),"Error",

public static void main(String args[])
SearchResult sr = new SearchResult();

Output :

Data in the database table is as follows :

When you will execute the SearchResult.java class you will get the output as follows :

1. At first a GUI window will be opened for taking input to fetch the corresponding record into the database table as follows :

2. When you will input the value into the textbox (say, give the value 125 into the textbox) and click on search button then the output will be as follows :

Download Source Code


  1. Display Image in Java
  2. Show Coordinates
  3. What is Java Swing?
  4. Creating a Frame
  5. Setting an Icon for a Frame in Java
  6. Show Dialog Box in Java
  7. Show message and confirm dialog box
  8. Show input dialog box
  9. Changing the Label of a JButton Component in Java
  10. Setting Multi-Line label on the Button
  11. Setting icon on the button in Java
  12. Making a Frame Non Resizable in Java
  13. Remove Minimize and Maximize Button of Frame in Java
  14. Removing the Title Bar of a Frame
  15. Setting Bounds for a maximized frame
  16. Iconifying and Maximizing a frame in Java
  17. Making a component drag gable in java
  18. Create a ToolBar in Java
  19. Animating Images in Java Application
  20. Drawing with Color in Java
  21. Drawing with Gradient Color in Java
  22. Adding a Rollover and Pressed Icon to a JButton Component in Java
  23. Creating Check Box in Java Swing
  24. Customize the Icon in a JCheckBox Component of Java Swing
  25. Create a JComboBox Component in Java
  26. Combo Box operation in Java Swing
  27. Create a JRadioButton Component in Java
  28. Selecting a Radio Button component in Java
  29. Create a JList Component in Java
  30. Setting Tool Tip Text for items in a JList Component
  31. Setting the Dimensions of an Item in a JList Component in Java
  32. Create a JSpinner Component in Java
  33. Show Time in JSpinner Component
  34. Disabling Keyboard Editing in a JSpinner Component
  35. Limiting the Values in a Number JSpinner Component
  36. JSlider Component of Java Swing
  37. Progress Bar in Java Swing
  38. Create menus and submenus in Java
  39. Crate a Popup Menu in Java
  40. Create a Popup Menus with Nested Menus in Java