Enter your email address:

Delivered by FeedBurner

feedburner count

How to Create Connection to an Embedded Derby Database (Java DB)

Labels: ,

The Derby database also known as Java DB is an open source Java database. It is part of the Apache project and it is distribute with Sun Java JDK 6.
In order to create a Connection or a DataSource Object to an embedded Derby database, we need to have the derby jar file in our Java classpath. Derby comes with JDK 6. If you don't have it you can download it from the derby web site or from sun site. On Ubuntu Linux you can download it using apt-ge:
apt-get install sun-java6-javadb

In the following Java examples the database files are at data/testdb, the user name and the password are empty and the driver is set to create the database if it is doesn't exist.

Creating a single connection to an embedded Derby database:
return DriverManager.getConnection("jdbc:derby:data/testdb;create=true", "", "");

Creating a DataSource Object to an embedded Derby database:
javax.sql.DataSource ds = new EmbeddedDataSource();
((EmbeddedDataSource) ds).setUser("");
((EmbeddedDataSource) ds).setPassword("");
((EmbeddedDataSource) ds).setDatabaseName("data/testdb");
((EmbeddedDataSource) ds).setCreateDatabase("create");


Post a Comment