Search code examples
javadatabasecsvpojoopencsv

Database or CSV to java object


For now I have a CSV with several columns in rows. Eventually, I will have a SQL relational database structure. I was wondering if there are any libraries to easily extract this data into a list of java objects.

Example:

title | location | date
EventA | los angeles, ca | 05-29-2014
EventB | New York, NY | 08-23-2013

This is the structure of the data in csv. I would have a java object called Event:

Event(String title, String location, String Date)

I am aware of openCSV. Is that would I need to use for csv? If that is the case, what is the different solution for a SQL relational database?

Also, does can reading a csv only be done in the main method?


Solution

  • For when you convert to the SQL database, you can use Apache's dbutils for a low-level solution, or Hibernate for a high-level solution.

    dbutils You can implement a ResultSetHandler to convert a result set into an object or if its a POJO the framework can convert it for you. There are examples on the apache site. http://commons.apache.org/proper/commons-dbutils/

    Hibernate There are plenty of tutorials out there for working with Hibernate. http://www.hibernate.org/