6

I am wanting to create an HTML form for entering/viewing data on localhost.

The data is in a file based H2 database on localhost.

Ideally, I'd like to use only client-side javascript and HTML so that the user does not need to run a local web server.

I have found some information here on how to connect: http://blog.jooq.org/2014/06/06/java-8-friday-javascript-goes-sql-with-nashorn-and-jooq/

but am wondering about the next step of how to integrate the connection/SQL queries into the web form.

I am aware that the use of javascript to connect to a database is usually frowned upon for security reasons, but for this use-case, it will only be accessing data on localhost.

Also, are there any recommended javascript libraries that would make this easier?

var someDatabaseFun = function() {
    var Properties = Java.type("java.util.Properties");
    var Driver = Java.type("org.h2.Driver"); //JDBC interface for H2

    var driver = new Driver();
    var properties = new Properties();

    properties.setProperty("user", "");     // database username
    properties.setProperty("password", ""); // database password

    try {
        var conn = driver.connect(
            "jdbc:h2:~/db", properties);  // connect to database

        // Database code here
    }
    finally {
        try { 
            if (conn) conn.close();
        } catch (e) {}
    }
}

someDatabaseFun();
FGiorlando
  • 1,121
  • 2
  • 12
  • 22
  • That tutorial is for running JavaScript in Nashorn/JVM, not JavaScript on the web. That code cannot be run client-side. – approxiblue Jan 12 '16 at 22:11
  • Is there something similar that could run client side (with the H2 database and JDBC interface also running on localhost?) – FGiorlando Jan 14 '16 at 00:26

2 Answers2

1

Connecting to a java based database like H2 is not easy with a pure javascript solution (despite the fact that H2 exposes itself via JDBC and HTML).
However, there are certainly ways of working with databases in pure-html. These essentially leverage indexeddb and websql storage mechanisms built into the browser. an incomplete list of javascript libraries are discussed here http://nolanlawson.com/2015/09/29/indexeddb-websql-localstorage-what-blocks-the-dom/:

  • Lawnchair
  • PouchDB
  • LocalForage
  • Dexie
  • Lovefield
  • LokiJS
  • AlaSQL
  • MakeDrive
  • ForerunnerDB
  • YDN-DB

These are in addition to working with pure WebSQL. For my purposes, pure WebSQL was the best solution, for example: http://www.tutorialspoint.com/html5/html5_web_sql.htm

I am wiling to lose IE/Firefox compatibility. But there are also options of shiming WebSQL to IndexedDB, for example: http://nparashuram.com/IndexedDBShim/

So, in summary, you can work with SQL client side with pure javascript but H2 is not the best DB to do this with. WebSQL has the advantage that the database is actually stored by the browser as an SQLite file (file based storage is important to my application)

FGiorlando
  • 1,121
  • 2
  • 12
  • 22
0

I'd like to use only client-side javascript and HTML

Where is your JVM going to run? H2 is a Java database. It runs inside a JVM.

  • Are you embedding it in a Java Applet?
  • Are you using Java Web Start?

They would be the only ways I know of to run Java on the client's machine.

Anything else has to connect to a server.


If I were tasked with implementing this, I would run H2 embedded in a Java Applet, then have my javascript talk to the Applet. It's very clunky though, and only keeps data in memory. Why not just keep all your data in javascript arrays?

Stewart
  • 17,616
  • 8
  • 52
  • 80
  • H2 provides a local JDBC interface via the local JRE. The user will have java installed on their local machine – FGiorlando Jan 14 '16 at 00:24
  • So you have H2 running as a server on the machine `localhost`? Is your question asking what the connection URL for this would be? – Stewart Jan 14 '16 at 01:57
  • the main question is if it's possible to connect to H2 using client-side javascript and then set up a webform for viewing/entering data – FGiorlando Jan 15 '16 at 04:32
  • Well, regardless of where (on which machine) H2 is running, it is still running as a server. Therefore you are connecting to a server. Therefore this question applies. MySQL is no different to H2, as far as JDBC connectivity is concerned. http://stackoverflow.com/questions/3020751/can-javascript-connect-with-mysql – Stewart Jan 15 '16 at 09:22