Is it possible to store an Arraylist in a Java database?

Asked

Viewed 3,170 times

3

In the case of Banco de Dados using Java, it is possible to store an object ArrayList in a Banco de Dados MySql? If yes, how can I store this type of data? And how can I recover it from the Data Base? If possible, they could show a simple example?

  • You want to store the data contained in Arraylist?

  • 1

    I want to store an Arraylist object as a whole within the bank. Example, I have ArrayList myArray = [2,43,1,4,11,78,43]. I wish I could store the object myArray. And later, you can perform a select and rescue the object myArray in full ([2,43,1,4,11,78,43]). The storage of myArray should be in a single tuple, not in 7 tuples

2 answers

9


The question is posed inappropriately. You can understand that you have a list of values and want to save this in a Mysql database, but these are two completely different worlds and you need to understand how they relate.

The correct answer depends on several factors.

Data model

The database does not know anything about Java and does not need to know. So, first, you must define a data model capable of storing the values.

The most "correct" model would be to store the values each in a record, perhaps in a table with a relationship 1:N (one to many) with a main table, in case the list is linked to another record.

By your comment, you want to store the values in a single field. While this is usually more "comfortable" from the programmer’s point of view, it can generate several side effects. The simplest case is if you need to search records looking for values from that list, because in that case you can’t just use a clause WHERE, but you’ll need to go through all the records.

If this is not a problem, your model will probably contain a field VARCHAR to store the list in text format. In this case, the question could be simply about how to convert a list to text and then again to a list.

Storing a list in a text field

If the idea is just to store whole numbers and you are using Java 8, you can convert a list to a String easily, like this:

List<Integer> lista = Arrays.asList(1, 2, 3);
String texto = lista.stream()
        .map(String::valueOf)
        .collect(Collectors.joining(","));

The code basically takes the list of integers, converts it to a list of strings and joins it all together by comma.

Then, to take the text and rebuild the list, you can do so:

List<Integer> novaLista = Arrays.stream(texto.split(","))
        .map(Integer::valueOf)
        .collect(Collectors.toList());

Code breaks text by separating by commas, converts each item to one Integer and finally retrieves the result from a list.

You need to be careful with the size of the field, not to exceed the number of characters.

In addition, this approach is not safe for other types of data that may contain commas in the values.

Storing the list using serialization

Another approach would be to serialize the ArrayList using the Java serialization API, storing the object as a byte sequence, which could be later deserialized.

It’s just an idea and there are other answers here in the O.R..

This approach is flexible because it allows you to store any type of list, as long as the objects inside it are also serializable.

The problem is that a sequence of bytes in the database is not a very pleasant thing to deal with.

Storing the list using JSON

Another more flexible alternative is to convert the ArrayList in a JSON object and store it in the database. JSON is a text object representation format, for example:

{ itens = [ 1, 2, 3 ] }

This approach is the most flexible because it allows any type of data to be stored.

The downside is you have to use a more complex API to convert ArrayList and JSON. There are several responses here in the OS that teach how to generate JSON or vice versa.

0

If you are working with JPA, I believe that the oracle documentation address the issue quite clearly.

In the link above, it is exemplified in a very simple way how the annotation that makes the "heavy work will be manipulated".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.