简体   繁体   English

JDBC连接字符串不适用于Play Framework上的MySQL

[英]JDBC connection string not working with MySQL on Play Framework

I'm trying to build an application using Play Framework 2.2 and Scala. 我正在尝试使用Play Framework 2.2和Scala构建应用程序。

I'm not very acquainted to Java environments, so I don't know exactly what's going on. 我不太了解Java环境,所以我不知道到底发生了什么。

To make it work with MySql, I should configure my conf/application.conf like this: 为了使其与MySql一起使用,我应该这样配置conf/application.conf

db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost:3306/sakila"
db.default.user=root
db.default.password="mypass"

Everything seems right to me, but when I try to access it, I get this: 一切对我来说似乎都是正确的,但是当我尝试访问它时,我得到了:

 Cannot connect to database [default]

Why? 为什么? These information are right! 这些信息是正确的! The database can be found at localhost:3306/sakila. 该数据库位于localhost:3306 / sakila。

What am I doing wrong? 我究竟做错了什么?

EDIT: Here is my stacktrace. 编辑:这是我的堆栈跟踪。 It seems to be missing the mysql connector .jar file, or something like that. 似乎缺少mysql连接器.jar文件或类似的文件。 What should I do? 我该怎么办?

[success] Compiled in 734ms [error] cjbhAbstractConnectionHook - Failed to obtain initial connection Sle eping for 0ms and trying again. [成功]用734ms编译。[错误] cjbhAbstractConnectionHook-无法获得初始连接Sle eping 0ms,然后重试。 Attempts left: 0. Exception: null.Message: No sui table driver found for mysql://localhost:3306/world [error] application - 剩余尝试次数:0。异常:null。消息:未找到mysql:// localhost:3306 / world [错误]应用程序的sui表驱动程序-

! @6gjp5p29b - Internal server error, for (GET) [/] -> @ 6gjp5p29b-内部服务器错误,用于(GET)[/]->

play.api.Configuration$$anon$1: Configuration error[Cannot connect to database [
default]]
        at play.api.Configuration$.play$api$Configuration$$configError(Configura
tion.scala:92) ~[play_2.10.jar:2.2.1]
        at play.api.Configuration.reportError(Configuration.scala:570) ~[play_2.
10.jar:2.2.1]
        at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:252) ~[pla
y-jdbc_2.10.jar:2.2.1]
        at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:243) ~[pla
y-jdbc_2.10.jar:2.2.1]
        at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike
.scala:244) ~[scala-library.jar:na]
        at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike
.scala:244) ~[scala-library.jar:na]
Caused by: java.sql.SQLException: No suitable driver found for mysql://localhost
:3306/world
        at java.sql.DriverManager.getConnection(Unknown Source) ~[na:1.7.0_07]
        at java.sql.DriverManager.getConnection(Unknown Source) ~[na:1.7.0_07]
        at com.jolbox.bonecp.BoneCP.obtainRawInternalConnection(BoneCP.java:363)
 ~[bonecp.jar:na]
        at com.jolbox.bonecp.BoneCP.<init>(BoneCP.java:416) ~[bonecp.jar:na]
        at com.jolbox.bonecp.BoneCPDataSource.getConnection(BoneCPDataSource.jav
a:120) ~[bonecp.jar:na]
        at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:245) ~[pla
y-jdbc_2.10.jar:2.2.1]
[error] application -

! @6gjp5p29b - Internal server error, for (GET) [/] ->

play.api.Configuration$$anon$1: Configuration error[Cannot connect to database [
default]]
        at play.api.Configuration$.play$api$Configuration$$configError(Configura
tion.scala:92) ~[play_2.10.jar:2.2.1]
        at play.api.Configuration.reportError(Configuration.scala:570) ~[play_2.
10.jar:2.2.1]
        at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:252) ~[pla
y-jdbc_2.10.jar:2.2.1]
        at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:243) ~[pla
y-jdbc_2.10.jar:2.2.1]
        at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike
.scala:244) ~[scala-library.jar:na]
        at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike
.scala:244) ~[scala-library.jar:na]
Caused by: java.sql.SQLException: No suitable driver found for mysql://localhost
:3306/world
        at java.sql.DriverManager.getConnection(Unknown Source) ~[na:1.7.0_07]
        at java.sql.DriverManager.getConnection(Unknown Source) ~[na:1.7.0_07]
        at com.jolbox.bonecp.BoneCP.obtainRawInternalConnection(BoneCP.java:363)
 ~[bonecp.jar:na]
        at com.jolbox.bonecp.BoneCP.<init>(BoneCP.java:416) ~[bonecp.jar:na]
        at com.jolbox.bonecp.BoneCPDataSource.getConnection(BoneCPDataSource.jav
a:120) ~[bonecp.jar:na]
        at play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:245) ~[pla
y-jdbc_2.10.jar:2.2.1]

You need to have the MySQL JDBC driver jar in your dependencies. 您需要在依赖项中包含MySQL JDBC驱动程序jar。 This page in the documentation shows how to add the derby driver to the dependencies. 文档中的此页面显示了如何将derby驱动程序添加到依赖项。 Substitute the derby coordinates with the MySQL ones . MySQL的 derby坐标代替。

OK, I don't know what's going on. 好吧,我不知道发生了什么。

I put this, and it started to work: 我放了这个,它开始起作用:

db.default.url="jdbc:mysql://localhost:3306/world"
db.default.driver="com.mysql.jdbc.Driver"
db.default.user="root"
db.default.pass="mypasswrd"
db.default.host="localhost"

Perhaps it needed a more detailed configuration, i just added the db.default.host configuration, stringified everything with "" (I think this is not necessary, but whatever) and checked if mysql was listed in play dependencies listing. 也许它需要更详细的配置,我只是添加了db.default.host配置,将所有内容都用""字符串化(我认为这不是必需的,但是无论如何),并检查mysql是否在play dependencies列表中列出。 Since it was listed there (in fact it was the first entry), and the error didn't say it was a missing library, I just headed to Error when i try connect play with mysql 5.5 and fixed my configurations. 由于它是在那里列出的(实际上它是第一个条目),并且错误并没有说明它是缺少的库,因此当我尝试使用mysql 5.5进行连接播放并修复我的配置 ,我只是走向了错误

Thanks to everyone! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM