1

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.

Sastrija
  • 3,284
  • 6
  • 47
  • 64
Dhinakar
  • 4,061
  • 6
  • 36
  • 68
  • 1
    Fetch only those rows which are not empty. Read this tutorial - http://docs.oracle.com/javase/tutorial/uiswing/components/table.html – KV Prajapati Dec 14 '11 at 08:16
  • 1
    Don't create a custom TableModel. Just use the DefaultTableModel. It allows you to dynamically add rows so you don't need to hard code the size of your table. – camickr Dec 14 '11 at 16:03

2 Answers2

2

The documentation for JTable gives the following example for how to use a table with a custom table model.

TableModel dataModel = new AbstractTableModel() {
    public int getColumnCount() { return 10; }
    public int getRowCount() { return 10;}
    public Object getValueAt(int row, int col) { return new Integer(row*col); }
};
JTable table = new JTable(dataModel);
JScrollPane scrollpane = new JScrollPane(table);

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

Select COUNT(*) from Employee_Master where status = 'Active'

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 JTable your 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.

Community
  • 1
  • 1
Vlad
  • 18,195
  • 4
  • 41
  • 71
  • I have added below coding on AbstractTableModel. But application does not responding .. Method count() returns number of rows in mysql table. `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; }` – Dhinakar Dec 14 '11 at 08:41
  • 1
    Is it slow or does it halt, specifically? If it's slow, see my edit above. – Vlad Dec 14 '11 at 09:14
  • Thanks a lot.. I have solved this issue with help of your answer. Created two instance variable row as int, flag as Boolean. When I invoke first time **getRowCount()** method set **flag = false** and initialize **row** using **getRowCountFromDB()** method which gives number of rows present in database. After that initialize flag = false. After that instantly i used row. So my application runs faster than previous modification. – Dhinakar Dec 14 '11 at 09:42
2

as mentioned by another posters better would be read JTable tutorial before, since little bit crypted in the text, your method is JTable#setValueAt(Object aValue, int row, int column),

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    that same `myModel#setValueAt(Object aValue, int row, int column)`. but my questions, 1) why there are new Object[500][500], are you meaning that as Limit, then in all cases you can see only first 500 rows from database, you have to look for Paginations, in MySql or Oracle this is simple job, because these funcionalities are implemented in DbEngine by default, then you can't restrict that in the JTable or its model and redirectiong this job to SqlEngine, 2) why not use DefaultTableModel, 3) are you thinking about separating GUI and data to the some task – mKorbel Dec 14 '11 at 09:22
  • I don't know how to initialize JTable row and columns. That's y i declared object as 500. Now i rectified this problem and re-sized row count.. Thanks.. – Dhinakar Dec 14 '11 at 09:48
  • 1
    search for ResultSetTableModel, TableFromDatabase, but JTable tutorial mentioned how to create that – mKorbel Dec 14 '11 at 10:00