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
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
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
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.