I write an application in Java. I get form a database a table (clients) which contains fields like:
name | surname | adress
What is the best solution to store this data in my app? Should I create an object for each client and store these objects in a list or set?
The table contains about 100 records and it’s already sorted.
Thanks in advance.
The most straightforward way is to create a class that contains fields that correspond to your database columns (name, surname, address). Then, as you already say yourself, create an instance of this class for each row in the table and store them in a
List.A
Setwill not be appropriate, since mostSetimplementations (for exampleHashSet) are not ordered, so if your data is sorted, theSetwon’t preserve the sort order. AListis ordered.To get the data out of the database, you could simply program it in JDBC yourself, which is straightforward in this case: open a connection to the database, execute an SQL query, loop through the rows in the
ResultSet, create an object for each row and fill it with the data that you get from theResultSet, store the created objects in aList.When your data model becomes more complicated than a single class, it might be worth it to use an object-relational mapping (ORM) framework. Java has a standard API for this, Java Persistence API (JPA). Hibernate is a popular implementation of this API.