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")
}
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
# 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.*"]
// 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")
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()
10 回答
对于播放java项目使用SBT
在“build.sbt”中将libraryDependency更改为llok
使用“激活器运行”运行项目
播放将需要jdbc连接器 .
从Play的文档中查看this page . 它说:
SBT将为您下载驱动程序 . 你还应该看看section on managing dependencies .
要连接到MySQL,您还需要更改
application.conf
中的一些设置:正如Carsten所写,它可以从文档中获取,但是这里是一个总结:
确保在
/project/Build.scala
中配置了依赖项在
/conf/application.conf
中添加DB的正确配置(替换默认的H2配置):(不要从URL中删除编码):
在同一个文件中查找并确保此行未注释:
这就是全部,重新启动你的应用程序(或以开发模式运行),然后它将创建一个DDL并要求你应用它 .
我正在使用play 2.2.0,我只需要将以下行添加到项目根文件夹中的build.sbt .
并且播放自动下载驱动程序 . 似乎不再需要Build.scala了 . 应该像上面的评论员提到的那样对application.conf进行更改 .
访问我遇到的mysql数据库的大多数方法都没有解释如何 Build 连接并从模型中检索数据 . 在我的应用程序中,我使用mongoDB和外部mysql数据库 . 所以这就是我做的事情(mysql方面):
如果要使用默认数据库或要使用的任何其他名称,可以将“myotherdb”替换为“default” . 将“xxx.xxx.xxx.xxx”替换为数据库所在服务器的IP地址(对于外部数据库)或本地数据库的localhost(或127.0.0.1) . 将“NameOfOtherDB”替换为您要使用的数据库的名称,将“MyOtherDbUSername”替换为您的数据库用户名,将“MyOtherDbPass”替换为您的数据库密码 .
其中“columnName”是要检索的数据库表列/字段的名称 .
最后但并非最不重要的,我想说明您可能希望通过调用close()来关闭连接
在我找到这个之前,我一直坚持使用我的MySQL配置 .
最重要的事情来自@biesior回答:
在项目的依赖项中添加MySQL连接器/ J(在
/project/Build.scala
内)添加依赖项后,运行
play dependencies
以解决新添加的MySQL连接器/ J依赖项取消注释默认的ebean配置行
ebean.default="models.*"
使用正确的字符编码正确配置MySQL数据库
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
它救了我的一天 .
对于 play 2.3.1 ,请执行以下步骤 .
1)在项目的依赖项中添加MySQL连接器/ J(在/project/build.sbt中)
2)取消注释默认的ebean配置行ebean.default =“models . *”
3)使用正确的字符编码正确配置MySQL数据库
4)大多数进出口 . 在控制台中运行 reload 命令 .
播放2.4.3和MYSQL 5.7.9
我能够通过拼凑所有先前答案中的一些信息来实现这一目标 . 所以这是另一个,希望对具有类似环境的人更新或有用 .
Environment Details: (这就是我正在使用的)
播放2.4.3这附带有activator-1.3.7-minimal
JDK8,你应该已经有了这个,因为我不认为这个版本的游戏适用于JDK7
MYSQL 5.7.9
appication.conf
Note:
如果您使用的是MYSQL workbench,则URL中的
testSchema是您的数据库名称你会在SCHEMAS部分看到这个 . 我打电话给我的testSchema . 其他人可能称之为"myDatabase"
该端口应该是MYSQL端口 . 不是您的应用程序端口我在示例中放了
3306
,因为这通常是MYSQL的默认值 .build.sbt
将以下行添加到build.sbt文件中 . 这应该在
libraryDependencies ++= Seq()
声明之后 .Finally
从项目根目录运行此命令 - >
activator reload
重新启动你的appplication
我在最新的游戏框架2.4.x和激活器1.3.6中遇到了同样的问题 .
这是步骤 . 我按照这里描述的步骤https://www.playframework.com/documentation/2.4.x/JavaDatabase
这是我的
application.conf
这是
build.sbt
plugins.sbt
这是重要的一步 .