简体   繁体   中英

How to Connect R to Oracle DB using RJDBC

I am trying to connect R to my Oracle database but keep getting the following error whenever I call the following function:

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

I have 2 files, a functions.r file and my working.r file. The following is what I have in each file.

functions.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)
}

and my working.r file

#
library(tidyverse)
library(RJDBC)
library(rJava)
library(ggplot2)
library(dplyr)

source("functions.R")

df <- Get_Data()

whenever I run Get_Data I get the error message. I am not sure where I am going wrong, would really appreciate it if somebody could advise as I am new to R and would really love it if somebody could help.


Nirmala, after making the changes you requested, I commented the following section out in my code:

## 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 = "")

and then changed the connection line to:

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

but this now gives me the following error:

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

even though the username/password is correct and works as I am able to connect to the database in sql developer.

It may be the issue with the way you are passing the connection URL. You are mixing both long form and easy connect URL. Can you just use URL as shown here and try it out?

jdbc:oracle:thin:@myhost:1521/myorcldbservicename

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM