首页 文章

Play Framework连接到数据库

提问于
浏览
2

我一直在关注Play Framework的Typesafe在线培训(我是一个完整的网页设计初学者),一切正常,直到我们应该连接到数据库的部分 .

当我在第二课上看到第二个视频时这个网站:https://typesafe.com/how/online-training/play-java

我遇到了一个问题,这是我尝试刷新浏览器时收到的错误消息:

Cannot connect to database [default]
In /Users/Arash/playconf/conf/application.conf at line 43.
40 db.default.driver=com.mysql.jdbc.Driver
41
 # Here we add the database that we have created locally
43 db.default.url="jdbc:mysql://localhost:3306/playconf"
44
45 # We set this to root, we login as root with no password
46 db.default.user=root

这是我的终端中的错误消息(正在运行)

[错误] c.j.b.h.AbstractConnectionHook - 无法获得初始连接睡眠0ms并再次尝试 . 尝试离开:0 . 异常:null.Message:未知数据库'playconf'[错误]应用程序 - ! @ 6hmk31f56 - 内部服务器错误,用于(GET)[/] - > play.api.Configuration $$ anon $ 1:配置错误[无法连接到数据库[默认]] at play.api.Configuration $ .play $ api $ Configuration $$ configError(Configuration.scala:92)〜[play_2.10.jar:2.2.2] at play.api.Configuration.reportError(Configuration.scala:570)〜[play_2.10.jar:2.2.2] at at play.api.db.BoneCPPlugin $$ anonfun $ onStart $ 1.apply(DB.scala:252)〜[play-jdbc_2.10.jar:2.2.2] at play.api.db.BoneCPPlugin $$ anonfun $ onStart $ 1 .apply(DB.scala:243)〜[play-jdbc_2.10.jar:2.2.2]在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]引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)〜[na:1.8.0] sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct)中的未知数据库'playconf' orAccessorImpl.java:62)〜[na:1.8.0] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)〜[na:1.8.0] at java.lang.reflect.Constructor.newInstance(Constructor . java:408)〜[na:1.8.0] at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)〜[mysql-connector-java-5.1.26.jar:na] at com.mysql . jdbc.Util.getInstance(Util.java:386)〜[mysql-connector-java-5.1.26.jar:na]

我尽可能精确地遵循了一切,但是我得到了这个错误,我整天都被困在这里 . 很沮丧......

然后我想,哦,也许我没有mysql?所以我用homebrew安装了mysql,并再次尝试,但得到了同样的错误 .

这是我的代码与数据库有关:

在配置文件中:

# We add our sql driver, which we have added in the build.sbt file
 # db.default.driver=org.h2.Driver # This is the original, commented out originally
 db.default.driver=com.mysql.jdbc.Driver

 # Here we add the database that we have created locally
 db.default.url="jdbc:mysql://localhost:3306/playconf"

 # We set this to root, we login as root with no password
 db.default.user=root

在构建文件中:

libraryDependencies ++= Seq(
    javaEbean,
    "mysql" % "mysql-connector-java" % "5.1.26",
    "org.webjars" %% "webjars-play" % "2.2.0",
    "org.webjars" % "bootstrap" % "2.3.1")

好吧,所以我似乎必须创建数据库,现在我已经这样做了,但我无法访问数据库 . 我得到ERROR 1044(42000):用户''@ localhost'访问被拒绝到数据库'playconf'

3 回答

  • 9

    您需要在MySQL服务器中手动创建这个 playconf 数据库,用于示例公共数据库GUI(它不会像在H2中那样自动创建)

    这样做之后杀死正在运行的应用程序并再次运行它 .

  • 2

    首先测试与数据库的连接:
    似乎架构不存在或您的用户无权访问它!

    试试这个并发布输出

    mysql -uroot -p <passwrd> -e 'use playconf;
    
  • 0

    application.conf

    db.default.driver = com.mysql.jdbc.Driver

    db.default.url =“jdbc:mysql://127.0.0.1:3306 / dbname”

    db.default.user = root

    db.default.password = 12345

    db.default.host = localhost

    无需为用户,密码,主机和驱动程序字段使用双引号 .

    built.sbt

    添加mysql连接器 .

    libraryDependencies = Seq(javaJdbc,cache,javaWs,filters,“mysql”%“mysql-connector-java”%“5.1.18”)

相关问题