RH2: Connect to H2 Database in R

2019-08-06 16:07发布

问题:

I tried to connect with RH2 to a H2 (1.4.181) Database. I've tried different things, like reinstalling R, and fixing potential problems with rJava on Ubuntu. However following error does not disappear:

> library("RH2")
Loading required package: chron
Loading required package: RJDBC
Loading required package: DBI
Loading required package: rJava
> con <- dbConnect(H2(driverClass="org.h2.Driver", jars =    "~/h2-1.4.181.jar"), "jdbc:h2:~/test", "sa", "")
Error in is(object, Cl): error in evaluating the argument 'drv' in selecting a method for function   'dbConnect': Error in .jfindClass(as.character(driverClass)[1]) : class not found

Does anyone know how to solve this or did I make a mistake in the syntax?

回答1:

Instead of RH2 you can use RJDBC directly. This would be the corresponding command:

con <- dbConnect( JDBC('org.h2.Driver', '~/h2-1.4.181.jar') , 'jdbc:h2:~/test', 'sa', '')

Note that, in Windows, using ~ for the driver path gave me an error. But it works if I substitute the jar location string for an absolute path, like 'C:/h2-1-4.181.jar'. Note that although the error I got was similar to yours, I can't say that that would have fixed RH2 as I was unable to use that for some other reason.

For more information on RJDBC see: https://www.rforge.net/RJDBC/index.html



回答2:

I think the jars argument is broken. My guess is that it isn't pre-pending.

I fixed it (on Mac OS X) by replacing the old H2 jar in my package library with an updated one. Specifically:

 box:java nmvanhoudnos$ pwd
 /Users/nmvanhoudnos/Library/R/3.2/library/RH2/java
 box:java nmvanhoudnos$ ls
 h2-1.3.175.jar
 box:java nmvanhoudnos$ mv h2-1.3.175.jar h2-1.3.175.old
 box:java nmvanhoudnos$ cp ~/workspace/defectprediction/h2*.jar .
 box:java nmvanhoudnos$ ls
 h2-1.3.175.old h2-1.4.184.jar

Where you will need to make the appropriate changes for your OS, filesystem, and location of the update H2 jar.

Once you have the new jar file in place, you can test that it works in R as follows:

> library(RH2)
> con <- dbConnect(H2())
> s <- "select VALUE from INFORMATION_SCHEMA.SETTINGS where NAME = 'info.VERSION'"
> dbGetQuery(con, s)
                 VALUE
1 1.4.184 (2014-12-19)

As expected.



标签: r h2 rjdbc