JDBC Plugin

From NSB App Studio
Jump to navigation Jump to search

This plugin allows you to execute simple queries against any database with a JDBC driver using Android. Due to the requirement of adding a driver JAR, it's not compatible with automated build services (like PhoneGap Build). You'll need to use PhoneGap CLI

The plugin provides a generic interface for using JDBC libraries with a PhoneGap plugin. The interface has a hook to a Java driver which does the actual work. JDBC drivers are available for many databases.

The main documentation and files for the plugin are here: https://github.com/arsmentis/cordova-plugin-jdbc. In this document, we will discuss how to use the plugin from AppStudio.

Begin by making sure the PhoneGap CLI toolchain is installed properly. Documentation for installing it is in the Install Tools section of this page: here.

Creating your AppStudio Project

Next, create a new project in AppStudio. You'll need to do a couple of extra steps:

1. Add the hook to configxml: Open configxml in Project Properties. Add the hook declaration just after <platform name="android">

<platform name="android">
  <hook type="after_prepare" src="scripts/copyDriver.js" />
  ...
  </platform>

2. Create a folder in your project directory called scripts. Add a file called copyDriver.js to it with the following contents:

var fs = require('fs');
var path = require('path');

module.exports = function(context) {
  var libsPath = path.join(context.opts.projectRoot, 'libs');
  var platformLibsPath = path.join(context.opts.projectRoot, 'platforms',
                                   'android', 'libs');
  var libs = fs.readdirSync(libsPath);

  libs.forEach(function (lib) {
    console.log('Copying libs/%s to platforms/android/libs...', lib);
    fs.createReadStream(path.join(libsPath, lib))
      .pipe(fs.createWriteStream(path.join(platformLibsPath, lib)));
  });
};

3. Add the scripts folder to your project by dragging it from the Finder to the AppStudio Project Explorer window.

4. Go into Preferences/PhoneGap and set 'Build Command' to cordova build.

5. Save your project.

6. From the run menu, choose Make Native App with PhoneGap CLI.

PhoneGap CLI

AppStudio should now have created a phonegap folder in your project directory. This will contain all the build files for PhoneGap CLI. Each time you choose Make Native App with PhoneGap CLI, these files will be updated with the latest version of your code.

1. If you get a message in the Build window which says about platform not being added, open up a cmd window in the phonegap folder and add Android as a platform:

cordova platform add android

2. If you get other errors, check if the PhoneGap toolchain is OK, by using the requirements commend:

> cordova requirements
Requirements check results for android:
Java JDK: installed .
Android SDK: installed
Android target: installed android-22,android-23
Gradle: installed

3. If the build is successful, the apk file will be in phonegap/platforms/android/build/outputs/apk.

Example (BASIC)

Function Button1_onclick()
  jdbc.load("com.ibm.as400.access.AS400JDBCDriver", loadSuccess, loadFail)
End Function

Sub loadSuccess(data)
  console.log("success", data)
End Sub

Sub loadFail(data)
  console.log("fail", data)
End Sub

AppStudio Code to use JSBC (JavaScript)

Button1.onclick = function() {
    jdbc.load("com.ibm.as400.access.AS400JDBCDriver", loadSuccess, loadFail);
};

function loadSuccess(data) {
    console.log("success", data);
}

function loadFail(data) {
    console.log("fail", data);
}