This week's book giveaway is in the Other Languages forum.We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Pass 6 elements of arraylist to prepared stmt

divya kundapur
Ranch Hand
Posts: 110
Hi,

I have an arraylist which for eg has 12 elements but i need to pass just 6 elements each time to prepared statement.. i m not able to figure out the logic.

any help

Raghavan Muthu
Ranch Hand
Posts: 3381
Are you struck with extracting 6 elements or passing only 6 elements to PreparedStatement?

divya kundapur
Ranch Hand
Posts: 110
Raghavan Muthu wrote:Are you struck with extracting 6 elements or passing only 6 elements to PreparedStatement?

i m struck with both... i have an arraylist for which i know i need to pass 6 elements each time to the preparedstatement and then execute the preparedstatement.. then pass another 6 elements repeat the process.

James Sabre
Ranch Hand
Posts: 781
Seems straight forwards to me! Loop through the array pulling out six values at a time and feed them to the prepared statement. What is the problem?

Campbell Ritchie
Sheriff
Posts: 50266
79
Pull out six values, and print them to screen. Then you have six values. Then you can feed them to the statement. Don't try to do everything all in one stage.

Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Looks straight forward- But has a catch because the process has to be continued until the elements of the list have been exhausted- One has to remember the last index, repeat the loop to complete all the elements of the list. You would have to first identify number of iterations so that you can completely traverse the list. And for each iteration you need to use the old index and then get the element from the list, increment the index.

Alternatively- You can create an ArrayList of ArrayList where each ArrayList will have an ArrayList of only 6(what ever number) of elements.

Raghavan Muthu
Ranch Hand
Posts: 3381
Mohamed Sanaulla wrote:Looks straight forward- But has a catch because the process has to be continued until the elements of the list have been exhausted- One has to remember the last index, repeat the loop to complete all the elements of the list. You would have to first identify number of iterations so that you can completely traverse the list. And for each iteration you need to use the old index and then get the element from the list, increment the index.

Alternatively- You can create an ArrayList of ArrayList where each ArrayList will have an ArrayList of only 6(what ever number) of elements.

Is this all required for doing the OP's problem? It is dependent on "what 6 values" he needs. Then he can set those values to PreparedStatement and get his work done. Isnt it?

Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Raghavan Muthu wrote:
Is this all required for doing the OP's problem? It is dependent on "what 6 values" he needs. Then he can set those values to PreparedStatement and get his work done. Isnt it?

With what information OP has provided- I concluded that the retrieval is sequential

Campbell Ritchie
Sheriff
Posts: 50266
79
• 1
I also thought he meant the first 6 elements. Of course, if you read the List interface documentation, you find there is a much simpler way to do it.

Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Campbell Ritchie wrote:I also thought he meant the first 6 elements. Of course, if you read the List interface documentation, you find there is a much simpler way to do it.

Yeah yeah,got it. Much simpler

divya kundapur
Ranch Hand
Posts: 110
Mohamed Sanaulla wrote:
Campbell Ritchie wrote:I also thought he meant the first 6 elements. Of course, if you read the List interface documentation, you find there is a much simpler way to do it.

Yeah yeah,got it. Much simpler

thanks a lot Mohamed Sanaulla ,i used the loop you provided and it worked .. just now checked the list interface as said by Campbell Ritchie n there is subList method ... thanks a lot both of you and to this great javaranch

Campbell Ritchie
Sheriff
Posts: 50266
79
I had forgotten about subList, until a few hours ago. It shows you need to go through the API and look which methods are available.

Raghavan Muthu
Ranch Hand
Posts: 3381
Thats great. subList() is a really a great feature

 It is sorta covered in the JavaRanch Style Guide.