Mon Jan 28, 2013 1:20 am
/*
* Created on Dec 11, 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package dbsucxent;
/**
* @author Seah Boon Siew
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
/** table model built from getting metadata about a table in a jdbc database
*/
public class JDBCTableModel extends AbstractTableModel {
Object[][] contents;
String[] columnNames;
Class[] columnClasses;
protected int pageSize;
protected int pageOffset;
public JDBCTableModel(ResultSet results, boolean hasEnded) throws SQLException {
super();
getTableContents(results, hasEnded);
//pageSize = size; //added 130808 - error
}
protected void getTableContents(ResultSet results, boolean hasEnded)
throws SQLException {
results.beforeFirst(); // added by Lee Tzyy Ming
ResultSetMetaData rsmd = results.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
System.out.println("got column results" + results.getWarnings());
ArrayList colClassesList = new ArrayList();
columnNames = new String[numberOfColumns + 1];
columnNames[0] = "Row";
colClassesList.add(Integer.class);
for (int i = 1; i <= numberOfColumns; i++) {
int dbType = rsmd.getColumnType(i);
switch (dbType) {
case Types.INTEGER:
colClassesList.add(Integer.class);
break;
case Types.FLOAT:
colClassesList.add(Float.class);
break;
case Types.DOUBLE:
case Types.REAL:
colClassesList.add(Double.class);
break;
case Types.DATE:
case Types.TIME:
case Types.TIMESTAMP:
colClassesList.add(java.sql.Date.class);
break;
case Types.BIGINT: // added by Lee Tzyy Ming
default:
colClassesList.add(String.class);
break;
}
columnNames[i] = rsmd.getColumnName(i);
}
columnClasses = new Class[colClassesList.size()];
colClassesList.toArray(columnClasses);
// get all data from table and put into
// contents array
ArrayList rowList = new ArrayList();
int row = 1;
while (results.next()) {
ArrayList cellList = new ArrayList();
Object cellValue = new Integer(row++);
cellList.add(cellValue);
for (int i = 1; i < columnClasses.length; i++) {
cellValue = null;
if (columnClasses[i] == String.class) {
if (results.getString(columnNames[i]) != null) {
cellValue = results.getString(columnNames[i]).trim();
} else {
cellValue = results.getString(columnNames[i]);
}
} else if (columnClasses[i] == Integer.class) {
cellValue = new Integer(
results.getInt(columnNames[i]));
} else if (columnClasses[i] == Float.class) {
cellValue = new Float(
results.getInt(columnNames[i]));
} else if (columnClasses[i] == Double.class) {
cellValue = new Double(
results.getDouble(columnNames[i]));
} else if (columnClasses[i] == java.sql.Date.class) {
cellValue = results.getDate(columnNames[i]);
} else {
System.out.println("Can't assign " + columnNames[i]);
}
cellList.add(cellValue);
}// for
Object[] cells = cellList.toArray();
rowList.add(cells);
if (hasEnded == true) {
String[] rowd = new String[2];
rowd[0] = new String("NA");
rowd[1] = new String("No Rows Returned");
String[][] emptyContent = new String[1][];
emptyContent[0] = rowd;
contents = emptyContent;
System.out.println("Aborted");
return;
}
} // while
// finally create contents two-dim array
contents = new Object[rowList.size()][];
for (int i = 0; i < contents.length; i++) {
contents[i] = (Object[]) rowList.get(i);
}
if (contents.length == 0) {
String[] rowd = new String[2];
rowd[0] = new String("NA");
rowd[1] = new String("No Rows Returned");
String[][] emptyContent = new String[1][];
emptyContent[0] = rowd;
contents = emptyContent;
return;
}
}
// AbstractTableModel methods
@Override
/*public int getRowCount() {
return contents.length;
}*/
// Return values appropriate for the visible table part.
public int getRowCount() {
return Math.min(pageSize, contents.length); //added 130808
}
@Override
public int getColumnCount() {
if (contents.length == 0) {
return 0;
} else {
return contents[0].length;
}
}
@Override
public Object getValueAt(int row, int column) {
//int realRow = row + (pageOffset * pageSize); //added 130808
//return contents[realRow][column]; //added 130808
return contents[row][column]; //deleted 130808
}
@Override
public Class getColumnClass(int col) {
return columnClasses[col];
}
@Override
public String getColumnName(int col) {
return columnNames[col];
}
// Use this method to figure out which page you are on.
//added 130808
public int getPageOffset() {
return pageOffset;
}
//added 130808
public int getPageCount() {
return (int) Math.ceil((double) contents.length / pageSize);
}
// Use this method if you want to know how big the real table is . . . we
// could also write "getRealValueAt()" if needed.
//added 130808
public int getRealRowCount() {
return contents.length;
}
//added 130808
public int getPageSize() {
return pageSize;
}
//added 130808
public void setPageSize(int s) {
if (s == pageSize) {
return;
}
int oldPageSize = pageSize;
pageSize = s;
pageOffset = (oldPageSize * pageOffset) / pageSize;
fireTableDataChanged();
/*
* if (pageSize < oldPageSize) { fireTableRowsDeleted(pageSize,
* oldPageSize - 1); } else { fireTableRowsInserted(oldPageSize,
* pageSize - 1); }
*/
}
// Update the page offset and fire a data changed (all rows).
public void pageDown() {
if (pageOffset < getPageCount() - 1) {
pageOffset++;
fireTableDataChanged();
}
}
// Update the page offset and fire a data changed (all rows).
public void pageUp() {
if (pageOffset > 0) {
pageOffset--;
fireTableDataChanged();
}
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.