Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Display Data from Database using JTable in Java?

JTable Example

Condition for Displaying Data in JTable

  • Description: Displaying data from a database using JTable in Java Swing involves retrieving the data through JDBC and populating the table dynamically. Establish a connection to the database using DriverManager and create a Statement or PreparedStatement to execute the SQL query. The query results are stored in a ResultSet, which holds the data to display. To transfer the data into the JTable, use a DefaultTableModel, iterating through the ResultSet to add rows to the model. Column headers are extracted from the ResultSetMetaData and set in the table model. Finally, the JTable is created with the populated model and added to a JScrollPane to support scrolling. This approach ensures dynamic updating of the JTable based on the data retrieved from the database.
Sample Source Code
  • # DataTable.java
    package JavaSamples;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import javax.swing.JOptionPane;
    import javax.swing.table.DefaultTableModel;

    public class DataTable extends javax.swing.JFrame {
    private final String jdbcUrl = "jdbc:mysql://localhost:3306/Data";
    private final String username = "root";
    private final String password = "root@123";
    Connection con = null;
    PreparedStatement pst = null;
    Statement st = null;
    ResultSet rs = null;

    public DataTable() {
    initComponents();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    try {
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection(jdbcUrl, username, password);
    st = con.createStatement();
    String display = "SELECT UserId, UserName FROM data";
    rs = st.executeQuery(display);
    DefaultTableModel model = new DefaultTableModel(new String[]{"UserId", "UserName"}, 0);
    while (rs.next()) {
    int user_id = rs.getInt("UserId");
    String username = rs.getString("UserName");
    model.addRow(new Object[]{user_id, username});
    }
    jTable1.setModel(model);
    JOptionPane.showMessageDialog(null, "displayed successfully");
    } catch (SQLException e) {
    System.out.println(e.getMessage());
    } catch (ClassNotFoundException e) {
    System.err.println("MySQL JDBC Driver not found.");
    } finally {
    try {
    if (pst != null) {
    pst.close();
    } else if (con != null) {
    con.close();
    }
    } catch (SQLException e) {
    System.out.println(e.getMessage());
    }
    }
    }
    }
Screenshots
  • Screenshot 1: The user makes a database-side table and uses Java to display the details.
  • Database Table Screenshot

  • Screenshot 2: When the 'Display Data' button is clicked, the data retrieved from the database is displayed in a JTable.
  • JTable Display Screenshot