Connecting Spring Boot with databases - MySQL and Oracle


This guide will help you understand how to connect Spring Boot with databases like MySql and Oracle.

You will learn

  • How to connect Spring Boot, JPA application with MySQL and Oracle?
  • What should you configure in application.properties?
  • How to setup the database schema?

10 Step Reference Courses

Updating the Spring Boot Project Step By Step

Let’s look at the 5 steps involved in connecting a Spring Boot application to a database.

You can use the example we created earlier for connecting to H2 in memory database as the starting point for this article- http://www.springboottutorial.com/hibernate-jpa-tutorial-with-spring-boot-starter-jpa

Step 1 - Add dependency for your database connector to pom.xml

Example for MySQL is shown below.

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

If you would want to connect to oracle database, you can use a dependency similar to the one shown below.

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc7</artifactId>
    <version>12.1.0.1</version>
</dependency>

Step 2 - Remove H2 Dependency from pom.xml

Or atleast make its scope as test

<!--
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
</dependency>
-->

Step 3 - Setup your My SQL Database

We would need to set up your database with a schema and the tables.

For an example, check out - https://github.com/in28minutes/jpa-with-hibernate#installing-and-setting-up-mysql

Step 4 - Configure your connection to Your Database

Configure application.properties to connect to your database.

An example for My SQL is shown below:

spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:3306/todo_example
spring.datasource.username=todouser
spring.datasource.password=YOUR_PASSWORD

spring.jpa.hibernate.ddl-auto

Spring Boot chooses a default value for this based on whether you are connecting to an embedded database or not.

  • Embedded Databases - default create-drop
  • Other Databases - default none

Here is a quick guide to all the options

  • none : No action will be performed.
  • create-only : Database creation will be generated from entities.
  • drop : Database dropping will be generated from entities.
  • create : Database dropping will be generated followed by database creation.
  • validate : Validate entites with the database schema
  • update : Update the database schema based on the entities

Step 5 - Restart and You are ready!

That’s it

Subscribe to get amazing offers on all our courses.

Find out how in28Minutes reached 100,000 Learners on Udemy in 2 years. The in28minutes Way - Our approach to creating awesome learning experiences.

Related Posts

Spring Boot Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. This resources will help you learn and gain expertise at Spring Boot.

Implementing AOP with Spring Boot and AspectJ

Learn how to implement AOP with Spring Boot Starter AOP and AspectJ. You will understand cross cutting concerns and the basics of AOP - Aspect, Pointcut, JoinPoint, Advice and more.

Spring Boot Developer Tools and Live Reload - Maximize productivity with Hot Swapping

When we develop web applications with Java, we had to restart the server to pick up all changes. This kills productivity. Spring Boot Developers Tools provides solutions to automatically pick up changes without a complete server restart. Lets get productive with Spring Boot Developer Tools.

Spring Boot - Unit Testing and Mocking with Mockito and JUnit

Learn the basics of JUnit and Mockito. We will Spring Boot to create a Business Service and Data Services. We will unit test the Business Service using Spring Boot, Mockito and JUnit in two different approaches.

Logging with Spring Boot - Logback, SLF4j and LOG4j2

Learn how to log effectively with Spring Boot. We will look at Spring Boot Starter for Logging and Spring Boot starter for log4j2.

Creating a Spring Boot Project with Eclipse and Maven

Setting up a basic project with Spring Boot is a cake walk. In this article, we will explore the different options of creating Spring Boot Projects with Maven and Eclipse.

Spring Boot - Integrating Static Content - Javascript (JS) and CSS files

Learn how to integrate Spring Boot with Static Content - Javascript (JS) and CSS files . We will set up a simple web application and add the static content for these frameworks.

Spring Boot - Integrating with Bootstrap & jQuery using Web Jars

Learn how to integrate Spring Boot with Bootstrap & jQuery using Web Jars. We will set up a simple web application and add the web jars for these frameworks.

Introduction to Spring Data - Spring Data JPA, Spring Data REST and MongoDB

Spring Data's mission is to provide a familiar and consistent, Spring-based programming model for data access. In this tutorial, we will explore the different interfaces provided by Spring Data. We will take a quick peek into Spring Data JPA and Spring Data for MongoDB.

Introduction to Spring Data Rest - Create RESTful APIs at F1 Speed

Learn to create RESTful Services with Spring Data Rest and Spring Boot. We will create a JPA Project connecting to H2 (in memory database) and add Spring Data JPA Rest Starter to expose API.