wood burning stoves 2.0*
The moose likes Java in General and the fly likes ArrayList seems to be not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList seems to be not working" Watch "ArrayList seems to be not working" New topic
Author

ArrayList seems to be not working

Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Hi friends,
I tried to save the resultset values into an ArrayList and then pass it to my JSP page. For which i employ the below code but it doesn't seems to be quite well. Here is my code


The error that it shows is:

I also change the code from rs.getString(vendor_name) to rs.getString("vendor_name")..but still shows an error. I need help to get rid of this problem..


Never try to be a hard-worker. Be a smart-worker.
My Blog
Sid Murrey
Ranch Hand

Joined: Jul 07, 2008
Posts: 58
Check out this line:

String query = "select * from tbl_vendor where vendor_id" + vendorid ;

I think there is something missing, an equal sign and quotes?
What about:

String query = "select * from tbl_vendor where vendor_id='" + vendorid + "'";

And be sure to write some code for the catch block, I hope you just removed it to shorten your code for this post :-)

[ July 10, 2008: Message edited by: Cedric Meury ]
[ July 10, 2008: Message edited by: Cedric Meury ]
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Yes ofcourse the "=" symbol is missing..but after i correct that too the error appears.. The error is an cannot find symbol error and i want to know that the rs.getString(vname) and rs.getString("vname") is equal?.If so then what may be the cause of the issue..
Sid Murrey
Ranch Hand

Joined: Jul 07, 2008
Posts: 58
Of course they are not equal. vname is a variable that you have not defined previously. The getString() method of the ResultSet interface takes a String as an argument. You have to write "vname", given that vname is a valid column in the table you are accessing.

Although I am not a SQL expert, I'd guess that you definitely have to add the "=", but you might also have to add single quotes ( ' ) around the vendorid that you append to the SQL query. You might have missed the tiny single quotes from my original reply.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Rajkumar balakrishnan:
i want to know that the rs.getString(vname) and rs.getString("vname") is equal?.If so then what may be the cause of the issue..


You seem to have got the clue. If you have it as 'rs.getString(vname)' that means there should be a variable declared with the same name 'vname' and that is what the JDBC would be trying to match with column name in the resultset.

The otherway, you have hardcoded the column name to fetch its value as 'rs.getString("vname"), which should not give any errors and should be working fine!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Could you please give me sample code for passing resultset values to the ArrayList . It could be helpful for me.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Rajkumar balakrishnan:
Could you please give me sample code for passing resultset values to the ArrayList . It could be helpful for me.


It is as simple as adding a value into ArrayList. But the values what you add would be fetched from the resultset.

Example,


ArrayList myList = new ArrayList();

//declare resultset and initialize it with the call to execute()

myList.add(resultSet.getString("colName1"));

//likewise you can append values to the ArrayList

[ July 10, 2008: Message edited by: Raghavan Muthu ]
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

I do it it myself and thanks for your reply.. But i got a problem with the retrieval phase of the values in my JSP page. I use a useBean method to import my class and call the ListIterator to iterate the values.Is this the right way to retrieve values? IF not then tell me the steps involved in retrieval of data from the ArrayList in my JSP page.. It helps me a lot
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Rajkumar balakrishnan:
I use a useBean method to import my class and call the ListIterator to iterate the values.Is this the right way to retrieve values? IF not then tell me the steps involved in retrieval of data from the ArrayList in my JSP page.. It helps me a lot


If I am right, useBean is not the way to import your class and it should definitely be done through the import attribute of page directive, even for the useBean

Why do you want to use a ListIterator? If you want to traverse the list from only one direction, you can rather use the plain Iterator!
Sid Murrey
Ranch Hand

Joined: Jul 07, 2008
Posts: 58
I would encapsulate your vendor data in a separate class ("Vendor") and set up an ArrayList<Vendor> arrayList (assuming you are using >1.5). Then you just create a new Vendor object using the values retrieved from the database. Your bean class can then return the iterator via a method like

Iterator<Vendor> getVendorIterator() {
return vendorList.iterator();
}

or the list itself via

List<Vendor> getVendorList() {
return vendorList;
}

For information on how to traverse lists in JSP, check out
http://www-128.ibm.com/developerworks/library/j-jstl0318/
[ July 10, 2008: Message edited by: Cedric Meury ]
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

I'll do accordingly to your suggestion but i stuck somewhere else that i don't know. Here is my code and please validate it for me.



I think i mess up the code.Please help me regards this
Sid Murrey
Ranch Hand

Joined: Jul 07, 2008
Posts: 58
I recommend using the standard tag library (JSTL) for viewing lists in JSP pages, it makes the code much simpler. I cannot spot an obvious error in your code, but did not spend too much time looking. Can you provide some more information, maybe an error message?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Rajkumar balakrishnan:

I think i mess up the code.Please help me regards this


As such, it looks okay. What exactly you think you are stuck at?
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Actually the problem is it doesn't seems any error message. I use AJAX to call the servlet by passing one parameter and in my servlet fil, i use the param to execute and store resultset from DB. After then i pass to List object which seems to be empty always.
Sid Murrey
Ranch Hand

