Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Storing objects un an ArrayList

 
Joe Brigs
Ranch Hand
Posts: 70
IBM DB2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ResultSet xxx_AppCode = statement.executeQuery(
"SELECT code, DESC FROM APPL_Table");
ArrayList<Object> applCodes=new ArrayList<Object>();

while (xxx_AppCode.next()) {
// create the Application Code Object
ApplicationCode applicationCode = new ApplicationCode();
applicationCode.setApplCD(xxx_AppCode.getString("code "));
applicationCode.setAPPLDESCTX(xxx_AppCode.getString("DESC "));

applCodes.add(applicationCode); }

What I'm trying to do above is read a DB table create an object for each row it brings back (2 cols each row) so I can store each row as an object in an ArrayList, Not sure if the above is the correct way ? If it is how do I read it back, any examples , commenst etc. are appreaciated. Thank You
 
Rob Camick
Ranch Hand
Posts: 2580
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PLease edit your question, repaste the code so it contains formatting and then select the code and use the Code button so the code will be nicely formatted in your question.

Not sure if the above is the correct way ?


Looks reasonable

comments etc. are appreaciated.


Use generics on the Arraylist to store a specific object type, not just any object:


how do I read it back




 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15276
38
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better yet, program to an interface, not an implementation:
 
Joe Brigs
Ranch Hand
Posts: 70
IBM DB2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all who replied your replies worked perfect ! But I left a few important points out.

I start out in Class A1 and create an object by calling Class APPL which does the read of the table and creates the Arraylist storing the object. then it returns the Arraylist object to Class A1. I store it in an Attribute to pass it to a JSP . I then do a foreach to display the values in the object. This is the part I can't get to work now, but I'm close. Becuase it displays the memory addresss of each item in the array. I only want to display the DESC (2nd value ) in each ArrayList Occurrence. I will use both(code & desc) later on though. Am I on the right path , please show an example. Thanks Again

Class A1



Class APPL


Class A1

//store the need data for the session



JSP


 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you'll want to implement a "public String toString()" method in your ApplicationCode which returns a suitable string for displaying in that position.

(By the way I edited your last post to use the "Code" tags. See that button which says "Code" above the box where you're posting?)

And in future, if you're going to design classes which can be used in JSTL and EL expressions like (which is definitely a good thing) then you should be designing them as Java beans. That puts certain requirements on the names of getter methods, for example, so that they can be used as properties in EL.

For example if your ApplicationCode class had a "public String getDescription()" method which returned a description for the object, then your EL expression to produce the description of an object which is referred to by a EL variable "XXX" would be like this:

However the methods I saw in your post don't conform to the Java Bean specification, which is why I suggested overriding toString() as a stopgap.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic