+ 6

Storing data in Java

hey everybody, what do you think is the best way to store data in java? I'm about to program something like a crm for a friend of mine. I could save the data in an xml file, but I want something more robust like a database, but I don't want to install additional software on the user's computer to accomplish this. any ideas?

30th Mar 2017, 5:05 PM
Mario L.
Mario L. - avatar
4 Answers
+ 14
How about Java DB? It's included in the jdk.
30th Mar 2017, 5:35 PM
Tashi N
Tashi N - avatar
+ 19
It depends on how much data you have. When you want to store a handfull of key/value pairs like some user preferences, you could use the Preferences class, which is implemented using the registry on Windows, config files on Linux and whatever else is the preferred way to store user preferences on other plattforms. When you have actual data which is too much for the registry but not enough to warrant a database (something in the order of a few MB), using one or more flat files might be a solution. When the data is complex, it might be a good idea to use a standardized format like XML instead of something homebrewed. Java has classes which allow easy parsing and serialization of XML. An alternative quick&dirty solution would be to use ObjectStreams to save and restore whole objects. This is very easy to implement, but might not be very efficient because it stores a lot of meta-information which is likely unnecessary. But when you have a lot of data (more than you are comfortable to read and write completely), it might be a smart move to use a database. A database allows you easy access to huge amounts of data (in the orders of several GB) and offers you a lot of features for free which would be hard to implement yourself (like searching for records using indices). Databases aren't magic. They also use files to store their data (files with very clever structure, however). You could replicate every feature of a database yourself. But why should you reinvent the wheel when you can just use an existing solution?
30th Mar 2017, 5:30 PM
Nitin Dixit
Nitin Dixit - avatar
+ 13
@Mario I have a great suggestion for this: http://stackoverflow.com/questions/8751273/how-to-store-data-in-java-database Most of the people recommend sqlite.
30th Mar 2017, 5:29 PM
Dev
Dev - avatar
+ 6
@Dayve & Tashi: thank you. I'd like to avoid SQLite if possible, but java db sounds great, I'll take a look at it. @Nitin: I don't know how many customers my friend has, so I don't know how much data to be stored. the user preferences is definitely not appropriate. and as I explained in my question, xml is out of question. I don't want the data to be manipulated by other programs that easy. that's the reason why I want to avoid xml, csv or txt files.
30th Mar 2017, 6:13 PM
Mario L.
Mario L. - avatar