This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

myresultset.first

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when I call myresultset.first() , I am getting an error saying only forward or something. myresultset.Next() works fine. please help
Thanks,
Krupa
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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") ;
.
.
.
}


------------------
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 358
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I got it now.
 
krupa devi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You've gotta fight it! Don't give in! Read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic