如何使用Gluon在Android平台上以嵌入式模式连接H2数据库?

时间:2018-07-10 09:49:17

标签: h2 gluon

我想使用H2数据库在我的Gluon Mobile应用程序上存储数据库。我尝试过JDBC,它在桌面版本上运行良好。但是我在Android上失败(org.h2.jdbc.JdbcSQLException:创建文件时出错)。

我拥有如下网址的更改连接,但没有运气 http://www.h2database.com/html/tutorial.html#android

已编辑以添加gradle

我的代码

try {
            Connection connection=null;

            lbError.setText("");
            lbError.setVisible(false);
            System.out.println("Label="+ label.getText());
            //step1 load the driver class
            Class.forName(DB_DRIVER).newInstance();
            //step2 create  the connection object

             if(Platform.isDesktop())
                 connection=DriverManager.getConnection(DB_CONNECTION,DB_USER,DB_PASSWORD);
             if(Platform.isAndroid())
                connection = DriverManager.getConnection(DB_CONNECTION_ANDROID,DB_USER,DB_PASSWORD);

            System.out.print("DATABASE Create");
            //step3 create the statement object
            Statement statement = connection.createStatement();
            String sql="DROP TABLE IF EXISTS contact;\n" +
                        "CREATE TABLE contact(\n" +
                        "        Id INT NOT NULL AUTO_INCREMENT,\n" +
                        "        Name VARCHAR(255) NOT NULL,\n" +
                        "        Email VARCHAR(255) NOT NULL UNIQUE,\n" +
                        "        PRIMARY KEY (ID)\n" +
                        ");";
            System.out.println("SQL="+"\n"+sql);

            //step4 execute query
            statement.executeUpdate(sql);
            System.out.println("Table Created");
        } catch (Exception e) {

            System.out.println("Error Line is here");
            lbError.setText(e.toString());
            lbError.setVisible(true);
            e.printStackTrace();
        }

堆栈跟踪:

