Using “ DriverManager.getConnection( ”jdbc:sqlite::resource: … “ ) ” in IntelliJ?
Answered
I get the following error from IntelliJ:
java.sql.SQLException: resource database/test.db not found: java.net.MalformedURLException: no protocol: database/test.db
And this is the line I am using:
Connection connectorForDB = DriverManager.getConnection( "jdbc:sqlite::resource:database/test.db" );
Here is the layout of my "assets" folder:
I am also using Oracle JDK 10.0.1 in an up-to-date Linux environment, although I am using compatibility with JDK 8. All other assets work fine from the "assets" folder. How should I address this?
Thank you for your time.
Please sign in to leave a comment.
Looking at the error I would say the the url connection string is not correct. Note the database connections in Android projects might be needed to make differently than use see here https://bitbucket.org/xerial/sqlite-jdbc/wiki/Usage
This seems rather related to Android and SQLite that IDE usage. Please check this threads: https://stackoverflow.com/q/20857734/2000323, https://stackoverflow.com/a/27253137/2000323
In my project, other "modules" access the "assets" folder within the "android" folder for resources. In the Run / Debug Configuration, the "Working directory" is set to the "assets" folder for the example I try to run.
The example I am testing focuses on "Desktop" at the moment. It presently does not involve the Android app stuff of Activities, Adapters, and so on. You were certainly right that I was not correctly using the "url connection string" for " DriverManager.getConnection() " in regard to how IntelliJ perceives the code. Using the following line enables the Desktop part successfully within IntelliJ:
However, this effectively breaks the Gradle build for the project, which had been working. I would still need the " ... jdbc:sqlite::resource: ..." part to remain compatible with both IntelliJ and Gradle. I believe the " ::resource: " part is addressed as a "subprotocol" in this link:
https://docs.oracle.com/javase/10/docs/api/java/sql/DriverManager.html#getConnection(java.lang.String)
I would not know how to address this within IntelliJ.
Again, thank you for your invested time.
>I was not correctly using the "url connection string" for " DriverManager.getConnection() " in regard to how IntelliJ perceives the code.
It is not IntelliJ IDEA perceives the code it is how java classloders work in general. Working directory sets the directory where the application is launched. I.e. like you would cd to that directory and launch the applicaiton with the java command. When you set it to assets folder and use "database/test.db" the file is found because the classloader uses assets folder as the root folder.] to start search from.
>I would not know how to address this within IntelliJ.
Again it is not related to IDE at all. It is about how resources are accessed in Android application and what syntax sqlite jdbc driver supports for it's connection strings. Are you sure using connection string is the correct way to connect to the database in Android? Please check this thread: https://stackoverflow.com/questions/38123756/connect-to-sqlite-in-android-using-connection-string