1

I'm currently working on a SBT-based Scala project and I need to use MySQL as database (through Slick 3.0.0 library).

The issue is that I can't connect to the database because of this exception:

> service[ERROR] java.sql.SQLException: No suitable driver
service[ERROR]  at java.sql.DriverManager.getDriver(DriverManager.java:315)
service[ERROR]  at slick.jdbc.DriverBasedJdbcDataSource$class.registerDriver(JdbcDataSource.scala:60)
service[ERROR]  at slick.jdbc.DriverJdbcDataSource.registerDriver(JdbcDataSource.scala:72)
service[ERROR]  at slick.jdbc.DriverJdbcDataSource.<init>(JdbcDataSource.scala:78)
service[ERROR]  at slick.jdbc.JdbcBackend$DatabaseFactoryDef$class.forURL(JdbcBackend.scala:101)
service[ERROR]  at slick.jdbc.JdbcBackend$$anon$3.forURL(JdbcBackend.scala:33)

enter image description here

What I've done so far (see screenshot):

  • I made sure the MySQL server is running.

  • I've added mysql-connector-java dependency the build.sbt file. The jar gets downloaded correctly (see column on the left).

  • I've used this to get a connection to the database:

    val db = Database.forURL(
      "jdbc:mysql/localhost:3306/service",
      "service",
      "service",
      driver="com.mysql.jdbc.Driver"
    ) 
    

Any idea on how to solve this?

Thanks.

3
  • Hint: if I run "sbt console" in the directory, classForName("com.mysql.jdbc.Driver") works Commented May 11, 2015 at 9:32
  • if you put, above the line you've shown, val driverClass = classForName("com.mysql.jdbc.Driver"), since that works in the console? alternatively, however you are starting up your JVM, -Djdbc.drivers=com.mysql.jdbc.Driver Commented May 11, 2015 at 10:10
  • The following, Class.forName("com.mysql.jdbc.Driver"), worked for me Commented Sep 13, 2015 at 23:21

1 Answer 1

1

It should be jdbc:mysql://localhost:3306/service not jdbc:mysql/localhost:3306/service. You are missing a slash and colon after mysql.

Tried on my own project. The error with the wrong jdbc URL gives me same exception as yours. I guess this exception is misleading.

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

1 Comment

The DriverManager queries each registered Driver in turn if it will connect using the URL, if none accept it (eg because it has the wrong prefix as in this case), then "No suitable driver" has been found. I am not sure how that would be misleading; it is a result of the service provider architecture of DriverManager.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.