I want to bind mysql table content with JTable. I have connected sucessfully. I set table row and width as 500. It will shows 500 rows including data and empty. Now I want to view only data. I don’t want to view empty rows.
Please help me
My application get slow after insert a method getRowCountFromDB()..
class AbstractTableEmployee extends AbstractTableModel {
int row2;
private String[] columnNames = { "ID", "Name", "Company", "Department", "Location", "Mobile NO" , "Shift" };
private Object[][] data = new Object[500][500];
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
int row = 0;
try {
row = this.count();
} catch (SQLException ex) {
Logger.getLogger(AbstractTableEmployee.class.getName()).log(Level.SEVERE, null, ex);
}
return row;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
private int getRowCountFromDB() throws SQLException {
Connection con = (Connection) DBConnection.getDBConnection();
int row = 0;
Statement st = null;
ResultSet rs = null;
String Sql = "Select * from Employee_Master where status = 'Active'";
try {
st = (Statement) con.createStatement();
rs = st.executeQuery(Sql);
while (rs.next())
{
row++;
}
}
finally {
con.close();
rs.close();
st.close();
}
return row;
}
private int count() throws SQLException {
return this.getRowCountFromDB();
}
}
Thanks in advance.
The documentation for
JTablegives the following example for how to use a table with a custom table model.This is the minimal implementation that you’d have to provide.
Edit:
See 3.3.4.8 Counting Rows in the MySQL Reference for information on how to get a row count efficiently. It seems to me that you’re fetching an entire table, when all you want is a number.
You’d want something along the lines of
and then just reading that value.
As for your
data, I see no reason for it to have 500 columns, given that your table seems to have only 7.Anyway, depending on the size of your table, you might not want to have all the data in there at once. If that’s the case, you can always fetch and cache chunks of rows, according to where in the
JTableyour user is scrolling. You could use LIMIT for that.For some other ideas and tips, you should also take a look at this question which covers a similar problem.