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.

Adding the database 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 80+ hands-on prep courses.