I have embedded an Apache Derby database inside a JAR (it's the only thing located inside the jar). I'm trying to connect to that database with an executable JAR (the database jar would be inside the executable one). I'm able to successfully connect to it within my IDE, but not when I run the executable from the command line. I get and error saying it cannot find the database jar.
My code:
public static void queryDatabase() throws IOException {
String connection = "jdbc:derby:jar:(src/main/resources/dbs.jar)employees";
try (Connection conn = DriverManager.getConnection(connection)) {
RowSetFactory factory = RowSetProvider.newFactory();
CachedRowSet cachedRowSet = factory.createCachedRowSet();
cachedRowSet.setCommand("select id,salary,filingStatus from employees");
cachedRowSet.execute(conn);
while (cachedRowSet.next()) {
System.out.print(cachedRowSet.getString("id") + " ");
System.out.print(cachedRowSet.getFloat("salary") + " ");
System.out.println(cachedRowSet.getString("filingStatus"));
}
} catch (SQLException exception) {
exception.printStackTrace();
}
}
I know once the executable JAR builds, the src/main/resources url will no longer be valid. Is there anyway I can get the executable jar to find the internal database jar when running outside of the IDE?