Scala 和 MySQL JDBC 驱动程序 [英] Scala and MySQL JDBC Driver
问题描述
我目前正在开发一个基于 SBT 的 Scala 项目,我需要使用 MySQL 作为数据库(通过 Slick 3.0.0 库).
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)
到目前为止我所做的(见截图):
What I've done so far (see screenshot):
我确保 MySQL 服务器正在运行.
I made sure the MySQL server is running.
我在 build.sbt 文件中添加了 mysql-connector-java 依赖项.jar 已正确下载(请参阅左侧的列).
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?
谢谢.
推荐答案
应该是 jdbc:mysql://localhost:3306/service
而不是 jdbc:mysql/localhost:3306/服务
.您在 mysql 之后缺少斜线和冒号.
It should be jdbc:mysql://localhost:3306/service
not jdbc:mysql/localhost:3306/service
. You are missing a slash and colon after mysql.
在我自己的项目上尝试过.jdbc URL 错误的错误给了我和你一样的异常.我想这个例外是有误导性的.
Tried on my own project. The error with the wrong jdbc URL gives me same exception as yours. I guess this exception is misleading.
这篇关于Scala 和 MySQL JDBC 驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!