Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass 6 elements of arraylist to prepared stmt

 
divya kundapur
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you struck with extracting 6 elements or passing only 6 elements to PreparedStatement?

 
divya kundapur
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats great. subList() is a really a great feature
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic