Steps Needed to Use MySQL Database with Play Framework 2.0

Steps needed to use MySQL database with Play framework 2.0

Look at this page from Play's documentation. It says:

Other than for the h2 in-memory database, useful mostly in development mode, Play 2.0 does not provide any database drivers. Consequently, to deploy in production you will have to add your database driver as an application dependency.

For example, if you use MySQL5, you need to add a dependency for the connector:

val appDependencies = Seq(
// Add your project dependencies here,
...
"mysql" % "mysql-connector-java" % "5.1.18"
...
)

SBT will download the driver for you. You should also check out the section on managing dependencies.

To connect to MySQL, you will also need to change some settings in your application.conf:

db.default.driver=com.mysql.jdbc.Driver
db.default.url="mysql://root:secret@localhost/myDatabase"

connect MySQL database with play-framework 2.5

Finally this worked for me :-

This error killed my lot of time.

Here is what worked for me,

uncomment these line.

    play.db {
config = "db"
default = "default"
}

then

    db {

default.driver=com.mysql.jdbc.Driver
default.url="jdbc:mysql://localhost:3306/playdb"
default.username=root
default.password="9403678957"
}

don't needdb.default.*, as you already in the db set range.

Lots of thanks to this answer.

How do you configure a File System with Play Framework 2.0 while keeping my current MySQL database?

You messed up things a little, the link you provided instructs how to configure whole H2 database to be storred on the disk or just in the memory.

For your purpose you should just adopt File Upload sample from Play's documentation

The additional thing you have to do BEFORE return ok("File uploaded"); line is to find record in your MySQL and update it by adding fileName and/or contentType as a refference to file storred on the disk.

Play for a while with File type, then you'll be able to store files in FS where you'll want and how you'll want

Fetching data from mysql with play framework using ebean

You must uncomment this property in the application.conf

   # Evolutions
# ~~~~~
# You can disable evolutions if needed
evolutionplugin=disabled

play framework - Driver not found: [com.mysql.jdbc.Driver] - intelli J

Here you have full recipe for configuring MySQL DB:

  • Steps needed to use MySQL database with Play framework 2.0

How to use ebean and mysql in play framework?

To enable MySQL

in the application.conf file:

# Database configuration using MySQL database engine
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://127.0.0.1/mydataabse"
db.default.username=yourusername
db.default.password="yourpassword"

and you also need to add MySQL connector to the build.sbt libraryDependencies:

libraryDependencies ++= Seq(
...
"mysql" % "mysql-connector-java" % "5.1.18"
)

To enable Ebean

Add Ebean plugin In to the project\plugins.sbt:

addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "1.0.0") 

Enable this plugin in the build.sbt:

lazy val root = (project in file(".")).enablePlugins(PlayJava, PlayEbean)

Configure Ebean in the conf\application.conf to take models from your model package:

ebean.default = ["my.models.*"]

How to save images with an object in Play framework 2.0 + java + mysql

After upload just save the image preferably in the file system (not directly in the app) or even in some external CDN/cloud storage and then save its path in DB as a common String.

So you can use for an example some lightweight HTTP server for serving the files or even use Play's streaming possibilities as described in the doc - in section Serving files.

Of course if you'll store the file in external service like CDN, all you need to do is place absolute path in the src attribute of the img tag.



Related Topics



Leave a reply



Submit