如何使用RJDBC将R连接到Oracle数据库



我正试图将R连接到我的Oracle数据库,但每当我调用以下函数时,都会收到以下错误:

Error in .jcall(drv@jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1],  : 
java.sql.SQLException: NL Exception was generated

我有两个文件,一个函数.r文件和我的工作.r文件。以下是我在每个文件中的内容。

函数.r

caesi <- function(DB){
## Create an Oracle connection to a
## remote database using the SID in the connect string.
drv <- JDBC(driverClass="oracle.jdbc.OracleDriver", classPath="E:/filelocation/ojdbc6.jar")
## user details 
username = "USERNAME" ## Your user name
password = "PASSWORD" ## Your password
## Refer to Oracle Database Net Services Administator's Guide for
## details on connect string specification.
## Current connection string this will change when we replatform
host <- "LOCALHOST"
port <- 1521
svc <- "rtreports"
connect.string <- paste("(DESCRIPTION=", " (ADDRESS= (PROTOCOL=TCP)(HOST= ",host,")(PORT=",port,"))","(CONNECT_DATA= (SERVICE_NAME=",svc,")))",sep = "")
## Use username/password authentication.
ErrReturn <- 0  
return({
con <-dbConnect(drv,paste0("jdbc:oracle:thin:@localhost:1521:orcll",connect.string),username, password)
})
} 
Get_Data = function(){
ch_or <- caesi("DB")
data <- dbGetQuery(ch_or, paste0(("SELECT * FROM MYTABLE")))
dbDisconnect(ch_or) 
outcoords <- data
return(outcoords)
}

和我的工作.r文件

#
library(tidyverse)
library(RJDBC)
library(rJava)
library(ggplot2)
library(dplyr)
source("functions.R")
df <- Get_Data()

每当我运行Get_Data时,我都会收到错误消息。我不确定我哪里出了问题,如果有人能给我建议,我将不胜感激,因为我是R的新手,如果有人为我提供帮助,我将非常高兴。


Nirmala,在做出您要求的更改后,我在代码中评论了以下部分:

## Current connection string this will change when we replatform
##host <- "LOCALHOST"
##port <- 1521
##svc <- "rtreports"
##connect.string <- paste("(DESCRIPTION=", " (ADDRESS= (PROTOCOL=TCP)(HOST= ",host,")(PORT=",port,"))","(CONNECT_DATA= (SERVICE_NAME=",svc,")))",sep = "")

然后将连接线更改为:

con <-dbConnect(drv,paste0("jdbc:oracle:thin:@localhost:1521:orcll"),username, password)

但这现在给了我以下错误:

Error in .jcall(drv@jdrv, "Ljava/sql/Connection;", "connect", as.character(url)[1],  : 
java.sql.SQLException: ORA-01017: invalid username/password; logon denied

即使用户名/密码是正确的,并且可以在sql开发人员中连接到数据库。

这可能是您传递连接URL的方式有问题。您混合了长格式和简单连接URL。你可以只使用这里显示的URL并尝试一下吗?

jdbc:oracle:shin:@myhost:1521/myorcldservicename

相关内容

  • 没有找到相关文章

最新更新