-
-
Save huangrh/30f2edb28a86c6bb6f30aaff83538afb to your computer and use it in GitHub Desktop.
Example of connecting to an Oracle database using R and RJDBC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set JAVA_HOME, set max. memory, and load rJava library | |
Sys.setenv(JAVA_HOME='/path/to/java_home') | |
options(java.parameters="-Xmx2g") | |
library(rJava) | |
# Output Java version | |
.jinit() | |
print(.jcall("java/lang/System", "S", "getProperty", "java.version")) | |
# Load RJDBC library | |
library(RJDBC) | |
# Create connection driver and open connection | |
jdbcDriver <- JDBC(driverClass="oracle.jdbc.OracleDriver", classPath="lib/ojdbc6.jar") | |
jdbcConnection <- dbConnect(jdbcDriver, "jdbc:oracle:thin:@//database.hostname.com:port/service_name_or_sid", "username", "password") | |
# Query on the Oracle instance name. | |
instanceName <- dbGetQuery(jdbcConnection, "SELECT instance_name FROM v$instance") | |
print(instanceName) | |
# Close connection | |
dbDisconnect(jdbcConnection) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment