Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to browse a derby memory database with Eclipse Datasource Explorer?

Tags:

eclipse

derby

For unit testing I use a derby in-memory database.

Is there any chance to connect to this database using a tool like Eclipse Datasource Explorer when the test is running?

I googled a lot and sometimes I found something like:

Connection-URL: jdbc:derby://localhost:1527/memory/mydb...

But it did not work for me.

It says that 1527 is the default port.

Is it possible at all to connect to a derby memory database with a tool like eclipse explorer? Does the database open a connection-port to connect to? Or is there something special I have to configure for this to work?

Thanks, Alex

like image 783
Subby Avatar asked Aug 03 '12 13:08

Subby


1 Answers

Hi after some more research I got the solution.

To connect to a embedded derby memory database you have to start the NetworkServerControl in your application. After that you are able to connect to the derby database by using for example the eclipse DTP Plugin / Datasource Explorer.

The code to create the in-memory db and to start the NSC could look like this:

public static void main(String args[])
{
   NetworkServerControl nsc = new NetworkServerControl(InetAddress.getByName("localhost"), 1527);
   nsc.start(new PrintWriter(System.out, true));

   Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

   Connection c = DriverManager.getConnection("jdbc:derby:memory:testdb;create=true");

}

You have to include the derby.jar & derbynet.jar that comes with the jdk7 (lib\db) to be able to create the NetworkServerControl and the database.

After that you can connect to the db as long as your application (and the database) is running. Connection-URL is: jdbc:derby://localhost:1527/memory:testdb

User and Password: your choice

Regards,

Alex

like image 140
Subby Avatar answered Jan 03 '23 14:01

Subby