Current Position:Home > How to use one ResultSet many times in a jsp page ?

How to use one ResultSet many times in a jsp page ?

Update:10-11Source: network consolidation
Advertisement
Hi all,
I have .jsp page and I have used it to get data from DB and display them to users. So I have to get data from DB in number of places in this particular jsp.
I thought that it is better to have one ResultSet for entire page and once it is done its job, the ResultSet will be closed and I can use it again and again like this.
Resultset rs = new ResultSet();
try{
    //My operations
}catch(Exception ex){
   //Handle Exceptions
}finally{
   rs.close();
}After above code snippet I can use same ResultSet again below the page.
I just want to know this,
1. is this a good coding practice?
2. Should i put rs = null; within finally clause?
any help will be appreciated
thank in advance,
Dilan.

The Best Answer

Advertisement
Ok, Finally I switched my coding to use DAO and DTO, and I learned it through internet.
I removed all of data access codes from my jsp file(lets say 'functions.jsp'). I then created one interface and two clasess.
here is my DAO interface.
public interface UserFunctionsDAO{
    public List<UserFunctionsDTO> selectUserList();
}here is DTO class
public class UserFunctionsDTO{
    private String category = "";
    private String sub_category = "";
    private int cat_id = 0;
    private int sub_cat_id = 0;
    public UserFunctionsDTO(){}
    public UserFunctionsDTO(String category, String sub_category, int cat_id, int sub_cat_id){
        this.category = category;
        this.sub_category = sub_category;
        this.cat_id = cat_id;
        this.sub_cat_id = sub_cat_id;
    //Setters and getters will go here.
}my concrete data access class is like this.
public class UserFunctionsDataAccess implements UserFunctionsDAO{
    MyDB dbObject = null;
   private static final String SQL_GET_DISTINCT_CAT= "SELECT DISTINCT cat FROM cat_table";
   public List<UserFunctionsDTO> selectUserList(){
       dbObject = new MyDB();
       dbObject.sqlSelect(SQL_GET_DISTINCT_CAT);
       ResultSet rs = dbObject.getResultSet();
       ArrayList list = new ArrayList();
       while(rs.next()){
           list.add(new UserFunctionsDTO(rs.getString('category'), .......................));
       return list;     
}I think now im following good coding practices, but I have one problem.
1. How do I retrieve this userlist from my jsp page?
2. Should I include UserFunctionsDTO in my jsp page as a bean?
3. If I include it, how can I get the list from it?
thanks in advance,
Dilan.
  • How to use one ResultSet many times in a jsp page ? Update:10-11

    Hi all, I have .jsp page and I have used it to get data from DB and display them to users. So I have to get data from DB in number of places in this particular jsp. I thought that it is better to have one ResultSet for entire page and once it is done

  • Best Practive to resturn Resultset to a JSP page Update:11-30

    I'm writing a class to return a resultSet to use in a JSP page or at least i think thats what i want to do. Whats the best practice to return that resultset.....As the class is written below, when i try to iterate through the resultset in the jsp pag

  • Displaying Same ResultSet Twice In A JSP Page Update:10-11

    I have a table that lists team names in my database. I'm trying to create a page with 2 dropdown boxes where each lists all the teams in the league (in other words, the data read in from the table). I got the first dropdown box to populate w/ this in

  • Database query ResultSet from servlet to JSP page Update:11-30

              Hi there,           I have an Access 2000 database. I am running Apache and Tomcat on Windows Me.           I would like to know if it is possible for me to use a Servlet to search the           database (I know this bit is possible!), but

  • How can a user scroll the resultset obtained from a servlet in a jsp page? Update:10-11

    Actually i am having a page where user has to select company name and for tht i am providing him with a button, and on clicking tht button a new search page is provided , and on mentioning the search criteria ,a servlet is fired which returns a list

  • How to return ResultSet object from Bean to JSP Update:11-30

    I have a Java Bean, in it I have ResultSet Object in a method. Now I want to send this ResultSet Object to the JSP.very good advice, otherwise you will spread your sql exception handling over way too many places. Keep it centered, so you can be sure

  • Returning ResultSet from servlet to jsp - java.lang.NullPointerException Update:11-30

    Hey all, i've been stuck on this for too long now...just trying to return a ResultSet from a servlet to jsp page. Had a bunch of problems earlier...which i think were fixed but...now i get a "java.lang.NullPointerException" in my jsp page when i

  • Controversy in two ResultSet!!! Update:10-11

    Could anyone please provide me a solution for this problem.. I've created two ResultSet rs1 and rs2, both are retrieving the data from the same table. Manipulation of rs2 or any kind of processing should affect the rs1. So, if i updating something in

  • Date based Resultset and Date display Update:10-11

    Hi I have two jsp pages. In first page user selects a Date and name, based on that 2nd jsp display a resultset in a table. I want to Display Selected name and Date on 2nd Page. I done it for name and unable to show the Date and query is also to be mo

  • Can you store the resultSet from a query into a vector? Update:10-11

    I am totally frustrated. How the heck does one store the results of a query (resultSet) in a vector so that I can get the vector from my jsp page to display in a table.? searchRS is my resultSet from stmt.executeQuery(). public Vector storeResultSet