• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Storing resultset into arraylist

 
Ranch Hand
Posts: 36
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bhardwaj Shweta
Ranch Hand
Posts: 36
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bhardwaj Shweta
Ranch Hand
Posts: 36
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 36
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in between i am learning how to use JSTL and EL.
 
Bhardwaj Shweta
Ranch Hand
Posts: 36
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 36
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 36
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys for helping me out, i will try this
 
Poop goes in a willow feeder. Wipe with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic