1

I've this message error when I run my scala code :

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

Here my code :


object ScalaJdbcConnectSelect extends App {
  val url = "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=XXX)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=XXX)(PORT=1521))(FAILOVER=on)(LOAD_BALANCE=on))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XXX)))"
  val driver = "oracle.jdbc.driver.OracleDriver"
  val username = "XXX"
  val password = "XXX"
  var connection:Connection = _
  try {
    Class.forName(driver)
    connection = DriverManager.getConnection(url, username, password)
    val statement = connection.createStatement
    val rs = statement.executeQuery("SELECT * FROM TABLE WHERE ID = 1")
    while (rs.next) {
      val host = rs.getString("ID")
      val user = rs.getString("Field")
      println("ID = %s, Field = %s".format(host,user))
    }
  } catch {
    case e: Exception => e.printStackTrace
  }
  connection.close
}

I download ojdbc6.jar (database oracle version is 11) and in Project Structure > Project Settings > Modules I add my JAR (that appears in Libraries tab) but my error still happen.

I tried to change oracle.jdbc.driver.OracleDriver to oracle.jdbc.OracleDriver but it changes anythings.

I know my database connection's configuration is good because I can connect via DB Browser and test some sql request.

Did I miss something ? I'm new with Scala

LynxWeb
  • 69
  • 1
  • 9
  • How do you run your scala code? The error indicates the driver isn't on the runtime classpath. – Mark Rotteveel Jan 22 '20 at 09:15
  • With the terminal of intellij, I do "scalac ScalaJdbcConnectSelect" and then "scala ScalaJdbcConnectSelect". I already add ojdbc6.jar via ProjectSettings > Dependencies tab and I click on "+" to add my Jar. (I explain it in my post). How can I check what is on the runtime classpath ? – LynxWeb Jan 22 '20 at 13:18
  • 1
    If you execute through the terminal, then you are responsible for declaring the classpath for `scala` (and for `scalac` for that matter), eg `scala -classpath ScalaJdbcConnectSelect`, see also [Include jar file in Scala interpreter](https://stackoverflow.com/questions/1707454/include-jar-file-in-scala-interpreter) – Mark Rotteveel Jan 22 '20 at 13:53
  • Yes !! scala -classpath ScalaJdbcConnectSelect solved my problem !! – LynxWeb Jan 22 '20 at 14:05

2 Answers2

0

I solved my problem !!

Thanks to @Mark Rotteveel for the solution :

If you execute through the terminal, then you are responsible for declaring the classpath for scala (and for scalac for that matter), eg scala -classpath ScalaJdbcConnectSelect

LynxWeb
  • 69
  • 1
  • 9
-1

Clearly seems to be a classpath related issue. Usually i'll ask for clarifications with regards to your dependency manaagement but the forum doesn't permit

Try importing oracle.jdbc.driver.OracleDriver to your package

2x_sham_y
  • 69
  • 1
  • 3