【发布时间】:2014-03-04 07:11:19
【问题描述】:
我想将结果集中的数据显示到JTable。
当我运行以下代码时,表格不会更新。
public void getHouses(int price) {
Connection conn;
ArrayList<Integer> ID = new ArrayList<Integer>();
ArrayList<String> Price = new ArrayList<String>();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:Houses");
Statement statement = conn.createStatement();
ResultSet rec = statement.executeQuery("SELECT * FROM Houses WHERE Price <= " + price + "");
while (rec.next()) {
ID.add(rec.getInt("ID"));
Price.add(rec.getString("Price"));
}
String[] columnNames = {"House ID", "House Price"};
Object[][] rows = new Object[ID.size()][2];
for (int i = 0; i < ID.size(); i++) {
rows[i][0] = ID.get(i);
rows[i][1] = Price.get(i);
}
jTable1 = new JTable(rows, columnNames);
statement.close();
} catch (SQLException se) {
} catch (ClassNotFoundException cnf) {}
}
注意!
我通过拖放将JTable 添加到gui。
我还测试了我的结果集中是否包含数据。
【问题讨论】:
-
您创建了一个新的
JTable,但您没有将其添加到任何内容中 -
@MadProgrammer JTable 已经添加了,我只是创建了一个新实例?
-
检查this answer...
-
没错,所以你现在有一个在屏幕上,另一个不在...
-
@MadProgrammer 好的,所以我添加了 JTable 但仍然没有
标签: java swing netbeans jdbc jtable