Skip to content
Advertisement

What type of data structure should I use to hold table rows?

I’m new to Java and just getting into querying databases. So far I have my results in a ResultSetMetaData. I’m think that for each row in the dataset I should add it to some form of collection? Can anyone tell me the best practice for this?

Thanks,
Jonesy

Advertisement

Answer

Usually we have a class with fields that correspond to a table. Then, whenever we have a (full) row in a result set, we create an instance of this class.

Example:

Consider a table created like this:

JavaScript

A model class would be like this:

JavaScript

Now if you read data and have a ResultSet, you would create a new customer object and set the fields:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement