关于jtable显示数据中Vector的问题
下面是一段从数据库取数据,然后添加到jtable的jbinit()代码
ResultSetMetaData rsd = null;
Vector col = new Vector();
Vector dat = new Vector();
sql d = new sql();
Connection con = null;
con = d.date();
Statement s = con.createStatement();
ResultSet rs = s.executeQuery( "Select * from yuangongxinxi ");
rsd = rs.getMetaData();
int i = 0;
for (i = 1; i <= rsd.getColumnCount(); i++) {
col.add(rsd.getColumnName(i));
} while (rs.next()) {
Vector v = new Vector();
for (i = 1; i <= rsd.getColumnCount(); i++) {
v.addElement(rs.getString(i));
}
dat.addElement(v);
}
table = new JTable(dat, col);
table.setBounds(new Rectangle(11, 0, 266, 117));
我想知道的是,这里v.addElement(rs.getString(i));已经循环取值,为什么还要将Vector实例v追加到Vector实例dat里,然后再显示出来,有什么好处吗?v和dat不是同一类吗?
------解决方案--------------------因为while (rs.next()) {
Vector v = new Vector();
for (i = 1; i <= rsd.getColumnCount(); i++) {
v.addElement(rs.getString(i));
}
dat.addElement(v);