This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, I am accessing a database from a stateless session bean through a DAO. I am accessing all the fields and storing the values in an object of a class I have made. The class has datamembers correspondng to the fields of the table and getter and setter methods. I am storing all the records as objects in an ArrayList object. This is the code of DAO Implementation class:
This is giving correct result when I see the output in the log file. That is all the records are added to the list. But when I access this list object from my client I find that all the elements of the list have the values of last record. This is the client code:
ArrayList result=new ArrayList(); result=myBean.giveResults(); System.out.println(result.isEmpty()); int k=result.size();
You are overwriting the same object each and everytime you add an item. You should initialize the itemDetails object each time in the loop, else it will point to the previously added itemDetails object and overwrite that data. Your code should be
Hello Ram, I did what you had told, but it doesn't help. I am still getting the same data for all the list contents. Also, This is the output of the log file, which is showing the output of System.out.prinltln that I have put in the same while loop where I am storing the data in the object fields. As you can see I get name of the item correctly when I display it from the list.
I even tried what you said but it doesn't help. What can be the reason? Thanks, Abhishek (IJ)
Ram is definitely right and if you made his changes your code should work. I actually intended to post a similar answer to your posting this passed weekend, but Ram was just seconds ahead me. Because it made no sense to have two identical answers I ultimately canceled my reply. However my point here is that if two different people make the same observation independently, then it must be true. There is one more hint I could suggest to you. Check your build again, make sure you�ve deployed the new version and from my experience I know that sometimes the application server could be the source of your errors. I remember WebLogic 6.1 gave me sometimes a hard time, because the new versions were not re-deployed correctly. The server maintained an internal cache and therefore I had to physically remove that folder from the disk and restart the server. You might like following this path as well. Regards.
I think, therefore I exist -- Rene Descartes
author & internet detective