title: Generic Table Model
body: <p>I wanted to create a flexible table model where it's list variable type may differ upon it's instantiation. I extends AbstractTableModel class, here is the code :</p>
  
  <pre><code>package models;
  
  import java.util.List;
  import java.util.ArrayList;
  import java.util.Collection;
  import javax.swing.table.AbstractTableModel;
  import java.sql.ResultSet;
  import java.sql.ResultSetMetaData;
  import java.sql.Connection;
  import java.sql.Statement;
  import java.sql.DriverManager;
  import java.sql.SQLException;
  
  public class GenericTableModel &lt; T &gt; extends AbstractTableModel{
      private static final long serialVersionUID = 1L;
      private List&lt; T &gt; list = new ArrayList&lt; T &gt;();
      private int columns;
  
  GenericTableModel(int columns){
      this.columns = columns;
  }
  
  public void updateAll(Collection&lt;T&gt; collection){
      list.clear();
      list.addAll(collection);
      fireTableDataChanged();
  }
  
  public T getCell(int index){
      return list.get(index);
  }
  
  public void setData(List list){
      this.list = list;
      fireTableDataChanged();     
  }
  
  public void insert(T data){
      list.add(data);
      fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
  }
  
  public void delete(int index){
      list.remove(index);
      fireTableRowsDeleted(index, index);
  }
  
  public void update(int index, T data){
      list.set(index, data);
      fireTableRowsUpdated(index, index);
  }
  
  public T select(int index){
      return list.get(index);
  }
  
  @Override
  public int getRowCount() {
      return list.size();
  }
  
  @Override
  public int getColumnCount() {
      return columns;
  }
  
  @Override
  public Object getValueAt(int rowIndex, int columnIndex) {
      String databaseURL = "jdbc:mysql://localhost/perkuliahan";
      Connection connection = null;
      Statement statement = null;
      ResultSet resultSet = null;
      try{
          connection = DriverManager.getConnection(databaseURL, "root", "");
          statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
          T className = null;
          resultSet = statement.executeQuery("select * from " + className.getClass().getName());
          while(resultSet.getRow()!=rowIndex){
              resultSet.next();
          }
          return resultSet.getObject(columnIndex);
      }
      catch(SQLException sqlException){
          return sqlException.getMessage();
      }
      finally{
          try{
              resultSet.close();
              statement.close();
              connection.close();
          }
          catch(SQLException sqlException){
              return sqlException.getMessage();
          }
      }
  }
  
  @Override
  public String getColumnName(int column){
      String databaseURL = "jdbc:mysql://localhost/perkuliahan";
      Connection connection = null;
      Statement statement = null;
      ResultSet resultSet = null;
      try{
          connection = DriverManager.getConnection(databaseURL, "root", "");
          statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
          T className = null;
          resultSet = statement.executeQuery("select * from " + className.getClass().getName());
          ResultSetMetaData resultSetMetaData;
          resultSetMetaData = resultSet.getMetaData();
          return resultSetMetaData.getColumnName(column);
      }
      catch(SQLException sqlException){
          return sqlException.getMessage();
      }
      finally{
          try{
              resultSet.close();
              statement.close();
              connection.close();
          }
          catch(SQLException sqlException){
              return sqlException.getMessage();
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>But, once I declared it, it shows error message :</p>
  
  <pre><code>package forms;
  
  import models.GenericTableModel;
  import pojo.Mahasiswa;
  
  public class MahasiswaForm extends javax.swing.JFrame {
  
  /**
   * Creates new form MahasiswaForm
   */
  public MahasiswaForm() {
      initComponents();
      GenericTableModel&lt;Mahasiswa&gt; tableModel = new GenericTableModel&lt;Mahasiswa&gt;();
  }
  </code></pre>
  
  <p>Can anybody tell me what is wrong ? I need this for school assignment, thank you.</p>