I am trying to create a database that is embedded within a JAR file and so far I am not having any luck. Here is what I have done so far (mostly by the "book" from the Derby docs).
Obviously, the first step is to create a database, which I did in a temporary directory. I then moved the created database files into my "classes" directory (from which I will build my JAR file) *excluding* the /tmp directory and *.lck files. All of these remaining DB files are jarred up. Executing jar -tf of this file yields this (among other things - this JAR also has a META-INF directory and some classes):
So far this is what I think it should look like for a database named 'northwind'. The next step is to include this JAR as a dependency on another project. Once this is done, I attempt to select data from one of the northwind tables. Here is code snippet of that
// configure system properties per docs
DataSource dataSource = new DriverManagerDataSource(EmbeddedDriver.class.getName(),"jdbc:derby:classpath:northwind", "");
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from employees");
And this is where everything goes wrong and I get "Table/View 'EMPLOYEES' does not exist." According to the documentation, this URL should not even work - the database name should begin with a "/" and I should not have to include the "classpath" subprotocol. But if I exclude either of these, I get a "Database /northwind not found" error message. The fact that I am not getting that error message leads me to believe that the embedded database *is* being found - I am just not sure where my tables are.
Also, if I change the URL to access the the database on the filesystem (from the previous project's build) via the directory subprotocol, everything works. So, I know the database was created correctly at one point in time.
I have been struggling with this for a day now and I am at a loss. Is there anything obvious I am doing wrong? Did I miss a configuration step to make a embedded JAR database work? Any help would be great.