JDBC with Oracle
Start a Spring Web project and add dependencies to pom.xmlPom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
Config
Add datasource configuration properties.
spring.datasource.url = jdbc:oracle:thin:@mydb-ora.example.com:2482:MYDB.exa.example.com
spring.datasource.username = myuser
spring.datasource.password = mypass
Application class
package com.example.jdbc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApp {
public void main(String[] args) {
SpringApplication.run(DemoApp.class);
}
}
Rest Controller
package com.example.jdbc.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
import java.util.Map;
@RestController
public class DemoController {
@Autowired
private JdbcTemplate jdbcTemplate;
@GetMapping("/api/date")
public Object getdates() {
String sql = "select to_char(sysdate, 'yyyy-MM-dd') as current_date from dual";
List<Map<String, Object>> data = jdbcTemplate.queryForList(sql);
return data;
}
}