文档详情

JTable_数据库模型.doc

发布:2016-07-22约2.52千字共5页下载文档
文本预览下载声明
JTable 的数据库模型 2007-04-08 11:43 package com.theone.rm.tool; import java.util.*; import java.sql.*; public class MyTableModel extends javax.swing.table.AbstractTableModel { ??? protected Vector rows=new Vector(); ??? protected Vector columns=new Vector(); ??? private ResultSet rs=null; ??? public MyTableModel() ??? { ??????? ; ??? } //这里传入一个记录集 ??? public MyTableModel(ResultSet rs) { ??????? //生成数据模型 ??????? this.rs=rs; ??????? try { ??????????? while (this.rs.next()) { ??????????????? Vector temp = new Vector(); ??????????????? for (int i = 0; i rs.getMetaData().getColumnCount(); i++) { ??????????????????? temp.add(rs.getString(i + 1)); ??????????????? } ??????????????? this.rows.add(temp); ??????????? } ??????????? for (int i = 0; i rs.getMetaData().getColumnCount(); i++) { ??????????????? this.columns.add(rs.getMetaData().getColumnName(i + 1)); ??????????? } ??????? } catch (SQLException ex) { ??????????? ex.printStackTrace(); ??????? } ??????? //关闭连接 ??????? try { ??????????? if (this.rs != null) { ??????????????? rs.close(); ??????????? } ??????? } catch (SQLException se) { ??????????? se.printStackTrace(); ??????? } ??? } ??? public Vector getRows() ??? { ??????? return this.rows; ??? } ??? public Vector getColumns() ??? { ??????? return this.columns; ??? } ??? /** ???? * 得到给定位置的数据 ???? * @param row int 要得到的值所在的行 ???? * @param col int 要得到的值所在的列 ???? * @return Object 数据 ???? */ //得到哪一行 哪一列 ??? public Object getValueAt(int row,int col) ??? { ??????? Vector temp=(Vector)rows.get(row); ??????? return temp.get(col); ??? } ??? /** ???? * 设置给定位置的数据 ???? * @param aValue Object 一个新的值 ???? * @param row int 要更改的值所在行 ???? * @param col int 要更改的值所在的列 ???? */ ??? public void setValueAt(Object aValue,int row,int col) ??? { ??????? Vector temp=(Vector)this.rows.get(row); ??????? temp.set(col,aValue); ??? } ??? /** ???? * 得到DBGrid数据摸型的列数 ???? * @return int ???? */ ??? public int getColumnCount() ??? { ????
显示全部
相似文档