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.
Related
I have a java code which I'm currently running as a jar. This code checks for a specific file in the given directory which is currently hard coded in the code.
To give more flexibility and not to touch the code. I would like to have the folers list managed by a different file and the code reads this config file and gets the list of folders each time and execute it.
I would like to know which is the best possible option of maintaining the folder list outside the code so that anyone can update it. Can a properties be used for this ? can we dynamically take values from a property file
In Java you have the java.util.Properties that allow you to load flat key/value data from external resources.
If you need something that can be dynamically updated, there's also the more sophisticated Preferences library. This one allows you to:
Keep data organized in tree structures (it's a tree of nodes, each node storing its own key/value preferences).
Make use of basic types (primitive types, strings and binary data).
Make use of platform-dependent "native" stores transparently (under the hood, it's going to use the file system on Unix systems and registries on Windows by default).
Plug in your own backing store if needed.
Get any data changes performed within the application persisted transparently.
Register node/preference change listeners and react to any change if needed.
The API is quite old and hasn't been updated, but it doesn't mean it's deprecated. It is used mostly with GUI applications (notably, IntelliJ IDEA was storing its configuration using Preferences the last time I checked).
There's also an attempt to revive this library that I made with a project called cross-preferences by integrating modern distributed config stores (such as zookeeper, etcd or consul) as backing stores for java.util.prefs.Preferences and providing a web console for preference management.
suppose that I have an Employee Class and there is another Class Company that has more than one Employee so I want to save the Employee objects locally, that means every time I run my application I can retrieve these objects.
I would suggest using some kind of embedded solution. There are a number of options available such as H2 or Neo4j.
For a comprehensive list check out Wikipedia.
Although these solutions are technically databases the don't run on their own server, they run inside your current Java process.
To get started with H2 the following steps are required:
Add the h2*.jar to the classpath (H2 does not have any dependencies)
Use the JDBC driver class: org.h2.Driver
The database URL jdbc:h2:~/test opens the database test in your user home directory
A new database is automatically created
After that, you can simply use JDBC against the new database everything is stored on disk.
I would not recommend using Java-serialization of Java objects to a file (Serializable). This is not a maintanable solution and whenever your code changes (for the classes that were serialized) you have to work out some plan on how to migrate your data.
Another solution is to dump your object graph as JSON in a file. There are multiple libraries that can help you with serialization and deserialization from Java to JSON and vice versa. One example is the excellent Jackson library that easily and without fuzz converts objects to and from JSON.
you can use Microsoft Excel to save the attributes of this object and retrieve them later, there is an api for excel http://poi.apache.org/download.html
I want to create a temporary database on memory to read ans store XML data from the API.
I have been doing this in C# and .Net by simply creating a structured DataSet/DataTable and reading XML API data and store in it. Then use it for the other work and at the end dump it.
The XML data structure is already known, so I would create the datatable structure and then read XML and save rows one by one.
I would like to achieve the same flexibility in Java too. Still a newbie in Java desktop application development.
Try using HSQLDB with this connection URL: jdbc:hsqldb:mem:testDb
This will ensure that your dataset is created in-memory.
You can either use collection framework to store temporary data or you can use Sqlite for local database if you really need database.
I am making a math game in Java and I want to create the ability for the user to create a local account (or log in to an existing one), which would have a unique properties file. How would I do this?
You have a couple of options to look into. Java comes with the necessary libraries with which you can create, open, read and write to files. Using the File class, you could create an account file, for instance "User1.properties", and save the information in an easy to parse format. If you're looking to go one further, you can look into using XML or JSON, which are commonly used formats for saving information in this way. Here's some helpful links to get you started.
Using files in Java
Using XML in Java
Using JSON in Java
Another option you have available is to use a database in a similar fashion to websites. The type of database you use is up to you, although MySQL is an example of one commonly used database. Once again, here is a link to help you get started with MySQL in Java.
Regardless of what you use, you will need to take the username and password provided (or whatever combination of login credentials you would like your system to use), and compare them with those stored in your file or database.
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.