How to Load Data From MySQL Database to Table Element Using Java

0
850

This tutorial will teach you on How to Load Data From MySQL Database to Table Element Using Java. It requires you to download and add rs2xml.jar library to complete this tutorial. This program will use jTable element from tool pallets of your Netbeans IDE.

How to load data from MySQL Database to Table Element in Java Steps

  1. Add/Insert a new jFrame Form

2. Design your form just look like the image below.

3. Insert the following codes above your class to access your required libraries.

[java]import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import javax.swing.table.TableColumnModel;
import net.proteanit.sql.DbUtils;[/java]

4. Insert the following codes inside your class but outside the method.

[java]Connection conn=null;
ResultSet rs = null;
PreparedStatement pst=null;[/java]

5. Create a method and insert the following codes below.

[java]try{
String sql=”SELECT * FROM userinfo”;
pst=conn.prepareStatement(sql);
rs = pst.executeQuery(sql);
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
}catch(SQLException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}[/java]

6. Call the method into your form load event.

7. Run your program and the output should look like the image below.

Advertisement

After completing this tutorial, you will understand on how you can create a program that loads the data from your MySQL Database through your jTable in Java. Follow and visit my other tutorials to learn and understand more about Java Programming.

Related Articles You May Like:

Looking for more source code? Type your keyword here here!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.