Often in Java Swing programming, we face a requirement of loading data from the database into our JTable . Implementing This may be difficult for a novice java programmer and easy for an experienced programmer as it involves writing code for advanced swing component like JTable and as well as the code to connect and load data from the Database via JDBC . Hence you’ll need to know basics of JTable implementation as well as the JDBC part to understand the below implementation and description .
Java code to load Database Table Data into our JTable ( JTable JDBC demo ):-

JTable JDBC example to load database Table data into JTable:-

package jtables;
/**
* JTable JDBC program to load database table into the JTable
* @author Pramod . Choudhari || http://techwayz.com
*/
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
class BuildFrame {
private JFrame jf;
private JScrollPane js;
private JTable jt;
private DefaultTableModel mod;
private String link = “jdbc:mysql://127.0.0.1:3306/testDB”;
private String uname = “root”;
private String passwd = “”;
public BuildFrame() {
System.out.println(“Constructor called”);
jf = new JFrame(“JTable Example #2″);
js = new JScrollPane();
jt = new JTable();
mod = new DefaultTableModel() {
@Override
public int getColumnCount() {
return 4;
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return “No”;
case 1:
return “Name”;
case 2:
return “Age”;
case 3:
return “Address”;
default:
return “Unknown”;
}
}
};
jt.setModel(mod);
jf.setSize(400, 300);
jf.setResizable(false);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setLocation(200, 300);
jf.add(js);
js.setViewportView(jt);
loadTableData();
jf.setVisible(true);
}
private void loadTableData() {
try {
Class.forName(“com.mysql.jdbc.Driver”);
Connection con;
Statement st;
ResultSet rs = null;
con = DriverManager.getConnection(link, uname, passwd);
st = con.createStatement();
rs = st.executeQuery(“select * from student”);
while (rs.next()) {
mod.addRow(new Object[]{rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getString(4)});
}
rs.close();
st.close();
con.close();
} catch (Exception x) {
x.printStackTrace();
}
}
}
public class JTables {
public static void main(String[] args) {
// TODO code application logic here
BuildFrame bf = new BuildFrame();
}
}
Codes to create MYSQL Table Named Student (Student Table resides in our testDB database) / Set Primary Key / Insert Test Data in our Student Table :-
CREATE TABLE `testdb`.`student` (
`no` INT NOT NULL ,
`name` VARCHAR( 100 ) NOT NULL ,
`age` INT NOT NULL ,
`address` VARCHAR( 300 ) NOT NULL
) ENGINE = MYISAM ;
ALTER TABLE `student` ADD PRIMARY KEY(`no`)
INSERT INTO `testdb`.`student` (`no`, `name`, `age`, `address`) VALUES (’1′, ‘Pramod’, ’24′, ‘ABC XYZ, India’), (’2′, ‘DD ‘, ’33′, ‘PJU DSJKSA , India’);
INSERT INTO `testdb`.`student` (`no`, `name`, `age`, `address`) VALUES (’3′, ‘Ram’, ’18′, ‘Heaven’), (’4′, ‘Raj’, ’23′, ‘Sp Wadi , India’);
Screenshot (After running our Java Code) :-
Description of the JTable JDBC program :-
  • The constructor BuildFrame() would initialize all the swing components of our application and display them .
  • Instead of passing the row and column data directly to the JTable constructor , I’ve made use of DefaultTableModel to create columns and rows for my JTable . I recommend you guys to use this method (Building rows and Columns of JTable through DefaultTableModel) )] .

 

private void loadTableData() {
try {
Class.forName(“com.mysql.jdbc.Driver”);
Connection con;
Statement st;
ResultSet rs = null;
con = DriverManager.getConnection(link, uname, passwd);
st = con.createStatement();
rs = st.executeQuery(“select * from student”);
while (rs.next()) {
mod.addRow(new Object[]{rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getString(4)});
}
rs.close();
st.close();
con.close();
} catch (Exception x) {
x.printStackTrace();
}
}
The above method is responsible to load our Database Table Data into our JTable . The above method would file a SQL query to select each and every row from the student table and would store the data into our ResultSet . Having our Database Table data into our ResultSet , we’ll need to iterate over the ResultSet data row by row and at the Same time we’ll have to add the iterated rows to our JTable as shown below . while (rs.next())
{
mod.addRow(new Object[]{rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getString(4)});
}

Done ! Kindly run our JTable JDBC example and share your thoughts on it .

the author

I am a post graduate in computer sciences .I have 1+ year experience of working as a Java/J2EE developer in a top notch IT company. I love developing applications and writing technology related articles .