Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
George Goubak
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34422
347
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
George Goubak
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic