This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes How to get desired no of rows from a select stmt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to get desired no of rows from a select stmt" Watch "How to get desired no of rows from a select stmt" New topic
Author

How to get desired no of rows from a select stmt

Goldie
Greenhorn

Joined: Dec 28, 2000
Posts: 11
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

Joined: Jan 02, 2001
Posts: 783
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

Joined: Dec 28, 2000
Posts: 11
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

Joined: Jan 02, 2001
Posts: 783
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

Joined: Dec 28, 2000
Posts: 11
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

Joined: Jan 02, 2001
Posts: 783
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

Joined: Dec 28, 2000
Posts: 11
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

Joined: Jan 02, 2001
Posts: 783
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

Joined: Jan 17, 2001
Posts: 71
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


Nitin S<br />Sun Certified Java Programmer for the Java 2 platform.<br />Tekmetrics Certified Java Programmer For the Java 2 Platform.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get desired no of rows from a select stmt
 
Similar Threads
Rows count in hql
To Retrieve multiple rows separately
How to select resultset in range?
How to find no of Rows in my ResultSet
fetch first in Oracle