Search code examples
javajarderby

Embedded Database into Jar File


For the past couple of days I have been trying to get my Derby database to be accessed in my jar file. Here is what my connection class looks like:

    import java.sql.Connection;
import java.sql.DriverManager;

import javax.swing.JOptionPane;

public class DBConnection
{
    public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
    public static final String JDBC_URL = "jdbc:derby:EmployeeInfo";
    public static Connection dbConnector()
    {
        try
        {
            Class.forName(DRIVER).newInstance();
            Connection conn = DriverManager.getConnection(JDBC_URL);
            JOptionPane.showMessageDialog(null, "Connection successfull");
            return conn;
        }catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
    }
}

Also, here is a screenshot of what my project explorer looks like: Note that the database jar file is my apache derby database

Everything works when I am in eclipse, my program runs as expected with my GUI updating the Database. But the minute I make my program a jar file it says it can't find my database EmployeeInfo (note: Database.jar is the EmployeeInfo database). One last thing, when I try the jar file out on a different machine it also states that it cannot find the database.

An explination on why this is going along with any fixes would be great! -Thanks, Aaron :)


Solution

  • Well i eventually found the fix :). I had to place the embedded derby database files into a folder along with the derby jars, and my jar file that contained my project and everything worked beautifully!