Question

My aim is to create a local database that can be read and written to with Java. I have some experience with manipulating a local sqlite database with Python, and with interacting with existing networked databases on Microsoft Azure via VB.Net, but the Java formulation for creating a database is escaping me.

Most sources (like the JDBC Docs) seem to assume that you are accessing a database through a network protocol, or a database hosted on localhost. My desired implementation is to create and store the database in a file (or collection of files), so that it can be stored and accessed locally, without network connectivity (presumable through the "file:" protocol).

The JDBC Tutorial looks like it will be very useful once I am up and running, but is currently beyond my scope, since I don't even have an existing database yet.

Many sources have suggested solutions like H2, MySQL, Derby, or Hypersonic DB. However, I'm loath to install extensions (if that's the right term) for a number of reasons:

  • This project is initially intended to help me learn my way around Java - widening the scope of the project will dilute my experience with the "base" language and, probably, increase the temptation to engage in "cargo cult programming"
  • If this project does ever get distributed to other users (admittedly unlikely, but still!), I don't want to force them into installing more than the core of Java.
  • I simply don't know how to install extensions (add-ons? modules?) in Java - one baby-step at a time!

For similar reasons, installing Microsoft SQL Server would not be productive.

This answer looks close to what I'm aiming for; however, it gives the error:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/?user=root&password=rootpassword

and trying "jdbc:file://targetFile.sql" gives a similar error.

I've seen the term "embedded" database, which I think is a subset of "local database" (i.e. a local database is stored on the same system - an embedded database is a local database that is only used by a single application) - if I've got those definitions wrong, please feel free to correct me!

Was it helpful?

Solution

Most likely, the reason for which you are getting the error, is due to the fact that you are not registering the driver (using reflection...) before actually using it for establishing a connection and so on.

Presumably you will want to do something along the lines of Class.forName("driver")

and then cast that if necessary and then registering it in the DriverManager before calling the getConnection() method.

Here is a very useful link that might help you out in solving the issue:

http://www.kfu.com/~nsayer/Java/dyn-jdbc.html

However, if you really want to use a local database/file you might want to have a look at SQLite, that might be one way to go about it, although I recommend going for the MySQL approach, as it is a lot easier to configure and learn how stuff works with JDBC.

If you are still considering SQLite check this out:

Java and SQLite

I see you need some guidance in importing external .jar files into your code (i.e. 3rd party libraries like the ones you will be using for a JDBC driver). Are you using an IDE (e.g. Eclipse, Netbeans, etc.) or are you writing in a text editor and compiling manually?

OTHER TIPS

A number of embedded pure Java databases appeared recently, which have a really simple interface, usually just java.util.Map, don't involve using JDBC or other SQL artifacts, and store their data in a single file or directory:

The main downside is that most of such databases provide only the simples key-value model.

DBC can be used with any database that has a JDBC driver, which isn't necessarily a database in "network mode", it can be used with embedded databases as well.

Here are some Java and embeddable databases:

http://www.h2database.com/html/main.html

http://db.apache.org/derby/

http://hsqldb.org/

Java's JDK does not include any implementation of a database nor drivers to access it. It only provides JDBC as an abstraction to connect to a "database". Is up to you to include all the needed libraries in your code.

If you want to have a self contained code you can simply include the .jar file of a embeddable database in you classpath. That way you can create the instance of the database in your code and minimize the external dependencies.

You can find here a list of java embeddable databases

You can find here an example of how to embed HSQLDB in your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top