0

Context:
I'm creating an application that lets a user connect to a database. This database is chosen by a FileChooser. If no database is selected, the application should connect to a sample database that comes with the program instead. The sample database is located inside src/files(/sample.db).

Problem:
The DriverManager.getConnection() method does not seem to work when I build the program as an executable JAR. It does however work in IntelliJ.

Code:

try {  
    System.out.println(selectedDB);  
    if (selectedDB == null) {  
        // TODO: make sure this works in JAR.  
        connection = DriverManager.getConnection("jdbc:sqlite::resource:files/sample.db");  
    } else {  
        connection = DriverManager.getConnection("jdbc:sqlite:" + selectedDB.toString());  
    }  
} catch (SQLException ex) {  
    ex.printStackTrace();  
    System.out.println("If you see this message, the DatabaseManager failed to make a connection to the database.");  
    System.out.println("This is most likely due to a fault in the databasename");  
}  

Should other code extracts be needed to answer this question, please ask.
Thanks in advance!

Error:
connection remains null after the above method is done, instead of a working connection.

Pictures:

https://i.stack.imgur.com/lqGVR.jpg
(Not enough reputation to post image itsself, only recently started stackoverflowing.)
Edit: Becomes -> remains

Crow Alt
  • 11
  • 2
  • "does not [..] work" => You have to describe the error! – Seelenvirtuose Mar 18 '18 at 12:45
  • @Seelenvirtuose My bad, added what happened vs what was expected. – Crow Alt Mar 18 '18 at 12:55
  • I doubt that `connection` will be `null` after that piece of code is run. Maybe it throws an exception which you swallow, but then work with the variable `connection` ... – Seelenvirtuose Mar 18 '18 at 13:04
  • @Seelenvirtuose Yes, most likely. The problem only arises when the program tries to use a function on this connection later. Would you know why the connection throws an exception when executing a JAR file, but not when testing in IntelliJ? – Crow Alt Mar 18 '18 at 13:11
  • You have to tell us the exception (and the stack trace)! – Seelenvirtuose Mar 18 '18 at 13:36
  • how is this related to fx? – kleopatra Mar 18 '18 at 13:57
  • @Seelenvirtuose Added link to stacktrace pictures (and updated code to show more details). – Crow Alt Mar 18 '18 at 13:57
  • Which condition line invokes this exception if or else ? – Menai Ala Eddine - Aladdin Mar 18 '18 at 17:37
  • @MenaiAlaEddine The if-condition, although the else-condition does it as well. But I haven't really tried to make that one work yet. – Crow Alt Mar 18 '18 at 17:53
  • A stacktrace is text! Please post it as one in your question (not as a picture). Additionally, always show the line (in your own code) where the exception occurs. – Seelenvirtuose Mar 18 '18 at 18:07
  • @Seelenvirtuose Will remember that for next time. Also, the line where the exception occurs is not very relevant to my problem, it is just a sideeffect. The real problem is that the above declaration of connection seems to become invalid when you compile the program into an executable jar file. – Crow Alt Mar 18 '18 at 20:55

0 Answers0