2 Replies Latest reply on Mar 24, 2007 5:07 PM by 807599

    arrays and resultsets

    807599
      okay so in my servlet i do a query, get a resultset, and pass it to a bean, then forward to a jsp page.

      well in the bean it turns it into a multidimentional array, and then the jsp page grabs it and does a few loops and pulls the information out.

      this is what i was intending my code to do, but i always get errors. when the jsp grabs the array, does the bean need to serialize it before its passed?

      and i dont know the exact wording that was used, but i read that once you use the resultset, it closes, is there any way to not get it to? because i have a methd that counts the rows in the rs in order to get the length for the array. if anybody needs the code pasted i got it here, but its not my programing, its just my understanding of the laguage.
        • 1. Re: arrays and resultsets
          JosAH
          and i dont know the exact wording that was used, but i read that once
          you use the resultset, it closes, is there any way to not get it to?
          because i have a methd that counts the rows in the rs in order to get
          the length for the array. if anybody needs the code pasted i got it here,
          but its not my programing, its just my understanding of the laguage.
          ResultSets don't know how many rows they represent. They are kind of
          stupid that way, i.e. you have to scroll to the last row (that they know of)
          and get the row number. It's a bit tedious and you shouldn't be interested
          in the number of rows at all. Therefore you shouldn't use arrays for the
          job; look at Lists instead. Two implementations are ArrayList and
          LinkedList. They keep you from using a scrollable resultset and they
          save you a lot of work (a JSP can handle Lists fine).

          kind regards,

          Jos
          • 2. Re: arrays and resultsets
            807599
            Jos said it right, but let me just repeat it, becaue it's that important.

            Your servlet, or whattever backend code is querying the database,
            should take the result set, interate through it and copy the data
            into a list. It's best if each element in the list is an instance of a class
            you've written to represent what a row of data represents in your business model.
            Once you've generated this list, you close the ResultSet.

            Of course, you will close your (Prepared)Statement and Connection as
            soon as possible, too.

            When the list gets passed to the front end code (JSP) there should be no
            mention of the database in sight. For example, you should be able to test your
            front end wihout a database involved, by forwarding it canned test data.