I'm trying to use xerial sqlite-jdbc to manage my database in Android with no success.I'm getting an java.lang.NoClassDefFoundError: org.sqlite.SQLiteConnection
exception.I've imported this dependency 'org.xerial:sqlite-jdbc:3.18.0'
in my gradle.
My code is as follows,
try {
Class.forName("org.sqlite.JDBC");
Connection connection = DriverManager.getConnection("jdbc:sqlite:hs.db");
} catch (ClassNotFoundException eString) {System.err.println("Could not init JDBC driver - driver not found");
} catch (java.sql.SQLException e) {e.printStackTrace();}
Using android.database.sqlite
in my project is not option so please don't suggest that as an answer.
Gradle
compile 'org.sqldroid:sqldroid:1.0.3'
JAVA
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
public class MainActivity extends AppCompatActivity {
private Connection connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
DriverManager.registerDriver((Driver) Class.forName("org.sqldroid.SQLDroidDriver").newInstance());
} catch (Exception e) {
throw new RuntimeException("Failed to register SQLDroidDriver");
}
String jdbcUrl = "jdbc:sqldroid:" + "/data/data/" + getPackageName() + "/my-database.db";
try {
this.connection = DriverManager.getConnection(jdbcUrl);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void onDestroy() {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
super.onDestroy();
}
}
https://github.com/SQLDroid/SQLDroid
I needed to share some database code between my server DB and my phone DB, so I spent a lot of time writing the common code to use JDBC.
However, what I found out, after already being neck deep in this endeavor, is that there essentially isn't a working JDBC implementation on Android. I would strongly urge you not to go down this path - both Xerial and SQLDroid JDBC drivers have significant problems that I uncovered only during testing.
It just doesn't work on Android :) It doesn't even load - I ran into this issue and couldn't work around it even after trying for hours.
Finally, I ended up creating an implementation of the small subset of the java.sql interfaces and methods that I needed, using the android.database.sqlite classes. This was far less painful than I imagined (it took me just a couple of hours, for the subset of the functionality I needed) and it works great!
Here's my way:
app/build.gradle
, and copy a so file, looks ugly, but really works in my Android 9.0 system.android {
defaultConfig {
ndk {
abiFilters "armeabi-v7a"
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
dependencies {
implementation 'org.xerial:sqlite-jdbc:3.34.0'
}
then copy this file sqlite-jdbc-3.34.0.jar!\org\sqlite\native\Linux\android-arm\libsqlitejdbc.so
to app/libs/armeabi-v7a/
, which looks like this: .
and the built apk looks like:
now you can enjoy javax.sql.DataSource
of sqlite on android.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With