In many circumstances, you might want to connect R directly to a database to store and retrieve data. If the source database is an Oracle database, you have a number of options:
Using ROracle should theoretically provide you with the best performing client, as this library is a wrapper around the Oracle OCI driver. The OCI driver, however, is platform-specific and requires you to install Oracle database client software.
What if you don’t want to write code that is either platform-specific or requires relatively complex, platform-specific installation steps? In this case, you should consider using RJDBC.
I’ll assume that you have a JRE/JDK installed and know the path to your JAVA_HOME.
- Hostname or IP, e.g., database.company.com
- Port, e.g., 1521
- Service name or SID, e.g., ORCL
- Username
- Password
This information will allow us to construct the DSN, which will look something like this: jdbc:oracle:thin:@//hostname:port/service_name_or_sid
Armed with this DSN and your Java home, you should now be able to modify and execute the example below.
# 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) |
Hi Michael,
I followed this process step by step but I am getting this error –
Error in .jinit() : Cannot create Java virtual machine (-1)
Any resolution for this?
Thanks
Harmeek
Is
@
short for something? Or is it literally the symbol@
?It’s literally the symbol @
can u help me with this http://stackoverflow.com/questions/24691603/r-rjdbc-java-lang-outofmemoryerror
Thank you. I followed your instruction and it worked well (only took 5 minutes to read and use successfully)!!
Thanks a lot. It worked. :)
For those stuck on Windows and/or have yet to install the packages:
install.packages(‘rJava’)
install.packages(‘RJDBC’)
Sys.setenv(JAVA_HOME=’C:\\Users\\username\\Documents\\bin\\Java\\jdk1.x.x_xx’)
Brilliant! I just wanted to say a BIG ‘Thank you” for solving my connection to Oracle problems. I had previously installed the free version of the DBvisualizer tool which came with a number of drivers (available from http://www.dbvis.com/ ).
I had to make a couple of minor changes to spell out the full path to the relevant files:
FROM Sys.setenv(JAVA_HOME=’/path/to/java_home’)
TO Sys.setenv(JAVA_HOME=’C:/ProgramData/Oracle/Java/javapath’)
FROM classPath=”lib/ojdbc6.jar”
TO classPath=”c:/Program Files/DbVisualizer/jdbc/oracle/ojdbc6.jar”
Then it worked perfectly. Cheers, mate!