• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java - Embedded Derby Database and NetBeans 6.1

bobross419

Golden Member
I'm trying to learn some basic JDBC to setup an MPG tracker in my spare time at work. After looking at some of the options and tutorials (Most of which are really f'n hard to make any sense of) I decided to go with the Derby database. I wanted to document everything that I have done in as much detail as possible for the best chance at getting an answer, so I apologize if I seem a bit long winded.


Due to software requirements at work I can't go any higher than Java 1.5_11 on my PC - I do my coding in downtime at work - so I didn't have JavaDB embedded with my JDK (6.0 and higher from what I've read). I tracked down and installed JavaDB.

To setup the drivers in NetBeans I went to the DataBases node on my Services tab and Right-Clicked on the "Drivers" folder and Selected "New Driver..." I pressed the "Add" button and went and found the derby.jar file then selected my driver class as "org.apache.derby.jdbc.EmbeddedDriver".

I now see "Java DB (Embedded)" in my Databases>Drivers folder.

I right clicked on Databases > Java DB and selected the "Create Database" option. I gave my Database a name and used the default database location "C:\Documents and Settings\sreno\.netbeans-derby".

I now see "jdbc:derby://localhost:1527/CoffeeShop [ on APP]" showing under Databases.

I can right-click on Databases>jdbc:derby://localhost:1527/CoffeeShop [ on APP] and select "Connect" - I am able to connect and create new tables.

I then followed the tutorial at Establishing a Connection (The Java Tutorials > JDBC(TM) Database Access > JDBC Basics to start writing my code.


Here is the part of my code that is giving me problems:

public class Main
public static void main(String[] args){

try{
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection con = DriverManager.getConnection("jdbc:derby:CoffeeShop");
}
catch (ClassNotFoundException e){
System.out.println("Unable to load the driver class");
}
catch (SQLException e){
System.out.println("SQL Exception");

I built and ran the project and received the following:

Compiling 1 source file to C:\Documents and Settings\sreno\My Documents\NetBeansProjects\AddressBookDemo\build\classes
compile:
run:
SQL Exception
BUILD SUCCESSFUL (total time: 6 seconds)

I decided that it didn't like the way I put just "jdbc:derby:CoffeeShop" so I changed this to the full "jdbc:derby://localhost:1527/CoffeeShop".

I again threw the SQL Exception. I went into the Services tab and went to Databases > jdbc[...]CoffeeShop > Properties and discovered that the driver is set to the following:

org.apache.derby.jdbc.ClientDriver

This is the driver for Java DB (Network). So I decided to try this driver instead. I changed the "EmbeddedDriver" to "ClientDriver" in my code's Class.forName() method. Running this results in the "Unable to Load the driver class" message.

I changed "ClientDriver" back to "EmbeddedDriver" in my code and changed the Driver in the jdbc[...]CoffeeShop's Properties to replace "ClientDriver" with "EmbeddedDriver". Doing this again results in the SQL Exception.

Trying to diagnose this a little further I have found that if I Right-Click on Databases>Drivers>Java DB (Embedded) and select the "Connect Using..." option I receive a screen that appears to let me connect to a database. I went in and put in the Database URL (Both with and without the "Localhost:1527" part) and both times I received the same message:

"Unable to add connection. Cannot Establish a connection to jdbc:derby:CoffeeShop using org.apache.derby.jdbc.EmbeddedDriver (Database 'CoffeeShop' not found)."

Changing this Database's driver from EmbeddedDriver to ClientDriver has no effect on this at all. I then tried connecting to the Database using the Java DB(Network) driver and received a message that the connection had been established. This worked wether I had the Driver for CoffeeShop set to either EmbeddedDriver or ClientDriver in the CoffeeShop Properties.


I then Right-Clicked on Databases> jdbc[...]CoffeeShop and select the option to "Connect". When the Driver is set to ClientDriver I am able to connect, but when the Driver is set to EmbeddedDriver I receive the following error message:

"Unable to connect. Cannot establish a connection to jdbc:derby://localhost:1527/CoffeeShop using org.apache.derby.jdbc.EmbeddedDriver (Unable to find a suitable driver)."

I have also tried changing the database's URL to just "jdbc:derby:CoffeeShop" but when I attempt to connect I receive a message that the Class CoffeeShop can not be found.

So I am now at a point where I can apparently connect in the NetBeans using the Client/Network Driver but not the EmbeddedDriver, and I can find the EmbeddedDriver in my code but not the Client Driver.

I had a really really slow day at work today and I was hoping to get at least a test database up and running, but I've spent all day dorking around with this.

Any suggestions are greatly appreciated,
Thanks - Bob
 
Just an update. I attempted to add another Embedded Driver to test this out, but I experienced the same issues as before. Oddly enough, when I attempt to delete the Driver I receive the following message:

"Driver Java DB(Embedded)(1) is in use by connection jdbc:derby://localhost:1527/CoffeeShop [ on APP]. Are you sure you want to delete it?"

If this wasn't a company laptop I do believe that it would be flying across the room right now...
 
Back
Top