Play framework with MySQL database Part 2
Hi folks, So in the first part we have seen that how to establish the basic foundation for this session. Anyway now it has come the coding part that you were waiting for.
First thing first. Since we are going to work with MySQL database first we must specify the corresponding dependencies withing the play framework. According to the play framework structure all the related dependencies are define on build.sbt. Now open up it and enter the following dependencies.

/**These are the dependencies that it necessary to be specified in order to establish mysql database connection*/
libraryDependencies ++= Seq(
jdbc
)
libraryDependencies ++= Seq(
"mysql" % "mysql-connector-java" % "5.1.41"
)
Now since we establish the correction then go to the terminal and compile the files again. Then it will be download and configure all of the necessary files. First type sbt then update then compile then run.

Now open up your browser and check that if it works correctly.

Now lets create some database and tables. So toggle your wamp server and go to the web browser and type http://localhost/phpmyadmin/ then create a database.



And lets create a table associated with out database.


Now lets feed some data to it.


Now we are good to go to the next step. So lets again go to the play framework and open up the application.conf file. You can find this file in conf folder. And add the following configuration commands.

db {
# Default database configuration using MySQL database engine
# Connect to playframework_group as root with default password
default.driver=com.mysql.jdbc.Driver
default.url="jdbc:mysql://localhost/simpleepic_db"
default.username=root
default.password=""
}
Now all the dependencies and configuration details were specified.
So the second step it defining routes. What is a route? As its name suggest its a pre-specifies path for a controller. Remember since we are dealing with MVC architecture each view has its own controller and routes defined the transitions though these controllers. Then according to the path specified in the URL the corresponding transition will take place with respect to the route.
Go to the conf folder there you will see routes.

Now where exactly is the home controller exists? Go to the app/controllers folder there you can see HomeController.

Now within the home controller you can define all the functionalities that is necessary to be take place within the home. So what is home? Again according to the MVC architecture view is the presentation layer to the end users. Basically its some html file. So where is that home.html? Generally it must be created in the app/views folder. So create it. In my case ill define it as home.scala.html

Now its time to do some old school htmls. I hope you know html syntaxes even if you don’t know just go through the syntaxes. It is super easy to understand. So what I’m going to do is first ill create a basic table with sample data then later on we can feed the corresponding data in our database.

<html>
<head>
<title>Home</title>
</head>
<Body>
<table border = "2">
<tr>
<th>Index</th>
<th>Name</th>
</tr>
<tr>
<td>1</td>
<td>A</td>
</tr>
<tr>
<td>2</td>
<td>B</td>
</tr>
<tr>
<td>3</td>
<td>C</td>
</tr>
<tr>
<td>4</td>
<td>D</td>
</tr>
</table>
</Body>
</html>
Now if you remembered we specified our route like following,
GET /home controllers.HomeController.home
This mean when URL specifies /home goes to controller folder then go to HomeController.scala script and execute the home method. So we must define this home method within our HomeController.

/**This is a main home function that passes the data associated within the arrays to the home.scala.html*/
def home = Action {
Ok(views.html.home())
}
So in there I just redirect to the home.scala.html file then it must show the content of the home. Before that again update compile and run the play files (It is not necessary but i addicted to do that.)

Now go to your web browser and type this URL http://localhost:9000/home
So it will redirect to our home view.

OK folks. So we almost there. but again I think this post again getting bit longer. So ill continue this in the Play framework with MySQL part 3. In there I’ll show you how to perform database injections to the home class and retrieve the data through the database for our view.
Thank you guys I’ll see you in the next part.