07-10 10:30:38.770 30397 30435 W System.err: org.h2.jdbc.JdbcSQLException: Error while creating file "/data/data/com.khmerdev.views" [90062-196]
07-10 10:30:38.771 30397 30435 W System.err:    at org.h2.store.fs.FilePathDisk.createDirectory(FilePathDisk.java:274)
07-10 10:30:38.772 30397 30435 W System.err:    at org.h2.store.fs.FileUtils.createDirectory(FileUtils.java:42)
07-10 10:30:38.772 30397 30435 W System.err:    at org.h2.store.fs.FileUtils.createDirectories(FileUtils.java:309)
07-10 10:30:38.772 30397 30435 W System.err:    at org.h2.store.fs.FileUtils.createDirectories(FileUtils.java:308)
07-10 10:30:38.772 30397 30435 W System.err:    at org.h2.mvstore.db.MVTableEngine.init(MVTableEngine.java:74)
07-10 10:30:38.772 30397 30435 W System.err:    at org.h2.engine.Database.getPageStore(Database.java:2476)
07-10 10:30:38.772 30397 30435 W System.err:    at org.h2.engine.Database.open(Database.java:697)
07-10 10:30:38.772 30397 30435 W System.err:    at org.h2.engine.Database.openDatabase(Database.java:276)
07-10 10:30:38.772 30397 30435 W System.err:    at org.h2.engine.Database.<init>(Database.java:270)
07-10 10:30:38.772 30397 30435 W System.err:    at org.h2.engine.Engine.openSession(Engine.java:64)
07-10 10:30:38.773 30397 30435 W System.err:    at org.h2.engine.Engine.openSession(Engine.java:176)
07-10 10:30:38.773 30397 30435 W System.err:    at org.h2.engine.Engine.createSessionAndValidate(Engine.java:154)
07-10 10:30:38.773 30397 30435 W System.err:    at org.h2.engine.Engine.createSession(Engine.java:137)
07-10 10:30:38.773 30397 30435 W System.err:    at org.h2.engine.Engine.createSession(Engine.java:27)
07-10 10:30:38.773 30397 30435 W System.err:    at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:354)
07-10 10:30:38.773 30397 30435 W System.err:    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:116)
07-10 10:30:38.773 30397 30435 W System.err:    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:100)
07-10 10:30:38.773 30397 30435 W System.err:    at org.h2.Driver.connect(Driver.java:69)
07-10 10:30:38.773 30397 30435 W System.err:    at java.sql.DriverManager.getConnection(DriverManager.java:569)
07-10 10:30:38.773 30397 30435 W System.err:    at java.sql.DriverManager.getConnection(DriverManager.java:219)
07-10 10:30:38.773 30397 30435 W System.err:    at com.khmerdev.views.ContactPresenter.initDB(ContactPresenter.java:75)
07-10 10:30:38.773 30397 30435 W System.err:    at com.khmerdev.views.ContactPresenter.initialize(ContactPresenter.java:53)
07-10 10:30:38.774 30397 30435 W System.err:    at java.lang.reflect.Method.invoke(Native Method)
07-10 10:30:38.774 30397 30435 W System.err:    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
07-10 10:30:38.774 30397 30435 W System.err:    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:277)
07-10 10:30:38.774 30397 30435 W System.err:    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
07-10 10:30:38.774 30397 30435 W System.err:    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
07-10 10:30:38.774 30397 30435 W System.err:    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
07-10 10:30:38.774 30397 30435 W System.err:    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
07-10 10:30:38.774 30397 30435 W System.err:    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
07-10 10:30:38.774 30397 30435 W System.err:    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
07-10 10:30:38.774 30397 30435 W System.err:    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
07-10 10:30:38.774 30397 30435 W System.err:    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
07-10 10:30:38.774 30397 30435 W System.err:    at com.khmerdev.views.ContactView.getView(ContactView.java:11)
07-10 10:30:38.774 30397 30435 W System.err:    at com.khmerdev.GluonApplication.lambda$init$0(GluonApplication.java:19)
07-10 10:30:38.775 30397 30435 W System.err:    at com.khmerdev.GluonApplication$$Lambda$1.get(GluonApplication.java)
07-10 10:30:38.775 30397 30435 W System.err:    at com.gluonhq.impl.charm.a.a.a.a(SourceFile:1032)
07-10 10:30:38.775 30397 30435 W System.err:    at com.gluonhq.charm.glisten.application.MobileApplication.a(SourceFile:721)
07-10 10:30:38.775 30397 30435 W System.err:    at com.gluonhq.charm.glisten.application.MobileApplication.switchView(SourceFile:475)
07-10 10:30:38.775 30397 30435 W System.err:    at com.gluonhq.charm.glisten.application.MobileApplication.switchView(SourceFile:450)
07-10 10:30:38.775 30397 30435 W System.err:    at com.gluonhq.charm.glisten.application.MobileApplication.a(SourceFile:266)
07-10 10:30:38.775 30397 30435 W System.err:    at com.gluonhq.charm.glisten.application.MobileApplication.start(SourceFile:232)
07-10 10:30:38.775 30397 30435 W System.err:    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$145(LauncherImpl.java:863)
07-10 10:30:38.775 30397 30435 W System.err:    at com.sun.javafx.application.LauncherImpl.access$lambda$8(LauncherImpl.java)
07-10 10:30:38.775 30397 30435 W System.err:    at com.sun.javafx.application.LauncherImpl$$Lambda$9.run(Unknown Source)
07-10 10:30:38.775 30397 30435 W System.err:    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$158(PlatformImpl.java:326)
07-10 10:30:38.775 30397 30435 W System.err:    at com.sun.javafx.application.PlatformImpl.access$lambda$6(PlatformImpl.java)
07-10 10:30:38.775 30397 30435 W System.err:    at com.sun.javafx.application.PlatformImpl$$Lambda$7.run(Unknown Source)
07-10 10:30:38.775 30397 30435 W System.err:    at com.sun.javafx.application.PlatformImpl.lambda$null$156(PlatformImpl.java:295)
07-10 10:30:38.776 30397 30435 W System.err:    at com.sun.javafx.application.PlatformImpl.access$lambda$18(PlatformImpl.java)
07-10 10:30:38.776 30397 30435 W System.err:    at com.sun.javafx.application.PlatformImpl$$Lambda$19.run(Unknown Source)
07-10 10:30:38.777 30397 30435 W System.err:    at java.security.AccessController.doPrivileged(AccessController.java:57)
07-10 10:30:38.777 30397 30435 W System.err:    at com.sun.javafx.application.PlatformImpl.lambda$runLater$157(PlatformImpl.java:294)
07-10 10:30:38.777 30397 30435 W System.err:    at com.sun.javafx.application.PlatformImpl.access$lambda$5(PlatformImpl.java)
07-10 10:30:38.778 30397 30435 W System.err:    at com.sun.javafx.application.PlatformImpl$$Lambda$6.run(Unknown Source)
07-10 10:30:38.778 30397 30435 W System.err:    at com.sun.glass.ui.monocle.RunnableProcessor.runLoop(RunnableProcessor.java:93)
07-10 10:30:38.778 30397 30435 W System.err:    at com.sun.glass.ui.monocle.RunnableProcessor.run(RunnableProcessor.java:52)
07-10 10:30:38.778 30397 30435 W System.err:    at java.lang.Thread.run(Thread.java:762)

Build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.javafxports:jfxmobile-plugin:1.3.12'
    }
}

apply plugin: 'org.javafxports.jfxmobile'

repositories {
    jcenter()
    maven {
        url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
    }
}

mainClassName = 'com.khmerdev.GluonApplication'

dependencies {
    compile 'com.gluonhq:charm:5.0.0'
    compile files('libs/h2-1.4.196.jar')
}

jfxmobile {
    downConfig {
        version = '3.8.0'
        // Do not edit the line below. Use Gluon Mobile Settings in your project context menu instead
        plugins 'display', 'lifecycle', 'statusbar', 'storage'
    }
    android {
        manifest = 'src/android/AndroidManifest.xml'

    }
    ios {
        infoPList = file('src/ios/Default-Info.plist')
        forceLinkClasses = [
                'com.khmerdev.**.*',
                'com.gluonhq.**.*',
                'javax.annotations.**.*',
                'javax.inject.**.*',
                'javax.json.**.*',
                'org.glassfish.json.**.*'
        ]
    }
}

请帮助我。

1 个答案:

答案 0 :(得分:0)

感谢JoséPereda给我一些想法,现在我找到了解决上述问题的方法。我们需要首先通过以下代码获取正确的私有存储:

File dir = Services.get(StorageService.class)
                    .map(s -> s.getPrivateStorage().get())
                    .orElseThrow(() -> new IOException("Error: PrivateStorage not available"));

以上代码的输出应为/data/user/0/com.khmerdev/files。然后,我们需要使用此目录与dbUrl串联。请参见以下示例:

String url = "jdbc:h2:/data/user/0/com.khmerdev/files" +
    "/test" +
    ";FILE_LOCK=FS" +
    ";PAGE_SIZE=1024" +
    ";CACHE_SIZE=8192";

在上面的示例中,“ test”是示例数据库名称。因此,我们需要将此字符串url与JDBC一起使用,以进行数据库事务。