0

我曾经在 JTable 中显示我的数据库数据,它工作正常。我发现我需要实现 AbstractTableModel 或 DefaultTableModel 来立即更新数据。

我不确定我应该在 getValueAt() 中写什么?我应该在哪里添加 fireDataChanged()?任何指导表示赞赏,谢谢!

我曾经使用以下代码检索我的数据库数据:

Vector columnNames = new Vector();
Vector data = new Vector();  

try
{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/watchlist","root","root");                   

    String sql = "SELECT * FROM watchlist";
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(sql);
    ResultSetMetaData md = rs.getMetaData();

    int columns = md.getColumnCount();
    for (int i = 1; i <= columns; i++)
    {
        columnNames.addElement( md.getColumnName(i));
    }                               

    int rowCount = md.getColumnCount();             
    while (rs.next())
    {
        Vector row = new Vector(rowCount);

        for (int i=1; i <= rowCount; i++)
        {
            row.addElement( rs.getObject(i) );
        }

        data.addElement( row );
    }

    rs.close();
    stmt.close();
    con.close();                                      
}

catch(Exception e)
{
    System.out.println(e);
} 

我的抽象表模型:

public class MyTableModel extends AbstractTableModel 
{
    Vector columnNames = new Vector(); 
    Vector data = new Vector(); 

    public void connectionDB()
    {
        try
        {                               
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/watchlist","root","root");

             String sql = "SELECT * FROM watchlist";
             Statement stmt = con.createStatement();
             ResultSet rs = stmt.executeQuery(sql);
             ResultSetMetaData md = rs.getMetaData();   
        }

        catch(Exception e)
        {
            System.out.println(e);
        }
    }

    public int getColumnCount() 
    {
        return columnNames.size(); 
    }

    public int getRowCount() 
    {
        return data.size(); 
    }

    public Object getValueAt()
    {
        return data;    
    }

    public boolean isCellEditable(int row, int col)
    {
        return false; 
    }

}
4

2 回答 2

1

这是一个使用示例AbstractTableModel

public class CustomTableModel extends AbstractTableModel
{
    private static final long serialVersionUID = 1L;
    private static final String[] columnNames = new String[]{"ID", "Name", "Number", "Yes/No"};
    protected List<Foo> lstFoo;
    protected Class<?>[] types = new Class[]{Integer.class, String.class, String.class, Boolean.class};

    public CustomTableModel(List<Foo> lstFoo)
    {
        this.lstFoo = lstFoo;
        fireTableDataChanged();
    }

    @Override
    public String getColumnName(int columnIndex)
    {
        return columnNames[columnIndex];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex)
    {
        return types[columnIndex];
    }

    @Override
    public boolean isCellEditable(int row, int columnIndex)
    {
        return false;
    }

    @Override
    public Object getValueAt(int row, int column)
    {
        if(row < 0 || row >= lstFoo.size()) return null;
        Foo obj = lstFoo.get(row);
        switch(column)
        {
            case 0: return obj.getId();
            case 1: return obj.getName();
            case 2: return obj.getNumber();
            case 3: return obj.isYes();
            default: return null;
        }
    }

    @Override
    public int getRowCount()
    {
        return lstFoo.size();
    }

    @Override
    public int getColumnCount()
    {
        return columnNames.length;
    }
}
于 2012-05-11T22:03:16.887 回答
0

只是一点,两个很多连接到一个数据库会降低速度和过多的开销。我遇到了这个问题并且能够解决它。

于 2013-09-16T09:20:52.970 回答