I am executing the code and the table is changed dynamically using a TableModel.
smt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs = smt.executeQuery(query);
rstm = new ResultSetTableModel(rs);
table.setModel(rstm);
In the tablemodel following code is executed.
ResultSetTableModel(ResultSet results) throws SQLException {
this.results = results;
metadata = results.getMetaData();
numcols = metadata.getColumnCount();
results.last();
numrows = results.getRow();
}
But the results.last(); is not getting executed.
java.sql.SQLException: Result set type is TYPE_FORWARD_ONLY
then how can I get the numrows.
IF I use
while(results.next())
{
numrows++;
}
then the cursor will reach at the end.
And when executed the JTable is created with
java.sql.SQLException: Result set type is TYPE_FORWARD_ONLY as content.
Any one have some idea to cure it.
Tino Simon.
Basically that means that the cursor you get from a procedure can't be run other than sequentially. Since, for a table model, you need to have random access by row number, you're going to have to buffer your results (and that means other than in a ResultSet, which is just the JDBC representation of a database cursor).
I take results1 as a buffer to calculate the num of rows,
then automatically the cursor of reults also moves(i think).Finally I am getting the output:
The cursor will reach at the end.
And when executed the JTable is created with
java.sql.SQLException: Result set type is TYPE_FORWARD_ONLY as content.
import java.sql.*;
import javax.swing.table.*;
import javax.swing.event.*;
public class ResultSetTableModel implements TableModel {
ResultSet results,results1;
ResultSetMetaData metadata;
int numcols, numrows=0;
ResultSetTableModel(ResultSet results) throws SQLException {
this.results = results;
this.results1= results;
metadata = results.getMetaData();
numcols = metadata.getColumnCount();
//results.last();
//numrows = results.getRow();
while(results1.next())
{
numrows++;
}
}
public void close() {
try { results.getStatement().close(); }
catch(SQLException e) {};
}
protected void finalize() { close(); }
public int getColumnCount() { return numcols; }
public int getRowCount() { return numrows; }
public String getColumnName(int column) {
try {
return metadata.getColumnLabel(column+1);
} catch (SQLException e) { return e.toString(); }
}
public Class getColumnClass(int column) { return String.class; }
public Object getValueAt(int row, int column) {
try {
results.absolute(row+1);
Object o = results.getObject(column+1);
if (o == null) return null;
else return o.toString();
} catch (SQLException e) { return e.toString(); }
}
public boolean isCellEditable(int row, int column) { return false; }
public void setValueAt(Object value, int row, int column) {}
public void addTableModelListener(TableModelListener l) {}
public void removeTableModelListener(TableModelListener l) {}
}
Tino Simon.