/*********************************************************************/
/*(c) Copyright IBM Corp. 2003 All rights reserved. */
/* */
/*This sample program is owned by International Business Machines */
/*Corporation or one of its subsidiaries ("IBM") and is copyrighted */
/*and licensed, not sold. */
/* */
/*You may copy, modify, and distribute this sample program in any */
/*form without payment to IBM, for any purpose including developing,*/
/*using, marketing or distributing programs that include or are */
/*derivative works of the sample program. */
/* */
/*The sample program is provided to you on an "AS IS" basis, without */
/*warranty of any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL */
/*WARRANTIES EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO*/
/*THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTIC-*/
/*ULAR PURPOSE. Some jurisdictions do not allow for the exclusion or */
/*limitation of implied warranties, so the above limitations or */
/*exclusions may not apply to you. IBM shall not be liable for any */
/*damages you suffer as a result of using, modifying or distributing */
/*the sample program or its derivatives. */
/* */
/*Each copy of any portion of this sample program or any derivative */
/*work, must include a the above copyright notice and disclaimer of */
/*warranty. */
/* */
/*********************************************************************/
package com.ibm.DB2Excel;
/*********************************************************************/
/******************************************************************/
/* Stores the contents of a database table */
/******************************************************************/
import javax.swing.table.AbstractTableModel;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.*;
/**
* Implements the TableModel interface so that a JTable can display the contents of ResultSet.
**/
public class ResultSetTableModel extends AbstractTableModel
{
/**
* number of rows in the ResultSet
*/
private int rowCount;
/**
* number of columns in ResultSet
*/
private int colCount;
/**
* Simple facade that hides the details of executing JDBC commands
*/
private SQLFacade sqlFacade;
/**
* The names of the columns in the ResultSet
*/
private List columnNames = new ArrayList();
/**
* The class types of the columns being displayed.
*/
private List columnTypes = new ArrayList();
/**
* Transcribes the data in the ResultSet into a List of lists
*/
private List result = new ArrayList();
/**
* Name of the database table being displayed
*/
private String tableName;
public ResultSetTableModel(SQLFacade facade) {
this.sqlFacade = facade;
}
// ----------------------------------------------------------------------------------------- //
// methods of javax.swing.table.TableModel interface
// ----------------------------------------------------------------------------------------- //
public int getRowCount() {
return rowCount;
}
public int getColumnCount() {
if(rowCount > 0)
return colCount+1;
else
return 0;
}
public Object getValueAt(int rowIndex, int columnIndex) {
if(columnIndex == 0)
return new Integer(rowIndex);
else
return ((ArrayList) result.get(rowIndex)).get(columnIndex-1);
}
public String getColumnName(int colIndex) {
if(colIndex == 0)
return "Row";
else
return (String) columnNames.get(colIndex-1);
}
/**
* returns the list of column names
*/
public List getColumnNames() {
return columnNames;
}
/**
* reloads the TableModel with the contents of specified tableName
* @param tableName table with which to reload the TableModel
* @exception SQLException if a database error occurs
* @exception ClassNotFoundException
*/
public void reloadTableModel(String tableName) throws SQLException, ClassNotFoundException {
ResultSet rs = null;
this.tableName = tableName;
try {
rs = sqlFacade.executeQuery("SELECT * from " + tableName);
clearAll();
updateColumnModel(rs.getMetaData());
while (rs.next()) {
ArrayList list = new ArrayList();
for (int i = 1; i <= colCount; i++) {
Object o = rs.getObject(i);
list.add(o);
}
result.add(list);
rowCount++;
}
fireTableStructureChanged();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException se) {
}
}
}
}
private void updateColumnModel(ResultSetMetaData metaData) throws SQLException, ClassNotFoundException {
colCount = metaData.getColumnCount();
for (int i = 1; i <= colCount; i++) {
String colName = metaData.getColumnLabel(i);
String colClass = metaData.getColumnClassName(i);
columnTypes.add(Class.forName(colClass));
columnNames.add(colName);
}
}
/**
* clears the TableModel
*/
private void clearAll() {
for (int i = 0; i < result.size(); i++) {
ArrayList list = (ArrayList) result.get(i);
list.clear();
}
result.clear();
columnNames.clear();
columnTypes.clear();
colCount = 0;
rowCount = 0;
}
public List getRow(int i) {
return (List) result.get(i);
}
}
No comments:
Post a Comment