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

4
  • How do you run your scala code? The error indicates the driver isn't on the runtime classpath. Commented Jan 22, 2020 at 9: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 ? Commented Jan 22, 2020 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 <list of jars> ScalaJdbcConnectSelect, see also Include jar file in Scala interpreter Commented Jan 22, 2020 at 13:53
  • Yes !! scala -classpath <list of jars> ScalaJdbcConnectSelect solved my problem !! Commented Jan 22, 2020 at 14:05

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.