This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JDBC and the fly likes Which is the best way to store the values of an ArrayList into a database? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Which is the best way to store the values of an ArrayList into a database?" Watch "Which is the best way to store the values of an ArrayList into a database?" New topic
Author

Which is the best way to store the values of an ArrayList into a database?

George Goubak
Greenhorn

Joined: Jun 14, 2009
Posts: 14
I have a set of objects that hold information about articles, for example the article's title, its author and its subject(s). The subjects are String values stored in an ArrayList. The number of subjects is different for almost every article. I want to store the values of those objects in a database (currently i use MySQL) so i can search in them. After a search, the returned values are going to be recreated into an object so i can use it the way i want.

So, i thought something like this:

Articles table
----------------
- Title
- Author
- Subject

In the subject column, if there are more than one subjects i will generate the string that will be stored, using comma separation like "Java, Databases, Programming" so upon retrieval i will generate again the ArrayList. Do you think this is the correct solution? Do you have any other suggestions?

Thank you

PS: This is just a small example of the actual table so don't mind about primary keys, IDs, etc.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30598
    
154

George Goubak wrote: i will generate the string that will be stored, using comma separation like "Java, Databases, Programming" so upon retrieval i will generate again the ArrayList. Do you think this is the correct solution? Do you have any other suggestions?

I wouldn't do this as it would prevent you from searching by subject.

Rather I would create another table called subject_article_relationship (or the like.) It would just contain an article_id and subject. That way you can represent multiple subjects in tabular form.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
George Goubak
Greenhorn

Joined: Jun 14, 2009
Posts: 14
Jeanne Boyarsky wrote:I wouldn't do this as it would prevent you from searching by subject.

Rather I would create another table called subject_article_relationship (or the like.) It would just contain an article_id and subject. That way you can represent multiple subjects in tabular form.


I was going to search by subject using the "LIKE" operator but your way is better so i'm gonna go with it.

Thank you very much!
 
GeeCON Prague 2014
 
subject: Which is the best way to store the values of an ArrayList into a database?