8

I'm looking for a library to manage a "database" in my Java application. I want something simple, and easy to use. Specifically, I'm looking for something which:

  • Is easy to start using. I'd like to have a single JAR to add to my project, and the database is a single Java File. I want to avoid having to set up a local server or connections and similar. Entirely Java without native dependencies would be nice.

  • Is simple to use. I only need to store a table with some strings and do simple queries, eg. for everything in a specific row.

  • Is lightweight.

I'm basically looking for something to emulate a "table" of sorts, like an Excel sheet, with text data.

Any libraries like this ?

2
  • My first question here, so please be gentle :-) Commented Apr 24, 2015 at 19:22
  • As the Answer by Flimzy says, you need to provide more information. [A] What kind of database: flat-file, relational, key-value, document, object? If you don't understand the types, then present small but specific examples of data. [B] Is the database meant to be embedded in the app and used only by the app, or should the database accept connections from other apps/clients? Commented Jun 17, 2015 at 2:53

6 Answers 6

10

H2

Without more specific information about your needs, my first thought is the H2 Database Engine project. H2 is a pure-Java, open-source, free-of-cost, relational database that has been actively developed for years.

The "2" refers to the fact that this is the second such database (Hypersonic SQL, later forked as HSQLDB) built by its main author, Thomas Mueller, so he knows his stuff.

While H2 can be run as a server, it is especially adept at being embedded inside an app and can run with relatively little memory if need be.

Being pure Java, H2 is built specifically for JDBC access. Comes bundled with its own JDBC driver.

The main web site has good documentation including a QuickStart overview and a Tutorial.

You will need to learn the basics of tables, columns, data types, and SQL commands. You can teach yourself. Getting some tutoring from someone familiar with relational databases will dramatically reduce that learning curve.

H2 is similar to SQLite but more fully featured. Being pure Java and embeddable makes H2 a better fit for your needs and probably easier to get you started.

Over at the sister site, Stack Overflow, I have written a few Answers providing source code for a complete Java example app using H2. Like this, this, this, this, this, and more.

Derby

The Apache Derby project is quite similar to H2. Formerly known as IBM Cloudscape, and also known as "Java DB" when provided by Oracle in a bundle with the Java SDK.

Derby is more complicated than H2, including being aimed at use as a server. I suspect H2 is a better fit for your needs.

Files

If all you need is relatively simple data in amounts small enough to be held in memory, then skip the database. Store your data as text in plain files.

Common formats for such plain text files include:

  • CSV, Comma-Separated Values.
  • TSV. Delimited by Tab.
  • ASCII Separators, ASCII codes # 28-31.
    Marks file, group, record, and unit/field). Less common that Tab but more sensible and flexible)

Note that while CSV sounds simple, various programmers have found ways to mix things up with variations. For decades, no one even bothered to write down a spec until RFC 4180 in 2005.

When you need this data, read it into memory, instantiate objects by defining your own classes such as "Person" or "Invoice", collect those objects, and search those collections to find desired objects.

While conceptually simple, there are many possible snafus. One of the main purposes of a database engine is to handle such problems for you.

  • You need to be sure to close files after using them, although when quitting or crashing the JVM should automatically close any open files.
  • You might consider file corruption. What if the app crashes or is otherwise interrupted in the middle of writing a file? One way to handle this is to write a new file and only delete the old one after.
  • If you are multi-threaded, then you need to guard against concurrency issues. You don't want different threads trying to replace data for the same file at the same time.

Apache Common CSV

The Apache Commons CSV project is a relatively new pure-Java free-of-cost library to handle the chore of reading and writing plain text files. It handles all the mentioned formats, not just CSV (despite its name). I've used version 1.1 and found it quite useful, robust, and reliable.

6

Your question is too broad to answer in anything like a definitive manner. You don't tell us at all what kind of database you want to use. Do you need an RDBMS? Key/value store? Something else?

If a relational database is what you have in mind, SQLite is the de-facto standard, and has Java support (as well as support for practically every other language and platform under the sun).

2
  • Thanks, I will try to improve my question. I'm basically looking for something to emulate a "table" of sorts, like an Excel sheet, with text data. SQLite would do, but i'm looking for something simpler. Commented Apr 24, 2015 at 21:10
  • @JonasCz: SQLite still meets all of your stated requirements: There are no servers to configure, no connections to make, etc.
    – Flimzy
    Commented Apr 25, 2015 at 2:00
4

You can take a look on HeftyDB. It is a fast key-value store, written in Java, and it's open-source.

4

Xodus

Based on your requirements, I think JetBrains Xodus is a very choice of database. It has three types of database- a key-value storage, an entity-store and a database for files and streams.

#1: It is very easy to start using. If you are using maven just add one or two dependencies. Or you can also import .jar files directly into your project.

No need to create any server. Just create an instant of Environment and you are ready to go. And you can give it your folder name to store data.

final Environment env = Environments.newInstance("/Users/me/.myAppData");

#2: Instead of managing tables you can just store entities. It is very similar to relational tables, but a lot more easier. Take a look here to know how to do it.

final Entity user = txn.newEntity("User");
user.setProperty("login", loginName);
user.setProperty("fullName", fullName);
user.setProperty("email", email);
final String salt = MessageDigestUtil.sha256(Double.valueOf(Math.random()).toString());
user.setProperty("salt", salt);
user.setProperty("password", MessageDigestUtil.sha256(salt + password)); 

#3: More lightweight than any other non-relational database avaiable. See their benchmark test.

1

Another option is MapDB. It is also a fast, open source key-value store written in Java.

1

The tragedy of high level questions on Stack forums! "Software Recommendations" is a good place but it's not well visited. I found in Stack Overflow nearly the same question in a few versions with a few more good answers:

it has links to overview articles and some additional mentions:

  • LevelDB
  • berkeley DB
  • jsondb.io

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.