Using SQL (JDBC) database in Android

2019-09-05 06:14发布

问题:

In an old Java application I have the following code to connect to a SQL database and use it for some queries:

private Connection dbConnection = null;
System.setProperty("derby.system.home", "C:\\");
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
String strUrl = "jdbc:derby:BOOKSDB";
dbConnection = DriverManager.getConnection(strUrl);
Statement stmt = dbConnection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM BOOKS");

Is there any chance to use it on Android? The documentation say that it is possible in general, isn't it? But how can one use it?

Or would it be easier to change this part so that it uses SQLite instead? I think this is much work to do because with SQLite, you always have a cursor instead of a ResultSet and so on ...

回答1:

You can use JDBC on android, provided you supply a JDBC driver that will run on Android. There is a open source pure-Java jdbc driver that should run on Android (although I haven't tried it myself). You will have to adapt the JDBC URL to connect to the sqlite db; that way you can use the local android database while making few changes to your data access code.