Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get desired no of rows from a select stmt

 
Goldie
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to see only the first 50 rows out of a Select * from tablename statement. How do I do that.
I tried setFetchSize on a resultset but it gives a AbstractMethodError exception.
Could someone ols guide me
Thanks
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Goldie,
First, I might as well tell you before a sheriff gets on your case: Your name is not JavaRanch compliant. Please change it using to your first name 'space' last name.
To your problem, try using Statement.setMaxRows(int).
-Peter
 
Goldie
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am trying to execute this piece of code using setMaxRows on a statement... its not working.. could somebody explain why not!
stmt = conn.createStatement();
stmt.setMaxRows(m_maxRecs);
out.println("max no of rows set to " + m_maxRecs);
rs = stmt.executeQuery(sSQL);
out.println("\n\nExecuting Query");
if ( rs != null ) {
out.println("\nExecuted statement, getting result set");
//rs = stmt.getResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
int numCols = rsmd.getColumnCount();
out.println("Cols:" + numCols);
while( rs.next() ) {... do smthng};
Its not returning any results.. whereas when I take out setMaxRows ... it gives the result fine.
Thanks and Regards
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Goldie,
What database are you using? Who implemented your driver? It should work. I don't have a problem using the setMaxRows().
-Peter
[This message has been edited by Peter Tran (edited January 16, 2001).]
 
Goldie
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Sybase 12.0 and the drivers are set accordingly.. it works for me in my test program.. but when I try to implement it over here.. it doesnt go thru.... any reasons?
Thanks
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Goldie,
If it works in your test program, then it should work here also. Why are you getting a metadata object between executing your statement and go through the result set via rs.next()? Maybe your result set is getting corrupted with the metadata.getColumns(). Trying moving the while(rs.next()) next to the ResultSet rs = stmt.getResultSet().
-Peter
 
Goldie
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
I put a try catch block around the
rs = stmt.executeQuery(sSQL);
and this is the exception I am getting
java.sql.SQLException: JZ0SJ: Metadata accessor information was not found on this
database.
Could this be a problem, cause I am using Tomcat on my sample program whereas Apache on this one??
And also the statement rs.getResultSet() is commented out.. I am not using it in the program.
Thanks,
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Goldie,
I'm not sure why you're getting the exception. Since I haven't ever had to use Sybase, I can't comment on your problem. Have you tried using a different vendor for the JDBC implementation?
-Peter
 
Nitin Shivaram
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Ok there are two ways of doing this.
1) Set rownum = 50 in the where clause of your sql query.
2) Try using the Statement.setFetchSize(50) or ResultSet.setFetchSize(50).
Cheerio
------------------
*******
Nitin S
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic