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.
*Java Courses : Java Complete Course
Java Swing Course
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");
setLocationRelativeTo(null);
setSize(600,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
// 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");
while(rs.next()){
u = new Users(
rs.getInt("id"),
rs.getString("fname"),
rs.getString("lname"),
rs.getInt("age")
);
users.add(u);
}
} 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";
model.setColumnIdentifiers(columnsName);
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();
model.addRow(rowData);
}
table.setModel(model);
// System.out.println(getUsers().size());
Work window = new Work();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JScrollPane pane = new JScrollPane(table);
panel.add(pane,BorderLayout.CENTER);
window.setContentPane(panel);
}
}
///////////////OUTPUT:
JTable Populated |
You can also Fill JTable From Mysql DataBas Using Vector
Bagikan
JAVA & MySQL - How To Bind JTable From MySQL DataBase Using ArrayList In Java NetBeans
4/
5
Oleh
insurance