R: JDBC() not finding Java drivers path when conne

2019-05-27 09:45发布

问题:

I'm trying to connect to Teradata through RStudio, but for some reason JDBC function has problems recognizing the path where Java drivers sit. See the code below:

library(RODBC)
library(RJDBC)
library(rJava)

# both Java drivers definitely exist 
file.exists('/Users/KULMAK/Documents/TeraJDBC__indep_indep.16.10.00.03/tdgssconfig.jar')
[1] TRUE

file.exists('/Users/KULMAK/Documents/TeraJDBC__indep_indep.16.10.00.03/terajdbc4.jar')
[1] TRUE

But when I paste those paths in JDBC call...

# allow more elaborated error messages to appear 
.jclassLoader()$setDebug(1L) 

drv = JDBC("com.teradata.jdbc.TeraDriver","/Users/KULMAK/Documents/TeraJDBC__indep_indep.16.10.00.03/tdgssconfig.jar;/Users/KULMAK/Documents/TeraJDBC__indep_indep.16.10.00.03/terajdbc4.jar")

... I get the following error:

RJavaClassLoader: added '/Users/KULMAK/Documents/TeraJDBC__indep_indep.16.10.00.03/tdgssconfig.jar;/Users/KULMAK/Documents/TeraJDBC__indep_indep.16.10.00.03/terajdbc4.jar' to the URL class path loader WARNING: the path '/Users/KULMAK/Documents/TeraJDBC__indep_indep.16.10.00.03/tdgssconfig.jar;/Users/KULMAK/Documents/TeraJDBC__indep_indep.16.10.00.03/terajdbc4.jar' does NOT exist, it will NOT be added to the internal class path! RJavaClassLoader: added '/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RJDBC/java/RJDBC.jar' to the URL class path loader RJavaClassLoader: adding Java archive file '/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RJDBC/java/RJDBC.jar' to the internal class path RJavaClassLoader@3d4eac69.findClass(com.teradata.jdbc.TeraDriver) - URL loader did not find it: java.lang.ClassNotFoundException: com.teradata.jdbc.TeraDriver RJavaClassLoader.findClass("com.teradata.jdbc.TeraDriver") - trying class path "/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/java" Directory, can get '/Library/Frameworks/R.framework/Versions/3.4/Resources/library/rJava/java/com/teradata/jdbc/TeraDriver.class'? NO - trying class path "/Library/Frameworks/R.framework/Versions/3.4/Resources/library/RJDBC/java/RJDBC.jar" JAR file, can get 'com/teradata/jdbc/TeraDriver'? NO

ClassNotFoundException Error in .jfindClass(as.character(driverClass)[1]) : class not found

Running the same code in R, rather than RStudio, returns the same error.

Also, re-installing RJDBC package (as suggested here) didn't solve the issue.

Can anyone explain why this is happening? Thanks for help.

Here's my session info:

> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.3

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] devtools_1.13.4 RJDBC_0.2-7     rJava_0.9-9     DBI_0.8         RODBC_1.3-15   
[6] dplyr_0.7.4     readr_1.1.1    

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.15     bindr_0.1        magrittr_1.5     hms_0.3          R6_2.2.2        
 [6] rlang_0.1.6      httr_1.3.1       tools_3.4.1      git2r_0.19.0     withr_2.1.1.9000
[11] yaml_2.1.16      assertthat_0.2.0 digest_0.6.15    tibble_1.4.2     bindrcpp_0.2    
[16] curl_3.0         memoise_1.1.0    glue_1.2.0       compiler_3.4.1   pillar_1.1.0    
[21] pkgconfig_2.0.1

回答1:

That's a mistake in the path - you have inadvertently pasted two paths together (note the semicolon between the paths). You probably intended

drv <- JDBC("com.teradata.jdbc.TeraDriver",
  c("/Users/KULMAK/Documents/TeraJDBC__indep_indep.16.10.00.03/tdgssconfig.jar",
    "/Users/KULMAK/Documents/TeraJDBC__indep_indep.16.10.00.03/terajdbc4.jar"))

note that you probably can make your life easier by simply using

drv <- JDBC("com.teradata.jdbc.TeraDriver", Sys.glob("/Users/KULMAK/Documents/TeraJDBC__indep_indep.16.10.00.03/*.jar"))


回答2:

This worked for me. Just make sure that both jars are located in the referenced directory.

library(RJDBC)
drv <- RJDBC::JDBC(driverClass = "com.teradata.jdbc.TeraDriver", classPath =  Sys.glob("~/drivers/teradata/*"))
conn <- dbConnect(drv,'jdbc:teradata://<server>/<db>',"un","pw")
result.df<- dbGetQuery(conn,"select * from table")