Win a copy of Head First Go this week in the Go forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Insert List<String[]> into database  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The current format of my list is:




whereby each separated value by space is a row in my database table. I want to insert the 5 separate values per loop. I want to insert all these lines into my database but am not sure on exactly how. This is also a working connection to my database as of now.

This is my rough idea:




I am using Java and not android.
Any help is amazing.

Many thanks
 
Sheriff
Posts: 5750
149
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you're asking.  Do you want to know how to split the rows into their separate parts?  If so, look up String.split().
 
Marshal
Posts: 24199
54
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 5 to 7 of your posted code are Java translations of this pseudo-code:

For each array of String in the List:
 do something



You need to decide what is that "something" which you want done, and then write Java code for that.

So what exactly do you want to do with an array of String objects?

Hint: That doesn't mean "What Java code do you want to write?" It means "Explain in English what you want done."

Hint: What would you do if you only had one array of Strings?
 
luke davis
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to place them into an insert statement but i imagine the easiest way to do this would be to assign them to variables.



i want as


and then to somehow insert these variables into my database (with a prepared statement?). Because im using an arrayList i find it very confusing. I think i have to split each line up and then assign through a loop would you not think?
 
Saloon Keeper
Posts: 5289
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think i have to split each line up and then assign through a loop would you not think?


Maybe you missed Knute's post where he suggested how to go about splitting up the line? Then you'd need an insert statement for the DB. And then add code to do all that in a loop.
 
luke davis
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would this be the correct way to split the List up by line?

 
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the correct approach would be to create a class that will encapsulate all the data from that String, then you won't need to mess about with String manipulations.
 
Tim Moores
Saloon Keeper
Posts: 5289
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, we don't know where the data is coming from. If the data is coming to the app in the format as shown here, then it needs to be tackled via string operations.
 
Knute Snortum
Sheriff
Posts: 5750
149
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

luke davis wrote:Would this be the correct way to split the List up by line?...


Looks good.  Did you try it?  What happened?

You don't even need to write st.split(" ") as the default for split() is to split on whitespace (space, tab EOL), so st.split() is fine.

The strikeout text above is incorrect. Ignore it.
 
Campbell Ritchie
Marshal
Posts: 63496
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:. . . If the data is coming to the app in the format as shown here, then it needs to be tackled via string operations.

Good point.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!