2

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 Answers1

2

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.

Community
  • 1
  • 1
José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • hi jose, thanks, i edited my build-gradle above and get following error if i deploy on Android: http://pastebin.com/hzyWii1S – erkan kaplan Nov 11 '15 at 17:48
  • If you are using IntelliJ, maybe `androidRuntime` doesn't work (in NetBeans it does). Try to move it just to `runtime 'org.sqldroid:sqldroid:1.0.3'`. It will work fine as well on iOS, since it won't be added. – José Pereda Nov 11 '15 at 17:52
  • Jose, may be, they have conflict? if i comment or delete the line **compile 'org.xerial:sqlite-jdbc:3.9.0-SNAPSHOT'** so i can **./gredlew androidinstall** without any problem. But if i try to compiler some time result is always error like here http://pastebin.com/hzyWii1S – erkan kaplan Nov 11 '15 at 18:26
  • Why do you add org.xerial for? – José Pereda Nov 11 '15 at 18:32
  • as i say, i will write a small app. First connect to mysql-server on my Lan and save results as sqlite.db in clients ...The clients may be IOS or Android... – erkan kaplan Nov 11 '15 at 18:35
  • My build.gradle contains dependencies for android (sqldroid) and iOS (SQLite). Do you still need one for desktop? – José Pereda Nov 11 '15 at 18:37
  • Yes..some User can have Laptops and will ne use my app onlaptop – erkan kaplan Nov 11 '15 at 18:42
  • hi @José Pereda, do you have a solution for this Problem, that i can compiler all 3 sometime? – erkan kaplan Nov 12 '15 at 09:36
  • I've just edited the question to add desktop support as well. – José Pereda Nov 12 '15 at 10:14
  • jose, i test now with netbean 8.1 .... can you please say me where is REFESH-Button for Build Gradle under Netbeans 8.1? (under IntelliJ was right side above) – erkan kaplan Nov 12 '15 at 11:26
  • On Project tab (left), right click on your project root and select `Reload Project` – José Pereda Nov 12 '15 at 11:28
  • ok thanks...i have added now the missing librarys like charm, mysql, sqlite etc... – erkan kaplan Nov 12 '15 at 11:30
  • Jose, i must comment 2 lines (see build.gradle above), when i want deploy for android. Testet with Netbeans 8.1 ...Otherwiese i get always *GC overhead limit exceeded** error, can not be compiler..Ok it's not a big problem at the moment... – erkan kaplan Nov 12 '15 at 12:45
  • do you have tried with mysq compile lines? may be you will be get same error as i too. – erkan kaplan Nov 12 '15 at 12:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94924/discussion-between-jose-pereda-and-erkan-kaplan). – José Pereda Nov 12 '15 at 12:52