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, you'll need to use Cordova CLI instead of VoltBuilder. Since it is based on Java, it is Android only.

The plugin provides a generic interface for using JDBC libraries with a Cordova 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 Corodova CLI toolchain is installed properly. Documentation for installing it is in the Install Tools section are 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" />

2. Go into Preferences/VoltBuilder and set 'Build Command' to cordova build

3. Save your project.

4. From the run menu, choose Make Native App with Cordova CLI.


AppStudio should now have created a cordova folder in your project directory. This will contain all the build files for Cordova CLI. Each time you choose Make Native App with Cordova 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 No platforms added to this project, open up a cmd window in the cordova folder and add Android as a platform:

cordova platform add android

2. Add the plugin: From a command window in your cordova folder, enter

cordova plugin add cordova-plugin-jdbc

3. Repeat Make Native App with Cordova CLI.

4. Add the following folders to the cordova directory.

  • spash: your splash screens.
  • icons: your icons.
  • libs: Put your JDBC driver here. It will be called something like jtopenlite.jar.
  • 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)));

5. To check if the Cordova toolchain is OK, by using the requirements command:

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

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


Button1.onclick = function() {
    jdbc.load("com.ibm.jtopenlite.database.jdbc.JDBCDriver", loadSuccess, loadFail);

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

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

Function Button1_onclick()
  jdbc.load("com.ibm.jtopenlite.database.jdbc.JDBCDriver", loadSuccess, loadFail)
End Function

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

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


Cordova apps are generally not difficult to decompile. This means that your database host, name, user, and password could easily be exposed by a knowledgable person if your app is public. If you use this plugin to access sensitive data, it's very important you restrict the rights of the database user so they can only perform the bare minimum of tasks needed for the app to function. You should assume that curious, or perhaps malicious people may connect to your database without using your app. Secure your data accordingly.

Additionally, the execute method has no protection against SQL injection. Be sure to sanitize your input appropriately for the underlying database.