Play framework with MySQL database Part 2

Dhanushka sandakelum
5 min readJun 21, 2021

--

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.

Necessary dependencies for establish the connection between play framework and MySQL
/**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.

Updating and configuring files for newly inserted dependencies

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

It works so far 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.

Type root as the username and let default password be empty. (But if you like you can create custom profile as well. Its up to you.)
Create new database
Give it a name

And lets create a table associated with out database.

Creating a table using SQL
Table was created successfully

Now lets feed some data to it.

Some data to our table.
Data was inserted successfully

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.

Added the configurations.
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.

Added a route

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

Default HomeContoller

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

home.scala.html created

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.

Sample table
<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.)

Update, compile and run

Now go to your web browser and type this URL http://localhost:9000/home

So it will redirect to our home view.

Awesome right !!!

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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Dhanushka sandakelum
Dhanushka sandakelum

Written by Dhanushka sandakelum

Hi! I'm Dhanushka. Currently I'm an undergraduate at University of Colombo School of Computing (UCSC) following B.Sc. in Computer Science.

No responses yet

Write a response