简体   繁体   中英

JDBC connection string not working with MySQL on Play Framework

I'm trying to build an application using Play Framework 2.2 and Scala.

I'm not very acquainted to Java environments, so I don't know exactly what's going on.

To make it work with MySql, I should configure my conf/application.conf like this:

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.

What am I doing wrong?

EDIT: Here is my stacktrace. It seems to be missing the mysql connector .jar file, or something like that. What should I do?

[success] Compiled in 734ms [error] cjbhAbstractConnectionHook - Failed to obtain initial connection Sle eping for 0ms and trying again. Attempts left: 0. Exception: null.Message: No sui table driver found for mysql://localhost:3306/world [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]
[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. This page in the documentation shows how to add the derby driver to the dependencies. Substitute the derby coordinates with the MySQL ones .

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

Thanks to everyone!

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.

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