JAVA & MySQL - How To Bind JTable From MySQL DataBase Using ArrayList In Java NetBeans

Populating JTable From MySQL DataBase Using ArrayList In Java NetBeans

In this java Tutorial we will see How To Fill data Into JTable from Mysql  database
using ArrayList In Java NetBeans .

maybe you need to see:
   - connect java to mysql.
   - using ArrayList.

Project Source Code:

package javaapp;

import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

// create a users class
class Users{
    private int Id;
    private String Fname;
    private String Lname;
    private int Age;
    public Users(int id,String fname,String lname,int age){
        this.Id = id;
        this.Fname = fname;
        this.Lname = lname;
        this.Age = age;
    public int getId(){
        return this.Id;
    public String getFname(){
        return this.Fname;
    public String getLname(){
        return this.Lname;
    public int getAge(){
        return this.Age;

public class Work extends JFrame {
    public Work(){
        super("Bind JTable From MySQL DataBase");

 // create a Function to get the connection
    static Connection getConnection(){
        Connection con = null;
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost/test_db","root","");
        } catch (SQLException ex) {
            Logger.getLogger(Work.class.getName()).log(Level.SEVERE, null, ex);
        return con;
 // create a function to fill the an arraylist from database
    static ArrayList<Users> getUsers(){
        ArrayList<Users> users = new ArrayList<Users>();
        Connection con = getConnection();
        Statement st;
        ResultSet rs;
        Users u;
        try {
            st = con.createStatement();
            rs = st.executeQuery("SELECT * FROM users");
                u = new Users(
        } catch (SQLException ex) {
            Logger.getLogger(Work.class.getName()).log(Level.SEVERE, null, ex);

        return users;
    public static void main(String[] args){

   now we are gonna create and populate a jtable from the arraylist who is populated from mysql database
        JTable table = new JTable();
        DefaultTableModel model = new DefaultTableModel();
        Object[] columnsName = new Object[4];
        columnsName[0] = "Id";
        columnsName[1] = "Fname";
        columnsName[2] = "Lname";
        columnsName[3] = "Age";
        Object[] rowData = new Object[4];
        for(int i = 0; i < getUsers().size(); i++){
            rowData[0] = getUsers().get(i).getId();
             rowData[1] = getUsers().get(i).getFname();
              rowData[2] = getUsers().get(i).getLname();
               rowData[3] = getUsers().get(i).getAge();
//        System.out.println(getUsers().size());
        Work window = new Work();
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        JScrollPane pane = new JScrollPane(table);


Populating JTable From MySQL DataBase Using ArrayList In Java
JTable Populated
You can also See -> Fill JTable From Mysql DataBas Using Vector

Share this

Related Posts

Next Post »