java 表格模型 vector_求救:java中如何实现把ResultSet中的记录显示到JTable控件中?...

在Java核心技术(卷二)中介绍了通过建立表模型实现表中显示数据的思想,即继承一个AbstractTableModel类(假设为model),实现其中的方法,如getRowCount(),getColumnCount(),getValueAt() 等等,然后调用Java的构造函数JTable table=new JTable(model);

令我不解的是,这样就能实现将结果集中的数据放到表格中了吗?也没看出用了什么方法啊。model中实现的诸如getRowCount()之类的方法,是为具体操作表格中的数据,并没有因此把数据放进表格吧

我大脑有些混乱了,还望高手们不惜赐教!!!

另外还有一个白痴性问题:表格中的第一行是0还是1?ResultSet中的第一行呢?

|

> 2) You can build your own table model which will be queried by JTable for data in each cell.How to build a table model? Table model will be a class which inherits AbstractTableModel class and will override at least three of its methods viz.

> public int getColumnCount ( ) ;

> public int getRowCount ( ) ;

> public Object getValueAt ( int rowNo, int coulnmNo ) ;

> If you just override these three methods then your column's header will be given as A, B, C ... . So if you want to supply your own column headers ( certainly you would ) then you need to override another method

> public String getColumnName ( int col ) ;

> By methods names you can guess what do you need to write in those methods. However, I will give you hints for every methods. Your table model class will be as follows.

> class MalaTableModel extends AbstractTableModel {

> private ResultSet rs ;

> private ResultSetMetaData rsmd ; // You need to populate it

> public MalaTableModel ( ResultSet rs ) {

> this.rs = rs ;

> // Get the meta data for result set here

> }

> public int getRowCount ( ) {

> // If you are using ODBC2.0 then you can get the

> // row count from a result set as follows

> rs.last ( ) ;

> int rowCount = rs.getRow ( ) ;

> return rowCount ;

> // if rs.last ( ) is not supported then

> /*

> Keep scrolling from 1st to last row and return the total

> no of rows

> */

> }

> public int getColumnCount ( ) {

> return rsmd.getColumnCount ( ) ;

> }

> public getValueAt ( int row, int col ) {

> // Scroll the result set to the row

> rs.absolute ( row + 1 ) ;

> return rs.getObject ( col + 1 ) ;

> }

> public String getColumnName ( int col ) {

> return rsmd.getColumnName ( col + 1 ) ;

> }

> }

|

ResultSet rst = conn.exe_select(sqlstr);

Vector vdata = new Vector();

Vector vname = new Vector();

vname.add("column1");

vname.add("column2");

vname.add("column3");

Vector vrow = null;

while (rst.next ()) {

vrow = new Vector();

vrow.add (rst.getString (1));

vrow.add (rst.getString (2));

vrow.add (rst.getString (3));

vdata.add (vrow);

}

DefaultTableModel model = new DefaultTableModel(vdata, vname);

jTable.setModel (model);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值