How to connect JDBC (H2 Database) in PHP?

Asked

Viewed 739 times

4

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

  • 2

    I voted to leave this question open, because it is not something so simple to do (at least as far as I researched) and even though the question sounds "broad" in fact it is not easy to find by "where to start".

  • Actually I don’t think it sounds like broad. It’s just hard to find something related to the subject.

  • 2

    Actually my comment wasn’t for you, you received 4 votes of closure on the question as being too wide, I put the comment in an area called the analysis queue, for others who see your question think before closing. It’s more of an endorsement to keep your question open :)

1 answer

4


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 should support PDO or test with Postgresql PDO

I have no way to test H2 here, but if the link is correct, then you can use the PDO for Postgresql. At the moment the banks supported by 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 pro driver H2 Database, but as said earlier you can try the driver Postgresql, according to the link itself H2 Database.

Something like:

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

If this is possible, there’s likely to be no full compatibility and from what I’ve researched, it looks like it’s a very difficult job and hardly anyone has managed to do this.

Another detail, I’m not sure, but I believe the H2 database is supported by ODBC and maybe I can use it with PDO like this.

Out this you can try to use the https://github.com/webdevelopersdiary/jamp which is a platform PHP independent with support for java databases (java?)

To set up 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 it with filename should be the relative path to a file (in relation to the 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 URL H2 JDBC see the H2 Database resource list.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.