Android 是否支持 JDBC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1728476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:32:30  来源:igfitidea点击:

Does Android Support JDBC

androidsqlitejdbc

提问by Mojo Risin

I know that on Android there is android.database.sqlite package that provides helpful classes to manage the internal android database.

我知道在 Android 上有 android.database.sqlite 包,它提供了有用的类来管理内部 android 数据库。

The question is - can I use the standard java.sql package to manipulate Android's database without using anything from android.database.sqlite.* I try to open connection using SQLite JDBC driver but when i added the library as e reference to the project eclipse crashes with "java heap out of memory ... couldn't convert to dalvik VM".

问题是 - 我可以使用标准的 java.sql 包来操作 Android 的数据库而不使用 android.database.sqlite.* 我尝试使用 SQLite JDBC 驱动程序打开连接,但是当我添加库作为对项目 eclipse 的引用时因“java 堆内存不足...无法转换为 dalvik VM”而崩溃。

采纳答案by CommonsWare

You cannot import a JAR implementing java.*classes easily. And, JDBC would need to be ported to Android, since it probably relies upon classes in JavaSE that Android lacks. And, you would need to write your own JDBC driver for SQLite anyway, wrapping the API Android already supplies, since I suspect the existing JDBC driver uses JNI. And, when all of that is done, you will have an application that adds a lot of bloat, making it less likely people will download and retain your application.

您无法java.*轻松导入 JAR 实现类。而且,JDBC 需要移植到 Android,因为它可能依赖于 Android 缺少的 JavaSE 中的类。而且,无论如何您都需要为 SQLite 编写自己的 JDBC 驱动程序,包装 Android 已经提供的 API,因为我怀疑现有的 JDBC 驱动程序使用 JNI。而且,当所有这些都完成后,您的应用程序会增加很多膨胀,从而降低人们下载和保留您的应用程序的可能性。

In short, I wouldn't go this route.

总之,我不会走这条路。

回答by kristianlm

There is an (undocumented?) JDBC driver for Android's SQLite database. Try this: (from http://groups.google.com/group/android-developers/browse_thread/thread/cf3dea94d2f6243c)

Android 的 SQLite 数据库有一个(未记录的?)JDBC 驱动程序。试试这个:(来自http://groups.google.com/group/android-developers/browse_thread/thread/cf3dea94d2f6243c

    try {
        String db = "jdbc:sqlite:" + getFilesDir() + "/test.db";

        Class.forName("SQLite.JDBCDriver");
        Connection conn = DriverManager.getConnection(db);
        Statement stat = conn.createStatement();
        stat.executeUpdate("create table primes (number int);");
        stat.executeUpdate("insert into primes values (2);");
        stat.executeUpdate("insert into primes values (3);");
        stat.executeUpdate("insert into primes values (5);");
        stat.executeUpdate("insert into primes values (7);");

        ResultSet rs = stat.executeQuery("select * from primes");
        boolean b = rs.first();
        while (b) {
            Log.d("JDBC", "Prime=" + rs.getInt(1));
            b = rs.next();
        }

        conn.close();
    } catch (Exception e) {
        Log.e("JDBC", "Error", e);
    } 

回答by Elliott Hughes

the JDBC driver is undocumented and unsupported. please do not use this code.

JDBC 驱动程序未记录且不受支持。请不要使用此代码。

avoid java.sql and use android.database.sqlite instead.

避免 java.sql 并使用 android.database.sqlite 代替。

回答by Alexey Romanov

There is such a driver now: SQLDroid.

现在有这样一个驱动程序:SQLDroid