0

I'm new using mySQL database. I have a class in Java that as an variable that is an array of strings.

I would like to save that array of strings on my database. How can i represent my array of string in the mysql database?

thank you

2
  • Option 1) Serialize and store. 2) id | arrayname + id | array_id | string. Commented Jun 26, 2014 at 22:54
  • If you want to be able to match individual array elements, best is to put each element in a different row of a table. Commented Jun 26, 2014 at 22:55

1 Answer 1

1

I can think of some ways:

  1. Serialize the object. How does that work? First you need a table that can hold the class name and the object serialized. You need the class name for when you need to read the object from the database.

    Sample code can be found here:

    http://javapapers.com/core-java/serialize-de-serialize-java-object-from-database/,

    and

    http://www.java2s.com/Code/Java/Database-SQL-JDBC/HowtoserializedeserializeaJavaobjecttotheMySQLdatabase.htm

  2. Sometimes all you need to serialize is not the object, but rather, the attributes of the object. In this case, you can use XML or JSON (for example). In this case, create a String representation of your object and save it to the database as a text field.

  3. You can map your object to one or more tables using any of the many ORM solutions, such as iBatis, Hibernate, etc. http://java.dzone.com/articles/getting-started-ibatis-mybatis http://hibernate.org/orm/

  4. If what you need is to save some attributes of your object to the database then you can either use JDBC and just update the field, or use an ORM and create a POJO with only the attributes you'd like to save.

Sign up to request clarification or add additional context in comments.

3 Comments

Ty.. Seems very complexo :-( I already have a table for the class and already have the others atributes represented.. But they are int variables and doubles.. I Would like Something Light and easy to and one More variable that is an array of strings with fixed size of 12 :-(
Regarding 4,my problem is that i dont really know how to represent an array of strings in my database. Should i make a New table named mystring, and the other table will have a relation of 1 to many mystring, and that way I Can represent an array of string that my object contains? Or this is a ugly way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.