This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
First of all, when your query has parameters, you should use PreparedStatement, use question marks in the SQL and then set the values for the parameters by setting them on the PreparedStatement, instead of concatenating strings as you are doing in line 3 of your code.
What is DBHandler?
In the code you have posted, you are looping over the rows in the ResultSet (lines 6 - 12), and for each row you create an Item object (line 11). However, you are not storing those Item objects anywhere. You just create them and then forget about them. Store the objects somewhere, for example in an ArrayList.
In line 20 you're looping over res but you probably wanted to loop over res2.
In line 22 you have a return statement. That will immediately return from the method, stopping the loop (lines 20 - 23). That's most likely not what you want. Also, baglist is an ArrayList<Integer>, while the method is supposed to return an Item. You'll get a compiler error because you're returning the wrong kind of object. Remove line 22.
Your method SearchItem is supposed to return one Item object. Why does it need to perform two queries?
You should have named the method searchItem instead of SearchItem. The common convention for naming Java methods is start with a lower-case letter.
Thank you Jesper . Thats such a good explanation. What i want to do get the bag sizes from database and store in a ArrayList and pass to Item Object. This is the format of Constructor of item class. item(String, String, String,String, Date, ArrayList ).
First 4 parameters collect from first SQL query and ArrayList comes from 2nd SQL query. As you can see i left it null as i dont understand how to combine these 2.
In short, i need to create ArrayList based on sql query and pass as a parameter to item object.
- Do the second query first, to build the ArrayList<Integer> baglist.
- Then do the first query, to get the values for the Item. Don't use a while (line 6), just use an if (since you only want one Item object). When you create the Item object, pass baglist as the last argument to the constructor.
Joined: Nov 25, 2012
Yea. It works now. Thnkx Lot
This is the final code.
But i came up with different problem in my Update method. All the text filed update without error. But There is Primary key Duplicate error at Jlist update code.
Jlist consists Bag Sizes for the Item. Since its a multi value attribute, i placed in a separate table with item code.
When i try to update item adding or removing bagsizes to Jlist it gives Primary key duplication error.
this is sample format how date stored in bagsizes tables. If you can please help to sort this out.
Code for Update Action and method at the controller class given below.