Joined: Jul 07, 2008
Posts: 58
Originally posted by Rajkumar balakrishnan:
Actually the problem is it doesn't seems any error message. I use AJAX to call the servlet by passing one parameter and in my servlet fil, i use the param to execute and store resultset from DB. After then i pass to List object which seems to be empty always.


Are you sure you are referring to the same instance of the bean using AJAX and JSP? It seems like the call to the Servlet will create a new instance of it and the session-scoped bean used in your JSP page keeps another instance.

But this is getting over my head slightly, maybe someone else can jump in? In any case, it's a bit a hard to decipher what you are actually asking. I think you would profit from better answers by putting a bit more effort into grammar and clearer description of the problems you are experiencing.

Cheers,
Ced
[ July 10, 2008: Message edited by: Cedric Meury ]
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Here for you Cedric Meury..
I use tha AJAX function to pass the value from the combobox to servlet file and in my servlet file i put the values(that i fetched from DB) in a session like session.setAttribute("arraylist",al). And i get it in the JSP page like ArrayList al=(ArrayList)session.getAttribute("arraylist").... But the al seems to be empty..when i try to print those values using an Iterator.
Sid Murrey
Ranch Hand

Joined: Jul 07, 2008
Posts: 58
Originally posted by Rajkumar balakrishnan:
Here for you Cedric Meury..
I use tha AJAX function to pass the value from the combobox to servlet file and in my servlet file i put the values(that i fetched from DB) in a session like session.setAttribute("arraylist",al). And i get it in the JSP page like ArrayList al=(ArrayList)session.getAttribute("arraylist").... But the al seems to be empty..when i try to print those values using an Iterator.


Well, did you try to debug? Does the actual fetching of the data work (print out the values after fetching)? Is the array really empty or does your JSP code not work (just print out the content of the ArrayList directly to HTML without any other tags)? How does the generated HTML look?
etc. etc.
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445



I use the above code in myt JSP page to display all the items in the ArrayList .Is this the right way to do so. If not then suggest me one.
Sid Murrey
Ranch Hand

Joined: Jul 07, 2008
Posts: 58
Originally posted by Rajkumar balakrishnan:


I use the above code in myt JSP page to display all the items in the ArrayList .Is this the right way to do so. If not then suggest me one.


Sure, looks alright.
I don't want to be rude and I am also new here, but please check out http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

What you found wrong with my question.?
Sid Murrey
Ranch Hand

Joined: Jul 07, 2008
Posts: 58
Originally posted by Rajkumar balakrishnan:
What you found wrong with my question.?


I don't really know what the problem is? You have just asked whether this is "the right way".
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

I ask you to give me the right way to accomplish my work. Is this a crime?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Rajkumar balakrishnan:
I ask you to give me the right way to accomplish my work. Is this a crime?


Rajkumar, I also agree with Cedric Meury. If you look at my earlier reply, i also asked the same and your question does not seem to be clear!

Kindly have some time to Ease Up and Thank the other ranchers who take their time to help you out!

You can be a bit polite I feel!
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Kindly have some time to Ease Up and Thank the other ranchers who take their time to help you out!

You can be a bit polite I feel!


Yes,i thank all the ranchers for their time..And ofcourse my query is When i try to print the values that i had extracted from ArrayList, it displayed as null. I extract it like this


These things were really looking messy?. If so then tell me how to get values from ArrayList. In List we use ListIterator to extract. But in ArrayList? I need your help and i always thankful for all ranchers for their kind help....
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

That's great!

Are you sure that the arraylist does have non-null values? If not, can you check the arraylist itself not null? Have a debugging statement (logger or System.out.println()) to ensure the same.

The get(int) method is there in List interface and ArrayList being a child also inherits it. You don't need to use an Iterator at all if the values you gonna fetch from a List is only a few. Otherwise, you can obtain an iterator by calling the iterator() method in List interface and you can navigate the list by using next() method on the iterator.In your case, what you have done is also right as you are retrieving only 3 values.
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Thanks Raghavan,
i am on my way of doing some research on ArrayList and i'll catch you all after do finish it..I think i can able to solve it with your help that you provide here.. I'll do post my queries after i finish my research.. Thanks for all..
Rodrigo Tomita
Ranch Hand

Joined: Apr 28, 2008
Posts: 70
Here goes my 2 cents: I would also put some System.out.println() statements on your servlet side, so you can be sure you have contents in your ArrayList.

Also, you can check if you did session.setAttribute("vendorTable"); in your servlet (note that vendorTable is case sensitive).

Finally, there is a Servlets forum in the Saloon... you will probably get a different audience there.
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Finally i ripped of my problem and fix it with your help...
Thanks for all ranchers
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

That's great Rajkumar!! Congratulations

At times posting the way you achieved will also be of little more help to the people with the similar query who land up here
Sid Murrey
Ranch Hand

Joined: Jul 07, 2008
Posts: 58
Congrats Rajkumar. Great to see that you reacted to the criticism. Good luck!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ArrayList seems to be not working
 
Similar Threads
POI File Not Compiling Problem
Wrapper cannot find servlet class
CActus problem
Amnesia for my Servlet file...cannot find symbol?
Can AJAX helps me to do that