Display Records From Database Using JTable in Java

Introduction

This article explains how to display fetched information using JTable in Java. The NetBeans IDE is used for creating this app.

What is JTable

It is just like a simple table. It displays records in a tabular format. It is a component of Swing and is used for GUI programming based on a matrix format.

Display Emp Records from Database in JTable

For creating this app we need the following files:

  1. Java file (DisplayEmpData.java)
  2. SQL table (emp.sql)

1. DisplayEmpData.java

This Java file consists of all the logic. First of all we initialize JFrame components using a constructor then create a database connection and finally set the database value to the textfield. If the given name is not found in the database then it displays an error message and displays it by running the constructor.

2. emp.sql

For fetching records we need a database table; for that we create an "emp" table in our database.

Syntax

emp.sql

create table emp
(
    uname varchar2(20), umail varchar2(30),
    upass varchar2(20), ucountry varchar2(20)
);

Insert some rows in it

insert into emp values ('sandeep', '[email protected]', 'welcome', 'India');
insert into emp values ('rahul', '[email protected]' , '123', 'India');

Now let's start creating this app. Use the following procedure to do that in the NetBeans IDE.

For creating this app in the NetBeans IDE, we need to use the following procedure.

Step 1

Open the NetBeans IDE.

NetBeans IDE

Step 2

Choose "Java" -> "Java Application" as in the following.

Java Application

Step 3

Now type your project name as "JTableApp" as in the following.

Project Name

Step 4

Now create a new Java Class with the name "DisplayEmpData" and provide the following code for it

DisplayEmpData.java

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class DisplayEmpData extends JFrame implements ActionListener {
    JFrame frame1;
    JLabel l0, l1, l2;
    JComboBox c1;
    JButton b1;
    Connection con;
    ResultSet rs, rs1;
    Statement st, st1;
    PreparedStatement pst;
    String ids;
    static JTable table;
    String[] columnNames = {"User name", "Email", "Password", "Country"};
    String from;
    DisplayEmpData() {
        l0 = new JLabel("Fatching Employee Information");
        l0.setForeground(Color.red);
        l0.setFont(new Font("Serif", Font.BOLD, 20));
        l1 = new JLabel("Select name");
        b1 = new JButton("submit");
        l0.setBounds(100, 50, 350, 40);
        l1.setBounds(75, 110, 75, 20);
        b1.setBounds(150, 150, 150, 20);
        b1.addActionListener(this);
        setTitle("Fetching Student Info From DataBase");
        setLayout(null);
        setVisible(true);
        setSize(500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        add(l0);
        add(l1);;
        add(b1);
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521:xe", "sandeep", "welcome");
            st = con.createStatement();
            rs = st.executeQuery("select uname from emp");
            Vector v = new Vector();
            while (rs.next()) {
                ids = rs.getString(1);
                v.add(ids);
            }
            c1 = new JComboBox(v);
            c1.setBounds(150, 110, 150, 20);
            add(c1);
            st.close();
            rs.close();
        } catch (Exception e) {
        }
    }
    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == b1) {
            showTableData();
        }
    }
    public void showTableData() {
        frame1 = new JFrame("Database Search Result");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setLayout(new BorderLayout());
        //TableModel tm = new TableModel();
        DefaultTableModel model = new DefaultTableModel();
        model.setColumnIdentifiers(columnNames);
        //DefaultTableModel model = new DefaultTableModel(tm.getData1(), tm.getColumnNames());
        //table = new JTable(model);
        table = new JTable();
        table.setModel(model);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table.setFillsViewportHeight(true);
        JScrollPane scroll = new JScrollPane(table);
        scroll.setHorizontalScrollBarPolicy(
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        from = (String) c1.getSelectedItem();
        //String textvalue = textbox.getText();
        String uname = "";
        String email = "";
        String pass = "";
        String cou = "";
        try {
            pst = con.prepareStatement("select * from emp where UNAME='" + from + "'");
            ResultSet rs = pst.executeQuery();
            int i = 0;
            if (rs.next()) {
                uname = rs.getString("uname");
                email = rs.getString("umail");
                pass = rs.getString("upass");
                cou = rs.getString("ucountry");
                model.addRow(new Object[]{uname, email, pass, cou});
                i++;
            }
            if (i < 1) {
                JOptionPane.showMessageDialog(null, "No Record Found", "Error", JOptionPane.ERROR_MESSAGE);
            }
            if (i == 1) {
                System.out.println(i + " Record Found");
            } else {
                System.out.println(i + " Records Found");
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        frame1.add(scroll);
        frame1.setVisible(true);
        frame1.setSize(400, 300);
    }
    public static void main(String args[]) {
        new DisplayEmpData();
    }
}

Step 5

Now our project is ready to run.

Right-click on the project menu then choose "Run". The following output will be generated.

output

Step 6

Now choose any name from the ComboBox and click on the "Submit" button as in the following.

Select sandeep

sandeep records

Step 7

Now choose another name as in the following.

Select Rahul

Rahul Records


Similar Articles