简体   繁体   English

Android 设备上的 Javafxports 和 SQLite

[英]Javafxports and SQLite on Android Device

i will write a sample code with sqlite, that must work both ANdroid and IOS (and Desktop)我将使用 sqlite 编写一个示例代码,该代码必须同时适用于 ANdroid 和 IOS(以及桌面)

Here is my build.gradle这是我的build.gradle

  buildscript {
    repositories {
        jcenter()
        mavenCentral()
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    }
    dependencies { classpath 'org.javafxports:jfxmobile-plugin:1.0.6' }
}
apply plugin: 'org.javafxports.jfxmobile'
repositories {
    jcenter()
    mavenCentral()
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    maven { url 'https://oss.sonatype.org/content/repositories/releases' }
}
ext.CHARM_DOWN_VERSION = "1.0.0"
dependencies {
    compile 'mysql:mysql-connector-java:3.1.12'

    //compile 'org.xerial:sqlite-jdbc:3.9.0-SNAPSHOT'

    compile 'org.sqldroid:sqldroid:1.0.3'
    compile "com.gluonhq:charm-down-common:$CHARM_DOWN_VERSION"
    desktopRuntime "com.gluonhq:charm-down-desktop:$CHARM_DOWN_VERSION"

    //desktopRuntime 'org.xerial:sqlite-jdbc:3.9.0-SNAPSHOT'

    androidRuntime "com.gluonhq:charm-down-android:$CHARM_DOWN_VERSION"
    androidRuntime 'org.sqldroid:sqldroid:1.0.3'

    iosRuntime "com.gluonhq:charm-down-ios:$CHARM_DOWN_VERSION"
}
mainClassName = 'com.version17.Version17'
jfxmobile {
    android {
        manifest = 'src/android/AndroidManifest.xml'
        packagingOptions {
            exclude 'META-INF/INDEX.LIST'
        }
    }
    ios {
        infoPList = file('src/ios/Default-Info.plist')
        forceLinkClasses = ['com.version17.**.*', 'com.mysql.**.*', 'SQLite.**.*', 'com.gluonhq.**.*']
    }
}

sqliteHelper.java sqliteHelper.java

public static void testSqli() throws SQLException, ClassNotFoundException{
    Class.forName("SQLite.JDBCDriver");

    String dbName = "mtt8.db";
    File dir = null;
    try {
        dir = PlatformFactory.getPlatform().getPrivateStorage();
    } catch (IOException e) {
        e.printStackTrace();
    }

    File db = new File (dir, dbName);
    String dbUrl = "jdbc:sqlite:" + db.getAbsolutePath();




    Connection conn = DriverManager.getConnection(dbUrl);

    //create table
    Statement st=null;

        st = conn.createStatement();
        st.executeUpdate("DROP TABLE IF EXISTS village;");
    st.executeUpdate("CREATE table village (id int, name varchar(20))");
        //insert row
    for (int i=0; i<50; i++){
        st.executeUpdate("INSERT INTO village VALUES (" +i+ ", 'Erkan Kaplan')");
    }


    //select
    String query = "SELECT id, name from village";
    ResultSet rs = null;

            rs = st.executeQuery(query);

            while(rs.next()) {
                int id = 0;
                id = rs.getInt(1);

                String name = null;

                    name = rs.getString(2);

                System.out.println("id:"+ id+ ", name: "+ name);
            st.executeUpdate("DELETE from village");
            rs.close();

    }

}

This work on Ipad Devices and Desktop but not on Android-Devices (like Samsung Tablet).这适用于 Ipad 设备和台式机,但不适用于 Android 设备(如三星平板电脑)。

Can anybody please say me why this code above dont work on Samsung Tablets?谁能告诉我为什么上面的代码在三星平板电脑上不起作用? or which depends i must add in my code?或者哪个取决于我必须在我的代码中添加?

thanks Erkan Kaplan感谢 Erkan Kaplan

You can load the driver you need for each platform, and with Gluon Charm-Down find out about the platform and load it.您可以为每个平台加载所需的驱动程序,并使用 Gluon Charm-Down了解平台并加载它。

Using Gluon plugin on your IDE, in the build.gradle file it's easy to add different dependencies depending the platform.在 IDE 上使用build.gradle插件,在build.gradle文件中,可以根据平台轻松添加不同的依赖项。

EDIT编辑

Added Desktop as well.还添加了桌面。

For Desktop we can use org.sqlite.JDBC and for Android we can use org.sqldroid.SQLDroidDriver .对于桌面我们可以使用org.sqlite.JDBC而对于 Android 我们可以使用org.sqldroid.SQLDroidDriver For iOS no dependency is required since SQLite.JDBCDriver it's already included by Robovm.对于 iOS,不需要依赖项,因为SQLite.JDBCDriver已经包含在 Robovm 中。

repositories {
    jcenter()
    maven { 
        url 'https://oss.sonatype.org/content/repositories/snapshots' 
    }
}

ext.CHARM_DOWN_VERSION = "1.0.0"

dependencies{
    compile "com.gluonhq:charm-down-common:$CHARM_DOWN_VERSION"

    desktopRuntime "com.gluonhq:charm-down-desktop:$CHARM_DOWN_VERSION"
    desktopRuntime 'org.xerial:sqlite-jdbc:3.9.0-SNAPSHOT'

    androidRuntime "com.gluonhq:charm-down-android:$CHARM_DOWN_VERSION"
    androidRuntime 'org.sqldroid:sqldroid:1.0.3'

    iosRuntime "com.gluonhq:charm-down-ios:$CHARM_DOWN_VERSION"
}

But we need to add it to the forceLinkClasses option:但是我们需要将它添加到forceLinkClasses选项中:

jfxmobile {
    android {
        manifest = 'src/android/AndroidManifest.xml'
    }

    ios {
        forceLinkClasses = [ 'your.package.**.*', 'SQLite.**.*']
        infoPList = file('src/ios/Default-Info.plist')
    }
}

Now, in your code you can load one driver or the other depending on the platform the app is running on, and create a connection, providing a local path like discussed here :现在,在您的代码中,您可以根据应用程序运行的平台加载一个或另一个驱动程序,并创建一个连接,提供一个像这里讨论的本地路径:

private void testSqli() throws SQLException, ClassNotFoundException {

    if (JavaFXPlatform.isAndroid()) {
        Class.forName("org.sqldroid.SQLDroidDriver");
    } else if (JavaFXPlatform.isIOS()) {
        Class.forName("SQLite.JDBCDriver");
    } else if (JavaFXPlatform.isDesktop()) {
        Class.forName("org.sqlite.JDBC");
    }

    File dir;
    String dbUrl = "jdbc:sqlite:";
    try {
        dir = PlatformFactory.getPlatform().getPrivateStorage();
        String dbName = "yourDatabase.db";
        File db = new File (dir, dbName);
        dbUrl = dbUrl + db.getAbsolutePath();
        Connection conn = DriverManager.getConnection(dbUrl);
        ...
    } catch (IOException ex) { }
}

Now you should be able to run it on Desktop, Android and on iOS.现在您应该能够在桌面、Android 和 iOS 上运行它。 I've tested on the three of them, both with NetBeans and with IntelliJ, but the former manages the platform dependencies better than the latter.我已经用 NetBeans 和 IntelliJ 对其中三个进行了测试,但前者比后者更好地管理平台依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM