5. week

Task 4 - Implementing service components using JDBC

Motivation

In game portals such as Gamestudio, additional services can enrich the user experience and bring a socialising aspect into the application which will make it engaging.

Your task on this exercise is to implement the following services into your game:

  • score - service for recording scores of game players,
  • comment - service for recording comments for a game entered by users,
  • rating - service for rating a game by users, e.g. by entering number of stars in scale from 1 to 5.

The following steps present implementation of the score service for storing scores of game players and for displaying the highest scores of players. You will implement the remaining services in similar way. To implement each service component, we need the following:

  • A database named gamestudio with a table containing data for the service, e.g. SCORE,
  • Entity class of the service containing all data about one service instance, e.g. Score,
  • Interface of the service declaring basic actions that should be accessible, e.g. ScoreService,
  • Implementation of service's interface, which will provide access to data from the database for reading and writing, e.g. ScoreServiceJDBC.

Objectives

  1. Create a database to store the service data.
  2. Create entity classes to represent data for services and add interfaces of service components into the code.
  3. Implement the interfaces of service components.
  4. Integrate created services into your game.

Instructions

Step 1

Task 1.1

Install a database system PostgreSQL on your computer. To download it, you can use the official webpage http://www.postgresql.org/download/ or you can install it by the package manager of your operating system. Use the newest version of the database server available for your system.

Task 1.2

Add a project management and comprehension tool Maven into your project.

To add Maven into existing project, right-click the project name in the panel Project, and from the context menu select Add Framework Support (Fig. 1).

Right-click on the project name opens a context menu where Add Framework Support option is available
Fig. 1: Right-click on the project name opens a context menu where Add Framework Support option is available

From the list of frameworks select Maven (Fig. 2). Usually, it is at the bottom of the list.

Selecting Maven in the list of frameworks.
Fig. 2: Selecting Maven in the list of frameworks.

After that a new file pom.xml will be added into your project. It is a Maven configuration file which allows defining dependencies (libraries, additional modules, etc.) and settings for project building and deployment.

In the pom.xml file, fill in the entries groupId, artifactId and version as follows:

<groupId>sk.tuke</groupId>
<artifactId>gamestudio</artifactId>
<version>0.0.1-SNAPSHOT</version>

Task 1.3

Add a jdbc driver for PostgreSQL into the project, which allows an application written in Java to connect to PostgreSQL database.

Use pom.xml to add a definition of the library into project dependencies. Inside the project element, add the dependencies element with a dependency definition for postgresql, as shown below:

<project ...>
    ...
    <groupId>sk.tuke</groupId>
    <artifactId>gamestudio</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    
    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</project>

After that, open the Maven panel (a button to show this panel is usually located on the right edge of the window) and press the button for reloading the project (Fig. 3). Maven will download the library from the internet and set it up in the project.

Opening the Maven panel and reloading the project to download new library from the internet.
Fig. 3: Opening the Maven panel and reloading the project to download new library from the internet.

Task 1.4

Create a database with the name gamestudio. You can find help for further work with the database in the PostgreSQL documentation.

Comment

For connecting to, creating and managing a database you can use directly IntelliJ IDEA development environment and its tool panel Database (Fig. 4). A button to show this panel is usually located on the right edge of the window. For advanced managing of several databases outside IntelliJ IDEA there is an application available by JetBrains called DataGrip. You can also use application pgAdmin.

Panel for managing databases in the IntelliJ IDEA, creating a connection to the PostreSQL database
Fig. 4: Panel for managing databases in the IntelliJ IDEA, creating a connection to the PostreSQL database

Step 2

Add interfaces and entities specifying the service components from the zip archive serviceSpecs.zip into your project. Read the specification description thoroughly. Individual interfaces contain the signatures of required methods that need to be supported by services.

Warning

Due to future compatibility with GameStudio portal, don't change the interfaces and don't create any new public methods in the interfaces. Adhere to given specification of service interfaces.

Task 2.1

Implement entity classes Comment and Rating.

As an example, the implementation of class Score is available.

Step 3

Task 3.1

With respect to the specification, create tables in the database which will contain the data for the services.

The development environment IntelliJ IDEA has a support for various database systems, so you can create tables for service data directly in the development environment. There is a graphical editor available for working with tables (dialogue windows), but also an option to write and execute your own database script.

You can create new table in the IntelliJ IDEA environment through tool panel Database (a button to show this panel is usually located on the right edge of the window). By clicking and expanding the name of connected database you will get to the schema (entry schemas->public). A right-click on the schema will show a context menu, from which you can choose option New->Table (see the image below).

Creation of a database table in the development environment IntelliJ IDEA
Fig. 5: Creation of a database table in the development environment IntelliJ IDEA

After that a dialogue window will appear, in which you can add database columns by using buttons, and set their data types (the upper part of the window). Alternatively, you can use the text field to enter the command for creation of the table (the lower part of the window). When adding columns by the button +, the command in the text field will be also automatically generated, matching the columns in created database.

Adding columns into newly created database table
Fig. 6: Adding columns into newly created database table

Comment

When creating the tables, follow the specification of the services. Also consider that this specification is general regarding the data types in the database, so when you add columns into the table, you need to choose data types which are supported specifically by the database system Postgresql.

Step 4

Task 4.1

Create JDBC implementation of service interfaces in the package service with class names RatingServiceJDBC and CommentServiceJDBC. Get inspiration from the example in class ScoreServiceJDBC.

To implement the services you will need:

  • Database connection credentials (URL, login, password),
  • SQL commands that will be used in the particular service component methods,
  • Implementation of the interfaces' methods along with connection to the database, exception handling and closing the database connection.

Comment

To get some inspiration regarding the implementation you can have a look at the implementation of class ScoreServiceJDBC from the lecture. Notice also how service score is integrated into the game in class ConsoleUI.

Warning

When writing the code, pay attention to naming the classes and methods according to the specification.

Step 5

Task 5.1

Integrate created service components into your game so that their functionalities can be used directly from the game through commands entered in console user interface.

For example, service score should be integrated into the game by storing player's score automatically after finishing the game (or a level). After that, either automatically or by user-initiated action (command), a list of top 10 players and their scores should be printed.

Similarly, integrate services comment and rating into your game.

Step 6

Task 6.1

Upload (commit and push) your implementation into your repository on GitLab. You can update the project as you gradually work on it. Before the next exercise, make sure you uploaded your latest work into your repository. Also, prepare questions that you would like to discuss on the next exercise.

Resources

  1. PostgreSQL documentation
  2. Archive with the specification of service components serviceSpecs.zip
  3. Description of the specification of service components
  4. Documentation of datatypes for Postgresql