📄 resultmodel.java
字号:
/*
* ResultTableModel.java
*
* Created on 2007年5月9日, 上午9:12
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.cn.szhaccp.sill;
import javax.swing.table.AbstractTableModel;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;
import java.sql.*;
public class ResultModel extends AbstractTableModel {
public void setResult(ResultSet results) {
try {
ResultSetMetaData metaData = results.getMetaData();
int columnLength = metaData.getColumnCount();
columnNames = new String[columnLength];
//设置列名
for(int i = 0; i < columnNames.length; i++) {
columnNames[i] = metaData.getColumnName(i + 1);
}
String[] columnData =null;
dataRows.clear();
//设置单元格的数据
while(results.next()) {
columnData= new String[columnNames.length];
for(int i = 0; i < columnNames.length; i++) {
columnData[i] = results.getString(i + 1);
}
dataRows.add(columnData);
}
this.fireTableChanged(null);//通知JTable重新显示数据
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public void delData(int row) {
dataRows.removeElementAt(row);
this.fireTableChanged(null);
}
public void refresh() {
this.fireTableChanged(null);
}
public String getColumnName(int column) {
return columnNames[column];
}
/**
* Returns the number of columns in the model.
*
* @return the number of columns in the model
* @todo Implement this javax.swing.table.TableModel method
*/
public int getColumnCount() {
return columnNames.length;
}
/**
* Returns the number of rows in the model.
*
* @return the number of rows in the model
* @todo Implement this javax.swing.table.TableModel method
*/
public int getRowCount() {
return dataRows.size();
}
/**
* Returns the value for the cell at <code>columnIndex</code> and
* <code>rowIndex</code>.
*
* @param rowIndex the row whose value is to be queried
* @param columnIndex the column whose value is to be queried
* @return the value Object at the specified cell
* @todo Implement this javax.swing.table.TableModel method
*/
public Object getValueAt(int rowIndex, int columnIndex) {
return dataRows.elementAt(rowIndex)[columnIndex];
}
private Vector<String[]> dataRows = new Vector<String[]>();
private String[] columnNames = new String[0];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -