I'm new to Play framework. I'm trying to configure MySQL database as a datasource to be used with Play Ebeans.
Could you some one please explain the steps that are needed to configure MySQL with Play 2.0 framework (like, downloading drivers, adding dependency etc).
Look at this page from Play's documentation. It says:
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
:Most of the methods of accessing a mysql database that I've come across do not explain how to establish a connection and retrieve data from within the Model. In my application, I am using both mongoDB and an external mysql database. So here's how I did (the mysql side of) things:
For Play 2.3.3, in the build.sbt file add the mysql specific line in the libraryDependencies:
In the /conf/application.conf file add this:
You can replace "myotherdb" by "default" in case you want to use the default database or with any other name that you want to use. Replace "xxx.xxx.xxx.xxx" with the IP address of the server where your database is located (in case of an external database) or localhost (or 127.0.0.1) for local database. Replace "NameOfOtherDB" with the name of the database that you want to use, the "MyOtherDbUSername" with your database username and "MyOtherDbPass" with your database password.
Inside your Model (/app/models/MyModel.scala) add this:
Create the statement, the query and execute it:
Then you can continue with whatever you want to do with the retrieved data. For example:
Where "columnName" is the name of the DB table column/field that you want to retrieve.
Last but not least, I would like to note that you might want to close the connection by calling close()
For play 2.3.1, Follow these steps.
1) Add MySQL connector/J in project's dependency (which is inside /project/build.sbt)
2) Uncomment default ebean configuration line ebean.default="models.*"
3) Configure MySQL database correctly with proper character encoding
4) Most Imp. Run a reload command in the console.
As Carsten wrote it can be fetched from documentation, however here's a summary:
make sure you have the dependency configured in
/project/Build.scala
Add a proper config of the DB (replace default H2 config) in
/conf/application.conf
:(don't remove encoding from URL):
in the same file find and make sure this line is NOT commented:
That's all, restart your app (or run in dev mode), then it will create a DDL and ask you to apply it.
For play java project Using SBT
Change the libraryDependency to llok like this in "build.sbt"
Run your project using "activator run"
Play will down required jdbc connector.