我是新来的游戏框架。 我想配置MySQL数据库作为数据源与播放Ebeans使用。
你能不能有人请解释一下,一起玩2.0框架配置MySQL需要(比如,下载驱动程序,添加依赖等)中的步骤。
我是新来的游戏框架。 我想配置MySQL数据库作为数据源与播放Ebeans使用。
你能不能有人请解释一下,一起玩2.0框架配置MySQL需要(比如,下载驱动程序,添加依赖等)中的步骤。
看看这个网页从播放的文件。 它说:
比对H2内存数据库等,有用的主要是在发展模式,播放2.0不提供任何数据库驱动程序。 因此,部署在生产中,你将有你的数据库驱动程序添加作为应用程序的依赖。
例如,如果你使用的MySQL5,你需要添加一个依赖于连接器:
val appDependencies = Seq(
// Add your project dependencies here,
...
"mysql" % "mysql-connector-java" % "5.1.18"
...
)
SBT将下载的驱动程序为您服务。 您也应该检查出的管理依赖部分 。
要连接到MySQL,你还需要在你更改一些设置application.conf
:
db.default.driver=com.mysql.jdbc.Driver
db.default.url="mysql://root:secret@localhost/myDatabase"
正如卡斯滕写的可以从文件中获取,但是这里有一个总结:
请确保您在配置的依赖性/project/Build.scala
val appDependencies = Seq(
// Add your project dependencies here,
"mysql" % "mysql-connector-java" % "5.1.18"
)
在加入DB的正确配置(替换默认配置H2) /conf/application.conf
:
(不要删除URL编码):
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost/your_db_name?characterEncoding=UTF-8"
db.default.user=your_login
db.default.password=your_pass
在同一文件中找到,并确保该行没有评论:
ebean.default="models.*"
这一切,重新启动您的应用程序(或开发模式下运行),那么它会创建一个DDL并要求你使用它。
我使用的游戏2.2.0,我只是不得不添加下面的行build.sbt项目的根文件夹。
"mysql" % "mysql-connector-java" % "5.1.27"
而游戏会自动下载驱动程序。 这似乎不需要Build.scala这个了。 如上述评论人士提到应适用变更application.conf。
大多数的访问,我已经遇到一个MySQL数据库的方法不解释如何建立连接,并从模型中检索数据。 在我的应用程序,我使用MongoDB的都和外部MySQL数据库。 因此,这里是我是如何做到的事情(的MySQL的一侧):
对于播放2.3.3,在build.sbt文件中添加特定的MySQL中的行libraryDependencies:
libraryDependencies ++= Seq( "mysql" % "mysql-connector-java" % "5.1.27" )
在/conf/application.conf文件中添加此:
db.myotherdb.driver = com.mysql.jdbc.Driver db.myotherdb.url = "jdbc:mysql://xxx.xxx.xxx.xxx/NameOfOtherDB?characterEncoding=UTF-8" db.myotherdb.user = MyOtherDbUSername db.myotherdb.password = MyOtherDbPass
你可以在你想使用默认的数据库的情况下或与您要使用的任何其他名称取代“myotherdb”的“默认”。 与(在外部数据库的情况下)或本地主机(或127.0.0.1)用于本地数据库您的数据库所在的服务器的IP地址替换“xxx.xxx.xxx.xxx”。 与您要使用的“MyOtherDbUSername”与您的数据库用户名和“MyOtherDbPass”与您的数据库密码的数据库的名称替换“NameOfOtherDB”。
内部模型(/app/models/MyModel.scala)补充一点:
val connection = DB.getConnection("myotherdb")
创建语句,查询并执行它:
val statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY) val query = "SELECT * FROM myTableName" val resultset = statement.executeQuery(query)
然后,你可以继续你想要检索的数据做什么。 例如:
while (resultset.next()) { resultset.getString("columnName") }
其中,“列名”是数据库表列/要检索的字段的名称。
最后但并非最不重要的,我想指出,你可能要关闭调用关闭连接()
卡住了我的MySQL配置,直到我发现这一点。
从@biesior答案采取最重要的事情:
/project/Build.scala
) play dependencies
,解决新增的MySQL连接器/ J的依赖 ebean.default="models.*"
db.default.driver=com.mysql.jdbc.Driver db.default.url="jdbc:mysql://www.sample.com:3306/test?characterEncoding=UTF-8" db.default.user=playuser db.default.pass=playuser
它救了我的一天。
对于游戏2.3.1,请按照下列步骤。
1)添加的MySQL连接器/ J在项目的依赖(这是内/project/build.sbt)
libraryDependencies ++= Seq( javaJdbc, javaEbean, "mysql" % "mysql-connector-java" % "5.1.29"
2)取消注释默认ebean配置行ebean.default = “模型。*”
3)用合适的字符编码正确配置MySQL数据库
db.default.driver=com.mysql.jdbc.Driver //this is com. and not org.
db.default.url="jdbc:mysql://127.0.0.1/test?characterEncoding=UTF-8"
db.default.user=playuser
db.default.pass=playuser
4)大多数进出口。 运行在控制台中reload命令。
我能够从所有以前的答案信息的比特拼凑得到这个工作。 因此,这里是另外一个,就是希望更多的最新的或有用的那些相似的环境。
环境信息:( 这是我使用 )
appication.conf
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost:3306/testSchema?characterEncoding=UTF-8"
db.default.user=yourDBUserName
db.default.password=yourDBUserPass
注意:
3306
的例子,因为这通常是MySQL默认。 build.sbt
下面这行添加到您的build.sbt文件。 这应该在之后去libraryDependencies ++= Seq()
声明。
libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.36"
最后
activator reload
对于可以玩Java项目中使用SBT
更改libraryDependency到llok像这样的“build.sbt”
libraryDependencies ++= Seq(
javaJdbc,
javaEbean,
cache,
javaWs,
"mysql" % "mysql-connector-java" % "5.1.27"
)
使用“激活运行”运行项目
比赛将下降所需的JDBC连接器。
我在最近的游戏框架的2.4.x同样的问题与激活1.3.6。
下面是步骤。 我跟着这里描述的步骤https://www.playframework.com/documentation/2.4.x/JavaDatabase
这里是我的application.conf
# MySQL DB Configuration db.default.driver=com.mysql.jdbc.Driver db.default.url="jdbc:mysql://{hostname or ipaddres}/{db name}?characterEncoding=UTF-8" db.default.username=username // Note that user id deprecated, instead use username. Though that is not a major issue db.default.password="password" # JPA Configurations jpa.default=defaultPersistenceUnit PlayKeys.externalizeResources = false # JavaEbean configuration ebean.default = ["models.*"]
下面是build.sbt
libraryDependencies ++= Seq( javaJdbc, cache, javaWs, javaJpa, evolutions, "mysql" % "mysql-connector-java" % "5.1.27" )
plugins.sbt
// The Play plugin addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.3") // Web plugins addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0") addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.0.6") addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.3") addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.7") addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.0") addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.1.0") // Play enhancer - this automatically generates getters/setters for public fields // and rewrites accessors of these fields to use the getters/setters. Remove this // plugin if you prefer not to have this feature, or disable on a per project // basis using disablePlugins(PlayEnhancer) in your build.sbt addSbtPlugin("com.typesafe.sbt" % "sbt-play-enhancer" % "1.1.0") // Play Ebean support, to enable, uncomment this line, and enable in your build.sbt using // enablePlugins(SbtEbean). Note, uncommenting this line will automatically bring in // Play enhancer, regardless of whether the line above is commented out or not. addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "1.0.0")
这里是重要的一步。
在配置上面的步骤后,进入命令行,停止激活并运行命令
activator run
。 在我的情况,我不断收到错误unable to find mysql drivers
。 运行后activator run
,激活实际上下载MySQL驱动程序,并会解决的依赖关系。 这是解决我的问题的重要一步。
For me this work ,Add this below line into your Dependencies
**"mysql" % "mysql-connector-java" % "5.1.36"**
So , here is the code
import java.sql.Connection
val driver = "com.mysql.jdbc.Driver"
val url = "jdbc:mysql://localhost/world"
val username = "root"
val password = "root"
var connection: Connection = null
try { // make the connection
Class.forName(driver)
connection = DriverManager.getConnection(url, username, password)
// create the statement, and run the select query
val statement = connection.createStatement()
val resultSet = statement.executeQuery("SELECT id , name FROM bar")
val sql: SqlQuery = SQL("select * from products order by name asc")
while (resultSet.next()) {
val id = resultSet.getString("id")
val name = resultSet.getString("name")
println(id, name)
}
} catch {
case e: Exception => println("exception caught: " + e);
}
connection.close()