如何将 MySQL Table 中的未来列添加到 ArrayList 并更新我的 jTable?

How to add future columns from a MySQL Table to an ArrayList and update my jTable?

所以我有一个 class 从我的数据库创建公司对象 table

public class Company {
private int id;
private String name;
private double marketValue;
private double lastValue;
private double currentValue;
private String ticker;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public double getMarketValue() {
    return marketValue;
}

public void setMarketValue(double marketValue) {
    this.marketValue = marketValue;
}

public double getLastValue() {
    return lastValue;
}

public void setLastValue(double lastValue) {
    this.lastValue = lastValue;
}

public String getTicker() {
    return ticker;
}

public void setTicker(String ticker) {
    this.ticker = ticker;
}

public double getCurrentValue() {
    return currentValue;
}

public void setCurrentValue(double currentValue) {
    this.currentValue = currentValue;
}

public String toString() {
    return ("Name of Company: " + this.name + ", Last stock price: " + this.lastValue + ", Company Cap: " + this.marketValue + ", ID: " + this.id);
}

public static ArrayList<Company> getCompanies() throws Exception {
    ArrayList<Company> companyList = new ArrayList<Company>();
    try {
        Connection conn = null;
        conn = SqlConnection.getConnection("jdbc:mysql://localhost:3306/stocks");
        Statement stmt = conn.createStatement();
        ResultSet result = stmt.executeQuery("SELECT companies.companies_id, companies.Companies, companies.ticker, companies.marketcap, stockvalue.lastprice FROM stocks.companies, stocks.stockvalue WHERE companies.companies_id = stockvalue.stockvalue_id");
        while (result.next()) {
            Company company = new Company();
            company.setId(result.getInt("companies_id"));
            company.setName(result.getString("Companies"));
            company.setMarketValue(result.getDouble("marketcap"));
            company.setLastValue(result.getDouble("lastprice"));
            company.setTicker(result.getString("ticker"));
            companyList.add(company);
        }
    }
    catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return companyList;

}

}

然后我用 jTable 显示该对象

public class Operations {
private static final String[] COLUMNS = {"Company", "Stock Price"};

public static DefaultTableModel createTableModel() throws Exception {

    DefaultTableModel tableModel = new DefaultTableModel(COLUMNS, 0);

    ArrayList<Company> companyList = Company.getCompanies();
    for (int i = 0; i < companyList.size(); i++) {
        Object[] row = {companyList.get(i).getName(), companyList.get(i).getLastValue()};
        tableModel.addRow(row);
    }

    return tableModel;
}

}

但是现在,我想更进一步,在我的 table 中按日期记录股票价值,例如现在我有这个: https://postimg.org/image/r0v3up2fx/

明天我的程序将添加一个标记为 12_13_16 的附加列,第二天将添加 12_14_16,等等

在这两个 classes 中,我将如何将我每天创建的那些列添加到对象,然后让 jTable 每天显示该附加列?

除了每次 table 更新时为每个新列手动添加新的 set/get 方法之外,我想不出解决方案。

或许您可以使用 ResultSet.getMetaData() 找到您想要的 table 架构。

https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html