Reputation: 23
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();
}
}
Upvotes: 1
Views: 1216
Reputation: 20741
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
Upvotes: 2