5. week

Task 4 - Implementing service components using JDBC

Motivation

In game portals such as Gamestudio, auxiliary services can enrich the user experience and bring a social aspect into the application, making it more engaging.

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

  • score - a service for recording players' high scores,
  • comment - a service for adding comments to the game,
  • rating - a service for rating the game, for example by entering the number of stars on a scale from 1 to 5.

The implementation will be described using the score service as an example for storing player scores and displaying the leaderboard of the highest scores. To implement a service component for each of the services listed above, we will need the following:

  • a database named gamestudio with a table containing the service data, for example SCORE,
  • an entity class representing the service, containing all data about one service instance, for example Score, Comment, Rating,
  • an interface for the service, defining the basic actions it should support, for example ScoreService,
  • an implementation of the service interface, which will provide access to the data from the database for reading and writing, for example ScoreServiceJDBC.

Objectives

  1. Create a database for storing service data.
  2. Create entity classes representing service data and add interfaces for the service components.
  3. Implement the service component interfaces.
  4. Integrate the created services into your game.

Instructions

Step 1

Task 1.1

Install a PostgreSQL database system on your computer. You can download it from the official website http://www.postgresql.org/download/ or install it using your operating system's package manager. Use the newest version of the database server available for your system.

Task 1.2

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

Comment

To connect to, create, and manage a database, you can use the IntelliJ IDEA development environment and its Database tool window directly (the button to show it is usually on the right edge of the window). For advanced database management outside IntelliJ IDEA, JetBrains offers an application called DataGrip. You may also use pgAdmin.

Database management panel in IntelliJ IDEA, creating a connection to PostgreSQL
Fig. 1: Database management panel in IntelliJ IDEA, creating a connection to PostgreSQL

Task 1.3

Verify that your project's pom.xml contains the dependency for the PostgreSQL JDBC driver. Add it if necessary.

Your project already has the PostgreSQL JDBC driver dependency prepared in pom.xml. Verify that it is present in the dependencies section:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

If the dependency is missing, add it. Then open the Maven panel (button on the right edge of the window) and click the reload button so the required libraries are downloaded.

Step 2

Add the interfaces and entities specifying the service components from the archive serviceSpecs.zip to your project. Read the description of the specification carefully. The individual interfaces contain the signatures of the basic methods supported by the services.

Warning

For compatibility with the GameStudio project, do not change the interfaces and do not define any new public methods. Follow the provided service interface specification.

Task 2.1

Implement the entity classes Comment and Rating.

An implementation of the Score class is provided as an example.

Step 3

Task 3.1

Based on the specification, create tables in the database that will contain the service-related data.

The IntelliJ IDEA development environment supports database systems, so you can create tables for service data directly in the IDE. A graphical editor for working with tables is available, but you can also write and execute your own SQL script.

You can add a new table in IntelliJ IDEA through the Database tool window (the button is usually on the right edge of the window). By expanding the name of the connected database, you can access the schema (schemas -> public). Right-clicking it opens a context menu, where you can choose New -> Table.

Creating a database table in IntelliJ IDEA
Fig. 2: Creating a database table in IntelliJ IDEA

A dialog window will then appear, where you can add columns using the button and set their data types (the upper part of the window), or write the command for table creation in the text field (the lower part of the window). When adding columns using the + button, the SQL command in the text field is automatically updated accordingly.

Adding columns to a newly created table
Fig. 3: Adding columns to a newly created table

Comment

When creating the tables, follow the specification of the services. Keep in mind that this specification is general, so when choosing data types for the columns, you must consider those supported specifically by the PostgreSQL database system.

Step 4

Task 4.1

Create JDBC implementations of the service interfaces in the service package with the class names RatingServiceJDBC and CommentServiceJDBC. Use the example of ScoreServiceJDBC as inspiration.

To implement the services, you will need:

  • database connection credentials (URL, login, password),
  • SQL commands that will be used in the methods of each service component,
  • implementation of the interface methods together with database connection handling, exception handling, and connection closing.

Comment

You may take inspiration from the lecture implementation of ScoreServiceJDBC. Also note how the score service is integrated into the game in the ConsoleUI class.

Warning

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

Step 5

Task 5.1

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

For example, the score service should be integrated into the game so that the player's score is automatically stored in the database after finishing the game. After that, either automatically or through a user-entered command, a list of the top 10 players and their scores should be printed.

Similarly, integrate the comment and rating services into your game.

Step 6

Task 6.1

Upload (commit and push) your implementation to your GitLab repository. You can update the project as you continue working on it. Before the next exercise, make sure your latest files are in the repository. Also prepare questions you would like to discuss in 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 PostgreSQL data types