import java.sql.*;
import javax.swing.table.*;
public class ResultSetTableModel extends AbstractTableModel {
private Connection connection;
private Statement statement;
private ResultSet resultSet;
private ResultSetMetaData metaData;
private int numberOfRows;
private boolean connectedToDatabase = false;
private int leftColumnsHiding = 0;
public ResultSetTableModel(final String driver, final String url,
final String query) throws SQLException, ClassNotFoundException {
Class.forName(driver);
connection = DriverManager.getConnection(url);
statement = connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
connectedToDatabase = true;
setQuery(query);
}
@Override
public Class getColumnClass(final int column) throws IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Not Connected to Database");
}
try {
String className = metaData.getColumnClassName(column + 1);
return Class.forName(className);
} catch (Exception exception) {
exception.printStackTrace();
}
return Object.class;
}
public int getColumnCount() throws IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Not Connected to Database");
}
try {
return metaData.getColumnCount() - leftColumnsHiding;
} // catch SQLExceptions and print error message
catch (SQLException sqlException) {
sqlException.printStackTrace();
}
return 0;
}
@Override
public String getColumnName(final int column) throws IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Not Connected to Database");
}
try {
return metaData.getColumnName(column + 1 + leftColumnsHiding);
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
return "";
}
public int getRowCount() throws IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Not Connected to Database");
}
return numberOfRows;
}
public Object getValueAt(final int row, final int column)
throws IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Not Connected to Database");
}
try {
resultSet.absolute(row + 1);
return resultSet.getObject(column + 1 + leftColumnsHiding);
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
return "";
}
public void setQuery(final String query)
throws SQLException, IllegalStateException {
if (!connectedToDatabase) {
throw new IllegalStateException("Not Connected to Database");
}
resultSet = statement.executeQuery(query);
metaData = resultSet.getMetaData();
resultSet.last(); // move to last row
numberOfRows = resultSet.getRow(); // get row number
fireTableStructureChanged();
}
public void disconnectFromDatabase() {
try {
statement.close();
connection.close();
} catch (SQLException sqlException) {
sqlException.printStackTrace();
} finally {
connectedToDatabase = false;
}
}
public void setLeftColumnsHiding(final int leftColumnsHiding) {
this.leftColumnsHiding = leftColumnsHiding;
}
public Object getHidingValueAt(final int row, final int column)
throws IllegalStateException, IllegalArgumentException {
if (!connectedToDatabase) {
throw new IllegalStateException("Not Connected to Database");
}
if (column >= leftColumnsHiding) {
throw new IllegalArgumentException("Wrong column index: " + column +
" >= " + leftColumnsHiding);
}
try {
resultSet.absolute(row + 1);
return resultSet.getObject(column + 1);
} catch (SQLException sqlException) {
sqlException.printStackTrace();
}
return "";
}
}