How to connect JDBC (H2 Database) in PHP?

Question:

How to connect a JDBC database (specifically H2 Database) in PHP?
The database is in a single file with a .db extension

Answer:

According to the link http://www.h2database.com/html/roadmap.html :

PHP support: H2 should support PDO, or test with PostgreSQL PDO.

PHP support: H2 must support PDO or test with PostgreSQL PDO

I can't test H2 here, but if the link is correct then you can use PDO for PostgreSQL. Currently the banks supported by the PDO are:

  • CUBRID
  • MS SQL Server
  • firebird
  • IBM
  • Informix
  • MySQL
  • MS SQL Server
  • Oracle
  • ODBC and DB2
  • PostgreSQL
  • SQLite
  • 4D

And there is no specific driver for H2 Database , but as said before you can try PostgreSQL driver , as the H2 Database link itself .

Something like:

$db = new PDO('pgsql:dbname=mydb;host=localhost;user=usuario;password=senha');

If this is possible, it is likely that there is not full compatibility and from what I have researched, it seems that it is a very difficult job and hardly anyone has managed to do this.

Another detail, I'm not sure, but I believe that h2 database is supported by ODBC and maybe I can use it with PDO like that.

Other than that you can try using https://github.com/webdevelopersdiary/jamp which is a platform independent PHP with support for java databases ( from java? )

To configure jdbc with h2 database go to src/main/webapp/WEB-INF/jetty-env.xml and change the line:

<Set name="url">jdbc:h2:mem:database;IGNORECASE=TRUE;MODE=MYSQL</Set>

for

<Set name="url">jdbc:h2:file:filename;IGNORECASE=TRUE;MODE=MYSQL</Set>

Where you replaced with filename should be the relative path to a file (relative to pom.xml ) or an absolute path to a file (in case of absolute something like jdbc:h2:file:c:\users\usuario\desktop\banco;IGNORECASE=TRUE;MODE=MYSQL ). For more information on the H2 JDBC URL see the H2 Database resource list .

Even if you can connect, I still believe that this is not a good idea for the production environment , due to probable instabilities due to lack of compatibility, so I recommend converting your database to mysql or another database supported by default by PDO.

Scroll to Top