Connecting to Other Databases

Learn about the changes required to change the data source.

An in-memory H2 database is easy to set up and use. Now it’s time to learn how to switch to another database. In real-life applications, we might want to connect to Oracle, MySql, or SQL server databases. Using Spring Boot, switching databases is very straightforward. We just need to add some dependencies and change some property values.

This lesson assumes that the database is installed and the schema is properly populated.

1. Replacing the H2 dependency

The first step is to remove the H2 dependency from pom.xml and replace it with the dependency of the database we wish to connect to. Suppose we wish to connect to MySql. The dependency for MySql is given below. Dependencies for other databases are available online.

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