File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Storing resultset into arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Storing resultset into arraylist " Watch "Storing resultset into arraylist " New topic
Author

Storing resultset into arraylist

Bhardwaj Shweta
Ranch Hand

Joined: Mar 29, 2011
Posts: 36

hello

I need to store my result set into an ArrayList and then to display data in a jsp page..

//coding in servlet


//coding is jsp page



but its not showing anything,may be its taking long time to process it..please help


Regards,
Shweta Bhardwaj
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3054
    
  33

You must be getting ClassCastException. If you carefully observe in the servlet you have an ArrayList of ArrayList but in the jsp you are trying to access the list as just a list of Strings and that might be the reason why you are seeing no output. Alternatively you can have one class for your Resultset data- say MyClass and each of the attributes are the columns from the resultset. And then you can have an arraylist declared as ArrayList<MyClass>.

Also try to remove the Java code dangling withing the JSP- You might want to make use of JSP tag library for traversing through the lists.


Mohamed Sanaulla | My Blog
Bhardwaj Shweta
Ranch Hand

Joined: Mar 29, 2011
Posts: 36

hello Mohamed

can i typecast it into Arraylist!!!

as far as separate class for result set,i am new to java and quite apprehensive regarding making so many classes ,i think i will get confuse :P i know you are telling me the correct approach but can i fix this problem and then make my code better by making separate classes.

and can you give me some example how to use tag library for traversing through the lists.

Thank you so much
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3054
    
  33

You have to typecast it to ArrayList and then traverse. You can add an instanceof check before typecasting and if the instanceof fails then you can tell the user that there was some issue in retreiving the data. It will not affect if you have too many classes. It will only help you modularize your application.

For JSTL you can visit this: http://jstl.java.net/ Its basically set of JSP tags which you can use instead of these Java code.
utsav gupta
Ranch Hand

Joined: Mar 19, 2009
Posts: 57
Hi

What i would suggest is to remove the ArrayList 'list' and simply pass a1 on to the Jsp. Hence in the Jsp you just have to change the 'list' to 'a1' and access all the elements straightway from it.

Regards
utsav.
Bhardwaj Shweta
Ranch Hand

Joined: Mar 29, 2011
Posts: 36

here is my complete code:
//jsp page

// servlet



i tried to typecast it but its still not showing any result..
can you please modify the code
Thank you
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

Why use two ArrayList instances here?
Try with one array list instance, then you may populate the list as you need. Also move your request dispatcher code out of the result set access code.

And you better off populating the appropriate object with the data and add it to the array list then retrieve them in the JSP. As already noted move all the Java code from the JSP instead use the JSTL, EL , standard.custom actions for data manipulations in JSP.

SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Bhardwaj Shweta
Ranch Hand

Joined: Mar 29, 2011
Posts: 36

@vijitha

I have changed my code to

and servlet is working fine
but in my jsp page its showing value of String s6 = rs.getString("priority") in all the fields..
error is in my logic
// jsp


when the control gets out of the loop mystring holds value of s6. how to fix it
Bhardwaj Shweta
Ranch Hand

Joined: Mar 29, 2011
Posts: 36

in between i am learning how to use JSTL and EL.
Bhardwaj Shweta
Ranch Hand

Joined: Mar 29, 2011
Posts: 36

In order to use JSTL
i have added <%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%>

but its showing and error:
"The absolute uri:http://java.sun/jstl/core cannot be resolved in either web.xml or the jar files deployed with application"
i have already added two jar files
jstl-impl-1.2.jar and jstl-api-1.2.jar

how to resolve it please help
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3054
    
  33

I would suggest you to start a new thread for any new query so that others can also have a look at the query.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3054
    
  33

Bhardwaj Shweta wrote:
and servlet is working fine
but in my jsp page its showing value of String s6 = rs.getString("priority") in all the fields..
error is in my logic

You are using the same variable for all the values in the list and consequently the variable will end up having the value of the last field. So you need not use a variable instead just do an get on the list.
Bhardwaj Shweta
Ranch Hand

Joined: Mar 29, 2011
Posts: 36

@ Mohamed
Thanks for replying
umm can you please give some example.

if i have <input type="text" name="pname" id="pname" size="25" value="<%=##%>" disabled>
<input type="text" name="remarks" size="25" value="<%=##%>" disabled>
how should i retrieve respective value from list object??
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3775

As I said encapsulate all the fields for a given record in an object (add it to the list) and access that object in the JSP. This way you can have many records retrieved easily in the JSP. But with this code, retrieve the data to different variables(since this is only for one record you are using) for each field and use them in the HTML elements.

Since you are learning the JSTL etc... you can re-factor the code to use them once you get this to work.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3054
    
  33

With the current implementation- You can declare different variables like mystring for each column in the resultSet and then use them alongside the html code. I cant think of other way with the current implementation. The one i suggested using the list.get might not work
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3054
    
  33

Vijitha Kumara wrote:As I said encapsulate all the fields for a given record in an object (add it to the list) and access that object in the JSP. This way you can have many records retrieved easily in the JSP. But with this code, retrieve the data to different variables(since this is only for one record you are using) for each field and use them in the HTML elements.

This is a better approach and the code thus written is more readable. And I remember suggesting this.
Bhardwaj Shweta
Ranch Hand

Joined: Mar 29, 2011
Posts: 36

Thank you guys for helping me out, i will try this
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Storing resultset into arraylist
 
Similar Threads
JSP Not Reading Arraylist
Online Shopping
unsafe operation??
Skipping element addition in list
request attribute set in the jsp cannot be retrieved in the servlet