B4J Tutorial H2 Database

This is a very quick tutorial on the H2 database - https://www.h2database.com/html/main.html

The main features of H2 are:
  • Very fast, open source, JDBC API
  • Embedded and server modes; in-memory databases
  • Browser based Console application
  • Small footprint: around 2 MB jar file size
Other advantages over SQLite are;
  • Its pure Java so JDBC driver is only 2Mb vs SQLite 7mb (since SQLite has platform specific binaries)
  • Encryption is out of the box
  • Richer feature set - users, roles etc
  • RDBMS compatability modes (eg run in Postgres mode - this means you can migrate to a full blown RDBMS with very little work in the future)
Feature comparison;

1626641272715.png


There are some speed comparisions with SQLite here https://www.b4x.com/android/forum/threads/jserver-sqlite-multiple-request-stress-test.130575 (in my tests its about 600% faster than SQLite in WAL mode).

To use you need to download the jar file and add to additional jars in your project;

B4X:
#AdditionalJar: h2-1.4.200

Conenction examples...

1. Embedded Mode

B4X:
Dim driver As String = "org.h2.Driver"
Dim url As String = $"jdbc:h2:C:\temp\mydatabase"$
DB.Initialize2(driver,url,"sa","sa")

2. Embedded In memory database

B4X:
DBtemp.Initialize2("org.h2.Driver","jdbc:h2:mem:mydatabase","sa","sa")

3. Connect to remote server (in Potsgres Mode)

B4X:
Dim driver As String = "org.h2.Driver"
Dim url As String = "jdbc:h2:tcp://db_host/~/mydatabase;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE"
DBdest.Initialize2(driver,url,"sa","sa")

To run the database in server mode there is a start up batch file called h2w.bat in the download from the H2 web site.

4. Encrypted database connection

B4X:
Dim DB As SQL
  
Dim driver As String = "org.h2.Driver"
Dim url As String = $"jdbc:h2:${File.DirApp}\report.h2;CIPHER=AES"$
DB.Initialize2(driver,url,"user","cipherpassword userpassword")
  
DB.ExecNonQuery("CREATE TABLE table1 (col1 TEXT , col2 INTEGER, col3 INTEGER)")
  
DB.Close
 
Last edited:

MichalK73

Well-Known Member
Licensed User
Longtime User
Links on the jar files download page do not work. Neither sourceforge nor Maven. Can you somehow share your jar version?
 

MichalK73

Well-Known Member
Licensed User
Longtime User
I just turned the H2 into my reworked project. Works approx.
For now I can write everything from my finger, but I would use GUI to H2 with encryption support from time to time. A few checked it either they do not have encryption support or they are paid. I'm looking for something simple, cheap or free. Do you know anything?
 

tchart

Well-Known Member
Licensed User
Longtime User
I just turned the H2 into my reworked project. Works approx.
For now I can write everything from my finger, but I would use GUI to H2 with encryption support from time to time. A few checked it either they do not have encryption support or they are paid. I'm looking for something simple, cheap or free. Do you know anything?

Have you tried DBeaver? It just uses JDBC so the GUI shouldn't care if the connection is encrypted or not. Just use the same connection string you are using the B4J. DBeaver is very clever, it will even download the driver for you. And it's free...

 

MichalK73

Well-Known Member
Licensed User
Longtime User
I didn't know this program. He downloaded the H2 driver, but I don't know how to set the ODBC connection to the encrypted database.
Ok it worked. You need to add a connection parameter.
h2ok.png
 
Last edited:

wimpie3

Well-Known Member
Licensed User
Longtime User
I've used H2 a lot in the past for my .net development. Does anyone know if there will be a version for Android some day?
 

MichalK73

Well-Known Member
Licensed User
Longtime User
Hey.

I have a problem with the new v.2.x H2 version.
The problem is that it doesn't accept the syntax I use for all databases with B4X.
The old checked syntax for DBM:

B4X:
Dim ww As List = DBM.SQLSelect(DBM.GetSQL, "select * from ? limit 3", Array As String(ItemId))

Generates an error.
B4X:
(Exception) java.lang.Exception:  org.h2.jdbc.JdbcSQLSyntaxErrorException: Błąd składniowy w wyrażeniu SQL "SELECT * FROM ?[*] LIMIT 3"; oczekiwano "identifier"
Syntax error in SQL statement "SELECT * FROM ?[*] LIMIT 3"; expected "identifier"; SQL statement:
select * from ? limit 3 [42001-206]
As you can see there is no substitution instead of ? variable.
However, if I write it like this:
B4X:
Dim ww As List = DBM.SQLSelect(DBM.GetSQL, $"select * from ${ItemId} limit 3"$, Null)

Everything is working properly.
How to revert to the old method of setting variables as it was in the old version of H2?
Maybe some parameter of database setting is responsible for that?
 
Top