Almost every application will need some way to persistently store data. There are many different ways to accomplish that. We’ll start with a simple way to save a small amount of data and continue to more advanced techniques.
Some of the techniques (Preferences, XML and reading/writing Files) described here were already used in the JavaFX 2 Tutorial Part 5.
Preferences
With the class Preferences
of the standard Java library we can save some small values. Typically, you’d save some simple user preferences in it like path to the last opened file.
Depending on the operating system, the Preferences
are saved in different places (e.g. the registry file in Windows).
Preferences
example in JavaFX 2 Tutorial Part 5.- Instructions in German in the book Java ist auch eine Insel
Saving Files
There are various possibilities to load and save a file in Java.
- An example is the
FileUtil
helper class in JavaFX 2 Tutorial Part 5. - The official Java File I/O Tutorial.
Simple text files can directly be saved to the disk. You construct a String (from objects) and save it using the means described above. But if you later want to read from the text files and use the information in your application, you might have some difficulties. You would have to manually parse the elements from one big unstructured String. Then, to get objects again, you would have to create new objects and fill them with the information from the String.
That’s why it helps to save data in some structured form like XML.
XML with XStream
XStream is a simple library to serialize objects to XML and back again. Serializing means that, given an object (which may contain a reference to other objects), XStream will create a single XML-String representing this object(s). The XML-String can then be saved to a file. XStream will later accept this XML-String to reproduce the object(s) again.
- XML example in JavaFX 2 Tutorial Part 5.
- Official XStream Website with a good Two Minute Tutorial.
When data gets more complex then a database instead of XML is appropriate.
Java Database (also called Derby)
Derby is an Open Source Database completely written in Java. The advantage of a pure Java database is that it can be embedded with the Java program (as opposed to being run as a separate, standalone server). Derby is based on the SQL Standard.
- Official Apache Derby Website containing downloads, manuals, etc.
- Derby Tutorial with an embedded example. Note: You will find the example
SimpleApp
inside the Derby zip underdemo/programs/simple/
.
Eclipse Data Tools Platform
With the Eclipse Data Tools Platform plugin you can manage your database. This is very practical, especially to view the data that has been saved to the database.
Database with a Mapper Tool and MySQL (Advanced)
Tools like Hibernate and EclipseLink help to fill the gap between the relational data in the database and objects (see Object-relational impedance mismatch).
- Hibernate Tutorial
- Other Hibernate Tutorials with more information.
- An extensive Hibernate video tutorial series
- EclipseLink Tutorial - also using Derby Embedded Database (could be replaced by MySQL)