SELECT Query

Learn how to write queries to return all rows and the others to return rows based on some criterion.

JDBC involves a lot of boilerplate code that is required just to get the application working. It is a tedious task to write a simple query using JDBC. There are a number of steps that are required to interact with the database.

  • The first step is establishing a connection

  • The second step is creating a prepared statement or query

  • The third step is to execute the query

  • The fourth step is looping through the result set to get the objects

  • The fifth and final step is to close the connection

Spring JDBC support makes interacting with databases easy. Since we are using Spring Boot, the connection part is automatically taken care of and the data source is automatically configured. Likewise, the connection is automatically closed. Spring provides JdbcTemplate, which makes it easy to write and execute a query. It also provides the BeanPropertyRowMapper which maps rows of a table to a bean.

Press + to interact
Spring JdbcTemplate and RowMapper simplify database operations
Spring JdbcTemplate and RowMapper simplify database operations

In this lesson, first we will write a query to return all rows from the Player table. We will learn how to map the data coming from the database to a bean in our application. Next, we will modify our query to return the row that matches an input argument.

Defining Player bean

We have created a Player table but we need to define a class ...