所以我有一个可以从数据库表创建公司对象的类

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;
}

}


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

明天我的程序将添加一个附加列,标记为12_13_16,第二天添加12_14_16,依此类推。

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

除了每次我的表更新时为每个新列手动添加一个新的set / get方法外,我无法找到解决方案。

最佳答案

也许您可以使用ResultSet.getMetaData()查找所需的表架构。

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

10-07 15:57
查看更多