[JAVA] Play Framework Study Memo Database ①

(Reference) Books you are studying https://www.amazon.co.jp/Play-Framework-2%E5%BE%B9%E5%BA%95%E5%85%A5%E9%96%80-Java%E3%81%A7%E3%81%AF%E3%81%98%E3%82%81%E3%82%8B%E3%82%A2%E3%82%B8%E3%83%A3%E3%82%A4%E3%83%ABWeb%E9%96%8B%E7%99%BA-%E6%B4%A5%E8%80%B6%E4%B9%83/dp/4798133922/ref=cm_cr_srp_d_product_top?ie=UTF8

About the database

What is a database in the first place? See below. https://www.sejuku.net/blog/8763#i-4 Database design is very important when creating an application.

About H2

The play framework incorporates a database program called H2 (H2 Data Base Engine).

About the settings to use the database

How to set application.conf

First, allow the application to use the DB. The setting method is as follows.

Setting item = value

Code to add to application.conf

・ Comments starting with # ・ Add the following code


Explanation of setting items

Explanation including the parts that have not been added.


→ Specify the driver to be used for database access. Here, Java classes are generally specified.


→ Specify the URL that indicates the database access destination. Specify in the form of "jdbc. 〇〇".


→ Not used in H2, but used when using a client-server database. Enter the user name.


→ Not used in H2, but used when using a client-server database. Enter the password.


→ Describe the contents related to the O / R mapper.

How to set up to call the database

There are two main patterns for calling the database. -How to write in the driver file → Files to access the database http://www.atmarkit.co.jp/ait/articles/0106/26/news001.html

-How to use the build tool → A tool that automatically activates the application, including access to the driver (What is a build) https://qiita.com/Mura-Mi/items/225825cc3715dc04d923

** The Play Framework uses a build tool called "sbt" to download the tools needed to access the database. ** **

Setting addition method

Describe the items used in the application after seq, separated by commas, as shown below.

libraryDependencies ++= Seq(Item 1,Item 2 ...)

Contents of sbt code after adding settings

name := "PlayApp"

version := "1.0-SNAPSHOT"

libraryDependencies ++= Seq(


About creating a database

In the play application, by defining the concrete table contents in the Model class The table can be prepared automatically at the time of execution.

How to make a Model class

Create a folder called models in a folder called apps. Create a file called Message.java in the folder.

Description method

public class class name extends Model{
//Definition of fields to store

Whole code

package models;

import java.util.Date;

import javax.persistence.*;
import javax.validation.*;

import play.data.validation.*;
import play.db.ebean.*;

public class Message extends Model{//②

	public Long id;
	public String name;
	public String mail;
	public String message;
	public Date postdate;

(1) Describe that it is an entity class. (Reference for entity classes) http://itdoc.hitachi.co.jp/manuals/link/cosmi_v0870/APKC/EU070254.HTM (2) By inheriting the class called Model, it is possible to retain the function as a database. ③ The field below @ID is treated as the primary key.

Other class changes


package controllers;

import play.*;
import play.mvc.*;

import views.html.*;

public class Application extends Controller {
    //Action when accessing the route
    public static Result index(){
      return ok(index.render("Database sample"));



@main("Sample page") {

Display result

スクリーンショット 2018-03-24 23.25.54.png

Recommended Posts

Play Framework Study Memo Database ①
Play Framework Study Memo Database ②Read
Play Framework study
spring framework Simple study memo (2): AOP
Play Framework Study Momo DB Update
spring framework Simple study memo (1): ApplicationContext, Bean, Autowired
Play Framework2.5 (Java) Tips
[Java ~ Method ~] Study memo (5)
Dot installation study memo 01
[Java ~ Array ~] Study memo 4
Play Framework studying test
play framework personal notes
[Personal memo] About Spring framework
Ruby study memo (conditional branching)
Spring Framework self-study memo series_1
Java Silver Study Method Memo
[Java ~ Boolean value ~] Study memo (2)
Introduction to JUnit (study memo)
First Play Framework personal notes
Java study memo 2 with Progate
Introduced Dozer to Play Framework
Ruby study memo (recursive function)
Validation function in Play Framework
[Ruby ~ Iterative processing ~] Study memo 4
Play Framework 2.6 (Java) development environment creation
Double submit measures with Play Framework
[Study session memo] Java Day Tokyo 2017
Ruby Study Memo (Test Driven Development)
Play framework scheduled jobs and crontab