1

How to display MySQL data in JTable without using vector?

I'm doing Java Swing application and I want to show reports from database. I'm using JTable to display data from database. I have tried following code but it is not displaying data from database. My database connection is also correct.

int i;
static Vector<Vector<String>> data = new Vector<Vector<String>>();

String[] columnNames = {"Registration id", "First Name", "Middle Name", "Last Name", "Address", "Dob", "Age", "Date Of Registration", "Register Rfid No."};
public AdmissionReport()
{
    initComponents();

    //this is the model which contain actual body of JTable
    DefaultTableModel model = new DefaultTableModel();
    model.setColumnIdentifiers(columnNames);
    //jTable1=new JTable(model);
    jTable1 = new JTable();
    jTable1.setModel(model);
    int i1 = jTable1.getRowCount();
    if (i1 > 1) {
        model.removeRow(0);
        i1 = i1 - 1;
    }

    String str = "select rid,fname,mname,lname,address,dob,age,dor,rfidtagdata from schoolrfid.registration";
    Connection cn;
    ResultSet rs;
    Statement st;

    String rid, fname, mname, lname, add, dob, age, dor, rfidtag;

    try {
        // Change the database name, hosty name,
        // port and password as per MySQL installed in your PC.
        cn = DriverManager.getConnection("jdbc:mysql://" + "localhost:3306/schoolrfid", "root", "root");
        st = cn.createStatement();

        rs = st.executeQuery(str);
        System.out.println("connected to database.. ");
        // int i=0;
        while (rs.next()) {

            //Vector <String> d=new Vector<String>();
            rid = (rs.getString("rid"));
            fname = (rs.getString("fname"));
            mname = (rs.getString("mname"));
            lname = (rs.getString("lname"));
            add = (rs.getString("address"));
            dob = (rs.getString("dob"));
            age = (rs.getString("age"));
            dor = (rs.getString("dor"));
            rfidtag = (rs.getString("rfidtagdata"));

            i = 0;
            String[] data = {rid, fname, mname, lname, add, dob, age, dor, rfidtag};

            model.addRow(data);
            i++;

        }
        if (i < 1) {
            JOptionPane.showMessageDialog(null, "No Record Found", "Error", JOptionPane.ERROR_MESSAGE);
        }
        if (i == 1) {
            System.out.println(i + " Record Found");
        } else {
            System.out.println(i + " Records Found");
        }

    } catch (SQLException e) {

        e.printStackTrace();
    }

}
Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
user3079393
  • 23
  • 1
  • 5
  • the similar question: http://stackoverflow.com/questions/23485987/how-to-add-items-to-a-jtable-using-a-loop – Xing Fei May 06 '14 at 09:21
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). Hard code some data to replace the DB. 2) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 3) A single blank line of white space in source code is *always* enough. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson May 06 '14 at 09:24
  • @user3079393 dont forget to mark it as answer and cast your upvote if it satisfies your requirement – Nidhish Krishnan May 06 '14 at 10:33

1 Answers1

2

The TableModel behind the JTable handles all of the data behind the table. In order to add and remove rows from a table, you need to use a DefaultTableModel

To create the table with this model:

JTable table = new JTable(new DefaultTableModel(new Object[]{"Column1", "Column2"}));

To add a row:

DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new Object[]{"Column 1", "Column 2", "Column 3"});

You can also remove rows by removeRow(int row) which will removes the row at row from the model.

Full details on the DefaultTableModel can be found here


Similarly from the MySQL you can add rows like as show below

DefaultTableModel memberTable = (DefaultTableModel) table.getModel();
while(rs.next())
{
    memberTable.addRow(new Object[]{rs.getString('rid'),rs.getString('fname'),rs.getString('lname'),rs.getString('address'),.....});
}

Also Take a look at this answer

https://stackoverflow.com/a/17655017/1575570

Community
  • 1
  • 1
Nidhish Krishnan
  • 20,593
  • 6
  • 63
  • 76