Javafxports and SQLite on Android Device

自古美人都是妖i 提交于 2019-12-19 10:28:07

问题


i will write a sample code with sqlite, that must work both ANdroid and IOS (and Desktop)

Here is my 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

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).

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


回答1:


You can load the driver you need for each platform, and with Gluon Charm-Down find out about the platform and load it.

Using Gluon plugin on your IDE, in the build.gradle file it's easy to add different dependencies depending the platform.

EDIT

Added Desktop as well.

For Desktop we can use org.sqlite.JDBC and for Android we can use org.sqldroid.SQLDroidDriver. For iOS no dependency is required since SQLite.JDBCDriver it's already included by 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:

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. 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.



来源:https://stackoverflow.com/questions/33655851/javafxports-and-sqlite-on-android-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!