Resultset + TYPE_FORWARD_ONLY

Hai Friends,The resultset I am obtaining from a query is TYPE_FORWARD_ONLY. And the cursor is now pointing to the last row.How can I move this cursor to the first row.(The problem is th result set is TYPE_FORWARD_ONLY). Anyone have some idea.....Tino
[300 byte] By [tinosimona] at [2007-10-2 4:27:09]
# 1
Simon,If you post this question to the JDBC forum ( http://forum.java.sun.com/forum.jspa?forumID=48 ) there is a better chance of getting it answered.[url http://www.feedfeeds.com/feedtv]FeedTV[/url]
jessua at 2007-7-15 23:56:02 > top of Java-index,Desktop,Core GUI APIs...
# 2
Execute the query again. FORWARD_ONLY means what it says.
malcolmmca at 2007-7-15 23:56:02 > top of Java-index,Desktop,Core GUI APIs...
# 3

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.

tinosimona at 2007-7-15 23:56:02 > top of Java-index,Desktop,Core GUI APIs...
# 4
The Problem is coming only when executing Stored procedures.Other Sql queries are executed nicely. Tino Simon..
tinosimona at 2007-7-15 23:56:02 > top of Java-index,Desktop,Core GUI APIs...
# 5

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).

malcolmmca at 2007-7-15 23:56:02 > top of Java-index,Desktop,Core GUI APIs...
# 6

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.

tinosimona at 2007-7-15 23:56:02 > top of Java-index,Desktop,Core GUI APIs...
# 7
hai friends, I still could'nt overcome the HURDLE. Tino Simon.
tinosimona at 2007-7-15 23:56:02 > top of Java-index,Desktop,Core GUI APIs...