当前位置: 技术问答>java相关
求救:java中如何实现把ResultSet中的记录显示到JTable控件中?
来源: 互联网 发布时间:2015-11-05
本文导语: 在Java核心技术(卷二)中介绍了通过建立表模型实现表中显示数据的思想,即继承一个AbstractTableModel类(假设为model),实现其中的方法,如getRowCount(),getColumnCount(),getValueAt() 等等,然后调用Java的构造函数JTable table=new...
在Java核心技术(卷二)中介绍了通过建立表模型实现表中显示数据的思想,即继承一个AbstractTableModel类(假设为model),实现其中的方法,如getRowCount(),getColumnCount(),getValueAt() 等等,然后调用Java的构造函数JTable table=new JTable(model);
令我不解的是,这样就能实现将结果集中的数据放到表格中了吗?也没看出用了什么方法啊。model中实现的诸如getRowCount()之类的方法,是为具体操作表格中的数据,并没有因此把数据放进表格吧
我大脑有些混乱了,还望高手们不惜赐教!!!
另外还有一个白痴性问题:表格中的第一行是0还是1?ResultSet中的第一行呢?
令我不解的是,这样就能实现将结果集中的数据放到表格中了吗?也没看出用了什么方法啊。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 ) ;
> }
> }
> 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);
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);