Solved

MVC - Looping through a ResultSet

Posted on 2002-03-29
7
703 Views
Last Modified: 2013-11-24
I am developing a site and learning the MVC approach at the same time.

I currently have a controller that takes a parameter to determine what page the user wants to go to. Using external XML files i store information about what servlet and jsp to load.

Once the destination has been determined, for example 'news', the newsServlet is invoked which calls a DB Query method to extract ( for example ) the latest 10 news items.

This i have working perfectly so far with a ResultSet returned.

What i want to do now is using a bean and its accompanying JSP loop through the ResultSet and display the results. I also want to implement paging into the solution. (display n number of records per page with the ability to move back and forward through the ResultSet ie 10 records at a time ).

As a solution i assume i could save the ResultSet into a session. Is this a good idea, if so how do i go about coding this solution and are there more acceptable methods to do this?

thanking you all in advance,

Damian
0
Comment
Question by:dgeorgio
  • 5
  • 2
7 Comments
 

Accepted Solution

by:
rashidkamranbs earned 200 total points
ID: 6906498
First for moving foward and Backward you need to create a scroll able result set.

Second you can use the same absolute(int) method of result set to generate paging explicitly.

Thrid it wouldn't be a good chioce to place a ResultSet in a session.. cuz the connection will remain open untill the pagin complete..

alternatively you can use XML to cache the results.. n pagig in a XML is not a big deal


0
 

Author Comment

by:dgeorgio
ID: 6910079
Sorry but i am still not sure how i am suppose to go about iterating through the resultSet returned by the servlet using the bean and the JSP. ( I am very new to Java and learning as I go ).

0
 

Expert Comment

by:rashidkamranbs
ID: 6910917
See when you create your statement you need to tell what kinda resultset ya want.. for scrolling in either direction ur createStatement method could be something like

Statement st=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE );

The above line is one of the approach..
now the suppsoe rs is the ResultSet object return by this statement.. you can move in either direction by calling

rs.absolute(rowNo);
or
rs.next();
or
rs.previous();

Now i dont think if there is ne problem to do paging kinda stuff while there are number of methods with boolean returned type
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:dgeorgio
ID: 6911753
Yes, thankyou that all makes sense but I am unsure how i access the information stored in the ResultSet with the use of the bean and the jsp.

This is currently what i have.

I have a servlet that when invoked by the controller runs a query on the database producing a ResultSet ( lets call it rs )

All this is working fine. What i need to do now is access the data stored in the ResultSet called rs via the use of a bean and writing it out in the jsp page.

I suppose what i need to know is:

1. Within the bean, how do i access the ResultSet.
2. How do i access certain columns withing the ResultSet (ie firstName )
3. how do i loop through the ResultSet to display/write all rows returned?

I hope o have made this clear enough.
Thanking you for your help rashidkamranbs.
0
 

Author Comment

by:dgeorgio
ID: 6911791
Yes, thankyou that all makes sense but I am unsure how i access the information stored in the ResultSet with the use of the bean and the jsp.

This is currently what i have.

I have a servlet that when invoked by the controller runs a query on the database producing a ResultSet ( lets call it rs )

All this is working fine. What i need to do now is access the data stored in the ResultSet called rs via the use of a bean and writing it out in the jsp page.

I suppose what i need to know is:

1. Within the bean, how do i access the ResultSet.
2. How do i access certain columns withing the ResultSet (ie firstName )
3. how do i loop through the ResultSet to display/write all rows returned?

I hope o have made this clear enough.
Thanking you for your help rashidkamranbs.
0
 

Author Comment

by:dgeorgio
ID: 6911908
Yes, thankyou that all makes sense but I am unsure how i access the information stored in the ResultSet with the use of the bean and the jsp.

This is currently what i have.

I have a servlet that when invoked by the controller runs a query on the database producing a ResultSet ( lets call it rs )

All this is working fine. What i need to do now is access the data stored in the ResultSet called rs via the use of a bean and writing it out in the jsp page.

I suppose what i need to know is:

1. Within the bean, how do i access the ResultSet.
2. How do i access certain columns withing the ResultSet (ie firstName )
3. how do i loop through the ResultSet to display/write all rows returned?

I hope o have made this clear enough.
Thanking you for your help rashidkamranbs.
0
 

Author Comment

by:dgeorgio
ID: 6912140
Yes, thankyou that all makes sense but I am unsure how i access the information stored in the ResultSet with the use of the bean and the jsp.

This is currently what i have.

I have a servlet that when invoked by the controller runs a query on the database producing a ResultSet ( lets call it rs )

All this is working fine. What i need to do now is access the data stored in the ResultSet called rs via the use of a bean and writing it out in the jsp page.

I suppose what i need to know is:

1. Within the bean, how do i access the ResultSet.
2. How do i access certain columns withing the ResultSet (ie firstName )
3. how do i loop through the ResultSet to display/write all rows returned?

I hope o have made this clear enough.
Thanking you for your help rashidkamranbs.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now