I'm trying to connect to a MySQL database in Intellij IDE using Scala but I keep getting SQLException.
Here is my code:
import java.sql.Connection
import java.sql.DriverManager
object main extends App {
println("Hello World")
val driver = "com.mysql.cj.jdbc.Driver"
val url = "jdbc:mysql://localhost:3306/demodatabase"
val username = "root"
val password = "pass"
var connection: Connection = DriverManager.getConnection(url, username, password)
val statement = connection.createStatement()
val resultSet = statement.executeQuery("SELECT * FROM persons;")
println(resultSet)
while (resultSet.next()) {
println(resultSet.getString(1) + ", " + resultSet.getString(2) + ", " + resultSet.getString(3))
}
connection.close()
}
Here is the output:
"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:D:\IntelliJ IDEA Community Edition 2021.2.2\lib\idea_rt.jar=60731:D:\IntelliJ IDEA Community Edition 2021.2.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\IdeaProjects\Project 0 Revature\out\production\Project 0 Revature;C:\Users\RiotV\.ivy2\cache\org.scala-lang\scala-library\jars\scala-library-2.13.6.jar;C:\Users\RiotV\.ivy2\cache\org.scala-lang\scala-reflect\jars\scala-reflect-2.13.6.jar" main
Hello World
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/demodatabase
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:706)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
at main$.delayedEndpoint$main$1(main.scala:15)
at main$delayedInit$body.apply(main.scala:6)
at scala.Function0.apply$mcV$sp(Function0.scala:39)
at scala.Function0.apply$mcV$sp$(Function0.scala:39)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
at scala.App.$anonfun$main$1(App.scala:76)
at scala.App.$anonfun$main$1$adapted(App.scala:76)
at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:563)
at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:561)
at scala.collection.AbstractIterable.foreach(Iterable.scala:919)
at scala.App.main(App.scala:76)
at scala.App.main$(App.scala:74)
at main$.main(main.scala:6)
at main.main(main.scala)
Process finished with exit code 1
It's throwing a SQLException "No suitable driver found for jdbc:mysql://localhost:3306/demodatabase", so I'm unsure on how to fix this or what driver I'm missing because I have not been able to find any help for Scala online.
Any help is greatly appreciated.