wood burning stoves*
The moose likes JDBC and the fly likes myresultset.first Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "myresultset.first" Watch "myresultset.first" New topic
Author

myresultset.first

krupa devi
Greenhorn

Joined: Jul 02, 2001
Posts: 14
when I call myresultset.first() , I am getting an error saying only forward or something. myresultset.Next() works fine. please help
Thanks,
Krupa
Daniel Dunleavy
Ranch Hand

Joined: Mar 13, 2001
Posts: 276
I believe that when the resultset is returned, the cursor is just before the first row. When you do the .next you are placing it at the first row, and can get the data.
I believe the .first() is for when you have processed some of the data, and wish to GO BACK to the first row. Whereas you were not even at the first row yet. And I think that is why you got the err.
Dan
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
Its not beacuse of .next()
I tried to use first() method but it gives an error
Exception in thread "main" java.lang.UnsupportedOperationException
at sun.jdbc.odbc.JdbcOdbcResultSet.first(Unknown Source)
at tableNames.main(tableNames.java:103)


Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Mohamed Yousuff
Ranch Hand

Joined: Jun 23, 2001
Posts: 73

In order to work with JDBC, one needs to understand the resultset object first. The most important method one should know is next() method. When a query is executed the resultset acts as a cursor to a single row to the result of the query, i.e. the records. Initially, it is just before the first record. When the next() method is called, it checks if it can move to the next record. If there is no record it returns false. If there is a record, then it moves to the next record and returns true. That is all about resultset. Generally it is used this way as shown below.
while( resultset.next() ){
String str = resultset.getString("xxx") ;
.
.
.
}


------------------
jlrober
Greenhorn

Joined: Jun 25, 2001
Posts: 10
Originally posted by parmeet bedi:
Its not beacuse of .next()
I tried to use first() method but it gives an error
Exception in thread "main" java.lang.UnsupportedOperationException
at sun.jdbc.odbc.JdbcOdbcResultSet.first(Unknown Source)
at tableNames.main(tableNames.java:103)

parmeet:
your problem lies in the type of statement you are creating. By default the cursor is of type forward only which allows you to use the ResultSet.next() method. It however will not let you use the method ResultSet.first() as it may have to move the cursor backwards. If you want to use the .next/.last methods then you have to declare a statement of type scroll insensitive(can move forwards and backwards through the resultset. Here is an example:
ResultSet results;
Statement statement;
statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
results = statement.executeQuery(query);
results.last();
transfers = results.getRow(); //returns the number of rows returned by the query
results.first();
//continue processing the results...
hope that helps,
Jamie
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
Thanks I got it now.
krupa devi
Greenhorn

Joined: Jul 02, 2001
Posts: 14
I am calling a stored procedure and I tried to use rs.Last() and I am still getting the error,
"Result set type is TYPE_FORWARD_ONLY
at sun.jdbc.odbc.JdbcOdbcResultSet.last(JdbcOdbcResultSet.java:2217)".
I tried using rs.setFetchDirection(rs.TYPE_SCROLL_INSENSITIVE);
it didn't work.
here is my code.
CallableStatement cs = con.prepareCall("{Call StoredProc1 (?,?,?,?)}" );
cs.setString(1,"100");
cs.setString(2,"TESTLAST");
cs.setString(3,"TESTMIDDLE");
cs.setString(4,"TESTFIRST");
ResultSet rs = cs.executeQuery();
rs.setFetchDirection(rs.TYPE_SCROLL_INSENSITIVE);
int fetchdirection = rs.getFetchDirection();
System.out.println(fetchdirection);

rs.last();
int rowCnt = rs.getRow();
System.out.println(rowCnt);
rs.first();

Please help.
Thanks,
Krupa
jlrober
Greenhorn

Joined: Jun 25, 2001
Posts: 10
Just a side note...
the ResultSet.CONCUR_READ_ONLY property will have to be changed if your planning on allowing updates to the selected records. It is the ResultSet.TYPE_SCROLL_INSENSITIVE property that renders the resultset scrollable. You probably already knew that but it may save you some time in the future.
good luck,
Jamie
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: myresultset.first
 
Similar Threads
Cant use object from another class
WA #1.....word association
Posting to bump up my count...
Text box value is not being completely filled with request attribute
The x900 effect ?