Last Updated on 31 December 2020   |   Print Email
In this post, I’ll show you the steps and some code examples for connecting to Oracle database server and executing SQL statements in Spring Boot application. Basically, there are two common ways:
Connect to an Oracle database using Spring JDBC with JdbcTemplate API
Connect to an Oracle database using Spring Data JPA with Hibernate framework
Here are the steps which you need to follow in order to configure a Spring Boot project working with an Oracle database:
Add a dependency for Oracle JDBC driver that connects Java applications with an Oracle database server.
Configure data source properties for the database connection information
Add a dependency for Spring JDBC or Spring Data JPA, depending on your need:
Use Spring JDBC for executing plain SQL statements
Use Spring Data JPA for more advanced use, e.g. mapping Java classes to tables and Java objects to rows, and take advantages of the Spring Data JPA API.
Let’s go into the details below.
1. Declare dependency for Oracle JDBC Driver
Oracle JDBC driver is required at runtime, so you need to add the following dependency to your Maven projectfile:
The Student class is a trivial POJO class with 3 fields id, name and email. Behind the scene, the JdbcTemplate creates database connection when necessary so you can focus on coding your business logic.For details about using Spring JdbcTemplate, I recommend you to read this tutorial.
4. Connect to Oracle Database with Spring Data JPA
In case you need to use Spring Data JPA, add the following dependency to your project:
And then you can use this repository in a Spring MVC controller or business class as follows:
@Controller
public class CustomerController {
@Autowired
private CustomerRepository customerRepo;
@GetMapping("/customers")
public String listAll(Model model) {
List<Customer> listCustomers = customerRepo.findAll();
model.addAttribute("listCustomers", listCustomers);
return "customers";
}
}
I recommend you to follow this article: Understand Spring Data JPA with Simple Example to learn more about Spring Data JPA.Those are some code examples for connecting to Oracle database in Spring Boot. As you have seen, Spring Boot greatly simplifies the programming, and you can choose to use Spring JDBC or Spring Data JPA.Watch the following video to see the coding in action:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Hi, do you have the working source code for download? Something is missing I think. I'm getting an error: "Error creating bean with name 'dataSource' defined in class path resource". Thanks!
Hi, im tring to do a simple select with JDBC driver as you do it and it works but when i try to do the same in other class different that main class jdbcTemplate is null and the jdbcTemplate.query trew java.lang.NullPointerException i tried using @Service and @Component in that class but same result :S can u help me pls
Comments
i tried using @Service and @Component in that class but same result :S can u help me pls