How to store simple data for standalone java application

How to store simple data for standalone java application

sqlite java
embedded database java
java sqlite create database
how to retrieve data from sqlite database in java
h2 embedded database java
java sqlite create table
java in-memory database
how to connect sqlite database in java eclipse

Assume I have a long list of People with some basic characteristics:

Matt, 27, male

Anne, 49, female


I want to store this data locally for a standalone desktop application that does not require access to the internet. My current method is writing the names to a json file and reading them all in to a Person class in java when the program is opened; however, I did not think this was efficient as the data only needs to be accessed if someone is performing a search.

So I considered a simple JSONparser, let's say searchNameInFile("Matt", "data.json") { return person; }, but I did not know if this was a good way to store the data with regards to efficiency. I considered sqlite shortly, but I am only storing one table of data and thought it might be overkill. What is an appropriate, efficient method for storing and searching local data?

I really think you should reconsider SQLite.

Your data will be stored locally in a file that can be easily queried with standard Java SQL stuff. It does not require any server software or an internet connection.

If you don't want to use SQLite, here are two possible ways, depending on what you do care about.

If you only care about time complexity, but have plenty of memory to throw around, you could store your data in an efficient structure such as a HashMap, and use Java's built in Serializable to write the HashMap out to a file, and read it back in on next start.

//Where Person is a data class that implements Serializable and contains your desired fields
public void saveData(HashMap<String, Person> people) {
    FileOutputStream f = new FileOutputStream(new File("people.txt"));
    ObjectOutputStream o = new ObjectOutputStream(f);
public HashMap<String, Person> loadData() {
    FileInputStream fi = new FileInputStream(new File("people.txt"));
    ObjectInputStream oi = new ObjectInputStream(fi);
    HashMap<String, Person> data = (HashMap<String, Person>) oi.readObject();
    return data;

If you don't mind O(n) search times, then your current solution of re-reading the file on every search is fine.

Persisting Data in Java, Almost every application will need some way to persistently store data. We'll start with a simple way to save a small amount of data and continue Java program (as opposed to being run as a separate, standalone server). In this tutorial, we are going to create simple Java Spring standalone applications. One application will use an XML file, the other one annotations.

You can also consider another Simple in-memory DB as H2. It will save data in a relational db that is store in your computer as a file. The advantage is the high-retrieval speed benchmark it has. H2

Advantage is that configuration is as simple as create the DB object and configure it to use in-memory file.

Creating a Stand-Alone Java Application, If you're using Windows, you will need to set your path to include Java, if you haven't and type "cmd" to run a program that brings up a command prompt window. For example, to change to a directory called Desktop, you would type: Regarding which files to store in your JAR file, the .class files are the important ones. If you compiled your program using Java 1.6, but plan to run it on a Mac, you'll need to recompile your code from the command line, by typing: javac -target 1.5 *.java Now we'll create a single JAR file containing all of the files needed to run your program.

it might be overkill

Re-inventing the wheel would be the real overkill. There are plenty libraries and methods out there for storing and loading data.

Since you want to store the data locally in a file, but also want to search in the data, an embedded SQL database should be a good option for you. Either SQLite, h2 or HSQL.

For acessing the data, you can use anything from plain JDBC up to Spring Data JPA

Working with Embedded Databases in Java, A Java application, in particular, accesses the database using a JDBC driver. In an in-memory database, the data is stored entirely in the main These type of databases are lightweight, simple, fast, and come with bare The necessity of a full blown database manager (as found in a standalone RDBMS  Sun distributes Java DB in many of its products, including the Sun Java Enterprise System and the Sun Java System Application Server. The NetBeans integrated development environment (IDE) 5.0 also supports Java DB. Java DB is lightweight at 2 megabytes and embeddable within desktop Java technology applications.

Using Java DB in Desktop Applications, Learn how to deploy Java DB, based on the Apache Derby database, within desktop Java applications. Desktop applications can now access powerful database storage with One example shows how to create a trivial embedded application. including Java Web Start software, applets, and stand-alone JAR files. Thanks it helped a lot,I am new to java so my questions may be a little unclear,I needed to know how to make .exe file.I found it,thanks. – user2768759 Sep 12 '13 at 11:16 I'm just as new I'm sure. I just happened to be looking for the same thing just a little earlier.

Handbook of Data Communications and Networks, Java program 27.1 gives a simple example of a standalone program. 27.3 Data types Variables within a program can be stored as either boolean values,  If you only care about time complexity, but have plenty of memory to throw around, you could store your data in an efficient structure such as a HashMap, and use Java's built in Serializable to write the HashMap out to a file, and read it back in on next start.

Creating a Java application to access a Derby database, This section will demonstrate using Derby from a stand-alone Java application. This example will  This sample code below shows how to build an application out of a JFrame, two JPanels and a JButton, which determines the visibility of the components held in the two JPanels. Follow along with what is happening in the code by reading the implementation comments , indicated by two slashes at the beginning of each comment line.