Say, I have a pretty simple Java application that needs the way to store some user settings. XML is not a really good solution, since I want to store them in binary form. So, what would be the best solution in this case, embedded database (such as Apache Derby) or just plain old serialization?
I know that these are two completely different things, but both allow to persist some application state. So what would you chose, and why?
Edit
As far as storing simple user preferences go, .properties or xml files are fine, I agree with you. But what if I want to store passwords, or some application-specific data?
User settings are typically stored as
properties, using the properties file format
properties using the XML format
preferences, using the Preferences API. This has the advantage of storing and reading user and system preferences for you, without having to think about where to store them, etc. See http://download.oracle.com/javase/1.4.2/docs/guide/lang/preferences.html
As Apache Derby is an embeddable relational database, it makes sense to use it for storing and manipulating relational data. Using an embedded db for persisting a few user settings only is a bit overkill.
If it were me, I would use a simple key/value pair serialization for persisting user settings.
Related
I'm currently getting into Socket Programming and building a multi-threaded console application where I need to register/login users. The data needs to be saved locally, but I can not seem find the right structure for it.
Here are the ideas I though about:
Simply saving the data to .txt file. (will be troublesome to search and authenticate the logins)
Using the Java Preferences API but since the application is multi-threaded I keep on overwriting the data each time a new client connects to my server. Can I create a new node for each new user?
What do you guys think is the ideal structure for saving login credentials? (security isn't currently a concern for this application)
I would consider the H2 database engine.
quote:"Very fast, open source, JDBC API Embedded and server modes; in-memory
databases Browser based Console application Small footprint: around 2
MB jar file size"
http://www.h2database.com
It really depends on what you want to do with the application. The result would be different, depending on what you would answer to the following questions:
Do you want/need to persist the databases?
Is there any other data which you need to store along with that?
are you using plain java or a framework like Spring?
Some options:
if you're just prototyping and you don't have any persistence: consider using an in-memory storage for it. For simplicity in coding/dependencies, something like a ConcurrentMap can be completely sufficient. If you wrap it properly, you can exchange it later - and you don't add dependencies and complexities at an early state.
If you're prototyping but you still need persistence, using properties files on top of the ConcurrentMaps can give you a quick win.
There might be some more stages to this, depending on where you want to go with this, choosing a database at one point can be an option. Depending on your experience and needs, you can use a SQL or NoSQL database. Personally, I get faster results with NoSQL (MongoDB in my case) but prefer SQL in production for use cases like account management.
I am making a school management project and I want to add a setting panel in my project, but unable to determine how to store the settings for a project in java?
how can i make it possible?
Easiest way would be with the Preferences API. You can save and load things without worrying how they're stored.
You can use many different things to store persistent data. Persistent means data will be stored after your app stopped running.
XML file:
JAXB
A database like: JAVA SQL
JSON file: JSON API
Java Properties: Properties Docs
However I recommend you JAXB, because I think it is the easiest way.
I have a program where I need to save a running application to be able to go back to it later
I know that I can write/read from a text file to achieve this but the program is pretty prodigious so it's not really a good way to do it because I have 10+ classes and thousands of JTextFields, JComboBoxs, etc. Does anyone know of a way I can achieve this without writing/reading from text files?
An example of what I need to be able to do is this:
In Microsoft Excel you can load files (.exl) into it and be able to edit them.
The Swing Application Framework provides a way to save session state when your application exits and restore the state when you restart. Session state is the graphical window configuration of your application. This state includes window size, internal frame locations, selected tabs, column widths, and other graphical properties.
How do you think Excel does this? It stores the type and value of each cell, along with metadata describing the worksheet in its own proprietary binary format in a file. If you have a custom application with complex internal state, you will have to design a storage format and serialize the state yourself. You may be able to use Java Serialization, but not without some effort.
A good way to do this is to save the data from your controls into a canonical form and then make that class serializable. You can then persist that data to a file. Here's a link about serialization in Java.
UPDATE
I just noticed that you said you have thousands of form controls. So you probably don't want to do all of this in one class, but you probably want to maintain a hierarchy of classes and split out the data into separate classes. This will also help you separate your concerns. Hopefully you have POJOs or domain classes that represents your data. If that is the case, your task will be much easier. This is also why separating concerns is good :).
To save the state of an application, i can think of two popular way:
1) save the state of the application in a Database
2) save the state of the application in a binary file or XML,json or any format you want.
Maybe giving more details about the app. would help.
is it a Web app, fat client app, client/server app... !^
Solution may vary with the type of application.
Hope it help.
I'm currently working on a simple Java application that calculates and graphs the different types of profit for a company. A company can have many branches, and each branch can have many years, and each year can have up to 12 months.
The hierarchy looks as follows:
-company
+branch
-branch
+year
-year
+month
-month
My intention was to have the data storage as simple as possible for the user. The structure I had in mind was an XML file that stored everything to do with a single company. Either as a single XML file or have multiple XML files that are linked together with unique IDs.
Both of these options would also allow the user to easily transport the data, as apposed to using a database.
The problem with a database that is stopping me right now, is that the user would have to setup a database by him/herself which would be very difficult for them if they aren't the technical type.
What do you think I should go for XML file, database, or something else?
It will be more complicated to use XML, XML is more of an interchange format, not a substitute for a DB.
You can use an embeddedable database such as H2 or Apache Derby / JavaDB, in this case the user won't have to set up a database. The data will be stored only locally though, so if this is ok for your application, you can consider it.
I would defintely go for the DB:
you have relational data, a thing DBs are very good at
you can query your data in that relational much easier than in XML
the CRUD operations (create, read, update, delete) are much more easier in DB than in XML
You can avoid the need for the user to install a DB engine by embedding SQLite with your app for example.
If it's a single-user application and the amount of data is unlikely to exceed a couple of megabytes, then using an XML file for the persistent storage might well make sense in that it reduces the complexity of the package and its installation process. But you're limiting the scalability: is that wise?
I am developing a medium Java desktop application, without using a database.
I am using xml, serializable objects, etc. to store the user/application data, but what is the right location to save these files to (system-independent)?
If you really don't want to store them in a data base take a look at the Preferences API it is platform neutral.
Why aren't you using a database? Use file-system database (like hsqldb) and an object relational mapping layer. You probably won't even need to write a mapping file of any kind, or arbitrarily make your classes serializable.
Store data in %APPDATA%/appName on windows, and probably ~/.appName on linux.
User/Library/Application/appName could work on macs.
Most, if not all, OSes have a concept of a home directory where you end up having a lot of hidden configuration directories of one form or another. You could create a hidden directory under the users home directory and store your configuration/data files there.