NetBeans‎ > ‎

NetBeans Platform: Database Explorer and Embedded Derby

Paul Richardson asked an interesting question on the list: how the use the Apache Derby module (from the Update Center) ? I never used Derby much and especially not under the Platform but I knew there should be a way. Usually the Platform API is quite clear.

Well, I'm not sure if you can use only Apache Derby (I think not) since I've used the Database Explorer module.

First, you have the concept of JDBCDriver, which means just some class that is able to provide JDBC Connections to some database.

Using the JDBCDriverManager you can get a list of these "drivers":

JDBCDriver[] drivers=JDBCDriverManager.getDefault().getDrivers();

Once you have a JDBCDriver (Embedded Derby is called apache_derby_embedded), you are ready to create a DatabaseConnection:

 DatabaseConnection dbc=DatabaseConnection.create(derbyEmbedded,"jdbc:derby:demo;create=true","sa","","",true);

Please note that this is a "virtual" connection and it will only show up in the Database Explorer from the Runtime tab. This will not actually connect to the database. It took me a while to get that.

But since database connections are expensive, you are not allowed to use them anyhow. You must give control to the ConnectionManager by first adding your connection:

ConnectionManager.getDefault().addConnection(dbc);

and then showing the GUI dialog to actually connect to the database:

ConnectionManager.getDefault().showConnectionDialog(dbc);

At this point you are connected ! Please note there is no way to get rid of the Dialog. I don't know why.

Further on you just get the proper JDBC Connection (note the space there ;-) ):

Connection con=dbc.getJDBCConnection();

and use your normal code:

Statement st = con.createStatement();
st.execute("CREATE table TESTING (name varchar(100), age INTEGER)");

 

Problems ? Oh yes, some.

The API is under development so it's expected to have some quirks.

The first one would be the name: JDBCConnection with ConnectionManager and getJDBCConnection() which returns an SQL Connection is just too much !

Second, JDBCConnection.getJDBCConnection() fails if the "connection" (see ? you get lost quickly there) isn't set up by the user from the Database Explorer. It is true that you do have a note in the javadoc. Missed that note ? So did I.

Third, there is no way to connect without showing some sort of GUI. You do have ConnectioManager.showConnectionDialog() which should do the trick but it doesn't work. Even more - the name of the method is misleading.

Comments