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.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.