I'm creating a program in Java in which I have to use a lot of tables.
I made the decision to create all those tables in a JavaDB file.
However, now I want to distribute this filled JavaDB file with my JAR file
for distribution, since connecting to a server is not an option.
In the past I could only distribute an empty database with the JAR
by using that Derby package.
Any ideas?
Thank you so much!
I'm not sure I understood the question but it is possible to package a read-only database inside a JAR. From the derby documentation:
Accessing Databases-in-a-Jar in the Class Path
Once an archive containing one or more
Derby databases has been created it
can be placed in the class path. This
allows access to a database from
within an application without the
application's knowing the path of the
archive. When jar or zip files are
part of the class path, you do not
have to specify the jar subsubprotocol
to connect to them.
To access a database in a zip or jar
file in the class path:
Set the class path to include the jar or zip file before starting up
Derby:
CLASSPATH="C:\dbs.jar;%CLASSPATH%"
Connect to a database within the jar or zip file with one of the
following connection URLs:
jdbc:derby:/databasePathWithinArchive
(standard syntax)
jdbc:derby:classpath:/databasePathWithinArchive
(syntax with subsubprotocol)
For example:
jdbc:derby:/products/boiledfood
jdbc:derby:classpath:/products/boiledfood
If this doesn't answer the question, please clarify.
Related
So i currently have a java application that uses an Apache derby embedded database to store information regarding the user.
I built the application in Netbeans, and it now resides within the dist folder, the folder contains the executable jar, the Apache database, and the lib folder. so I can launch the application by selecting the executable jar but i don't want to distribute this folder to users, I would rather have a single exe that the user can select instead of giving them the folder that has the executable jarand other folders within it.
I have been looking into tools like launch4j but they don't do what i need.
Is it possible to create a exe that contains a derby database?
EDIT
also I need to write to the database to store users details.
I have been looking for a while now but haven't found exactly what I want. So, I have a project written in Java and have created a small Derby database that I have embedded in the project. Now when creating an executable jar of the project, I cannot access the database anymore unless I put the derby database files in the same directory of where the executable jar is located. However, I do not want this, I really want that the database files are INSIDE the jar.
I have tried to put the database files in a package and reference it like that, without any luck. Could someone point out how I can get this to work please?
Thanks!
EDIT
The goal here is that I want to create more than one executable JAR file and upload them to a Sun Grid Engine and execute each jar with different parameters. However, they all need to be able to access the database and until now I get an error when 1 application already booted the derby database, another application cannot access it. Therefor ALL jar files need to have their own database.
Do you use the jdbc:derby:jar protocol to access your database?
According to the documentation what you're after should be possible.
Inside your app (the one going into the executable jar) you must access the database using the jdbc:derby:jar protocol.
Create the database with the desired content.
When you have built your jar, you can use jar commands to add the database dir to your jar.
All of this could go in your build script, obviously..
There may be issues related to jar sealing and such, YMMV.
I'm trying to make an servlets application with java and oracle10g and I've had it well so far until I need some specific values from some the database, for wich I have a DAO class that handles the connection for retrieving data. I have the following issue.
First off, I excecute a main() method in this class that is suposed to retrieve all entries in some table an print the name of each one in console. I works perfectly.
then I want to return an ArrayList of all those names in order to use them in the servlet. So I make a method just like the one in the main() with the only difference that instead of printing the names, I add each one to an ArrayList which is returned after closing the conection. Well, It gets ClassNotFoundException in the line Class.forName("oracle.jdbc.driver.OracleDriver")
If it helps, I'm guided with this tutorial to connect java applications to oracle databases.
Any help would be appreciated
Put ojdbc14.jar inside your war file at WEB-INF/lib/ directory. You can use ANT task to do this. If you are unfamiliar with ANT, you can just copy the jar file inside WEB-INF/lib/ directory under your project and just zip it using Windows explorer or WinZip or anything else that works for you. Then rename the .zip file to .war and deploy on Tomcat server.
If you want some quick fix just copy the ojdbc jar file to server/lib directory under tomcat and restart tomcat. It should work.
EDIT: refer the comment below. While personally I have not seen any unexpected behavior with JDBC drivers in web-app classloader, but, it is recommended to keep driver jars under Server lib.
I've done a java aplication that have some images as resources like this:
->src
->resources
->images
For accessing this resources i've used getClass().getResource("/resources/images/img.jpg"), this works fine when i am on eclipse, but when i export the project to a jar the path change to something this genre: "jar:C:/path/deployed.jar!/resources/images/img.jpg".
What i am doing wrong? It is possible to export all the project in one jar?
One more question, this resources include a derby db that dont work either when deployed in jar file
Thanks
Try using getResourceAsStream() instead of getResource() to access resources in your jar file.
One more question, this resources include a derby db that dont work either when deployed in jar file
As Hovercraft stated, Derby DB (meaning the data files, not implementation) won't start from a jar. And it doesn't matter you don't insert anything, Derby needs to open these files for writing. They need to be in a directory where you have writing access.
In my project when i clicked the jar file it extracts embedded database files to near it self.
Is there any way to make them stay in the jar file?
You can package a read-only Derby database into one of the jar files in your classpath, and access it using a different form of the connection URL. See: http://db.apache.org/derby/docs/10.8/devguide/cdevdvlp24155.html and http://db.apache.org/derby/docs/10.8/devguide/cdevdeploy11201.html#cdevdeploy11201
Sure it's possible, you can put your DB image into any JAR's classpath and access it using:
Class.getResourceAsStream()