Connecting to oracle 10g database through R

蓝咒 提交于 2020-01-05 05:31:10

问题


I want to import data from SQL databases through R.I tried too many times(almost 6 hrs) connecting to the server. I ran these command and it shows error:-

jdbcDriver <- JDBC(driverClass="oracle.jdbc.OracleDriver", classPath="ojdbc6.jar")

jdbcConnection <- dbConnect(jdbcDriver, "jdbc:oracle:thin:@//database.desktop-65l5f3s:1521/orcl", "username", "password") Error in .jcall(drv@jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1], : java.sql.SQLRecoverableException: IO Error: Unknown host specified

I think there is a problem of driver but I am unable to resolve it.


回答1:


Below is what I did to connect R to the Oracle database

  1. Download the Oracle driver JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html
  2. Install RJDBC

then run the code below in r

library(RJDBC)
## Loading required package: DBI
## Loading required package: rJava
# specify the driver type and location in your machine

jdbcDriver = JDBC("oracle.jdbc.OracleDriver",
                  classPath="C:/app/techsupport/product/11.2.0/client_1/jdbc/lib/ojdbc6.jar") # may be different on your machine, it's based on your JDK installation

# connect to the database

conn = dbConnect(jdbcDriver, "jdbc:oracle:thin:@//database.hostname.com:port/service_name",  # get the database name, port, and service name from you DBA
             user = YourUserName,
             password = YourPassword)

# get office table using a query string

startTime <- Sys.time() # start timer
OfficeTable = dbGetQuery(conn, "select * from office")
# disconnect after working with database
dbDisconnect(conn)
## [1] TRUE
Sys.time() - startTime  # calc time to import table
## Time difference of 0.1630161 secs
# take a look at the data
str(OfficeTable)
## 'data.frame':    373 obs. of  22 variables:


来源:https://stackoverflow.com/questions/47097850/connecting-to-oracle-10g-database-through-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!