我想创建一个灵活的表模型,它的列表变量类型可能会因其实例化而有所不同。我扩展了AbstractTableModel类,这里是代码:
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 < T > extends AbstractTableModel{
private static final long serialVersionUID = 1L;
private List< T > list = new ArrayList< T >();
private int columns;
GenericTableModel(int columns){
this.columns = columns;
}
public void updateAll(Collection<T> 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();
}
}
}
}
但是,一旦我宣布它,它就会显示错误信息:
package forms;
import models.GenericTableModel;
import pojo.Mahasiswa;
public class MahasiswaForm extends javax.swing.JFrame {
/**
* Creates new form MahasiswaForm
*/
public MahasiswaForm() {
initComponents();
GenericTableModel<Mahasiswa> tableModel = new GenericTableModel<Mahasiswa>();
}
谁能告诉我有什么问题?我需要这个用于上学,谢谢。
答案 0 :(得分:2)
结帐Row Table Model。它是通用TableModel的一个示例。您需要扩展此类并为POJO实现getValueAt()和setValueAt()方法。
您不应该在getValueAt()
方法中拥有数据库代码。只要表需要呈现单元格,就会经常调用此方法。因此,此代码应该非常有效。 getColumnClass()
方法的注释相同,但如果使用RowTableModel则不会出现问题。
对于完全通用的解决方案,您需要编写自定义TableModel,您还可以查看Bean Table Model