通过R连接到oracle 10g数据库

时间:2017-11-03 14:14:01

标签: sql r oracle jdbc oracle10g

我想通过R.从SQL数据库导入数据。我尝试了太多次(差不多6小时)连接到服务器。我运行了这些命令并显示错误: -

  

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

     

jdbcConnection< - dbConnect(jdbcDriver," jdbc:oracle:thin:@ // database.desktop-65l5f3s:1521 / orcl"," username","密码&#34)   .jcall中的错误(drv @ jdrv," Ljava / sql / Connection;"," connect",as.character(url)[1],:     java.sql.SQLRecoverableException:IO错误:指定了未知主机

我认为有驱动程序问题,但我无法解决。

1 个答案:

答案 0 :(得分:0)

下面是我将R连接到Oracle数据库的方法

  1. 下载Oracle驱动程序JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html
  2. 安装RJDBC

然后在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: