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


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.

  1. import java.sql.Connection;
  2. import java.sql.PreparedStatement;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import javax.swing.JOptionPane;
  6. import javax.swing.table.TableColumnModel;
  7. import net.proteanit.sql.DbUtils;

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

  1. Connection conn=null;
  2. ResultSet rs = null;

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

  1. try{
  2. String sql="SELECT * FROM userinfo";
  3. pst=conn.prepareStatement(sql);
  4. rs = pst.executeQuery(sql);
  5. jTable1.setModel(DbUtils.resultSetToTableModel(rs));
  6. }catch(SQLException e) {
  7. JOptionPane.showMessageDialog(this, e.getMessage());
  8. }

6. Call the method into your form load event.

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

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:

Facebook Comments


Please enter your comment!
Please enter your name here