Last active
May 3, 2021 15:33
-
-
Save aalmiray/d592deb0f4aae5f3f21dadf87980ec01 to your computer and use it in GitHub Desktop.
Oracle JDBC + SpringBoot 24.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/XEPDB1 | |
spring.datasource.username=databaseUser | |
spring.datasource.password=$eCr3tP@s$w0rD! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/XEPDB1 | |
spring.datasource.username=databaseUser | |
spring.datasource.password=$eCr3tP@s$w0rD! | |
spring.datasource.type=oracle.ucp.jdbc.PoolDataSource | |
spring.datasource.oracleucp.min-pool-size=5 | |
spring.datasource.oracleucp.max-pool-size=15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.acme.todo01; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.jdbc.core.JdbcTemplate; | |
import org.springframework.jdbc.core.RowMapper; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.Scanner; | |
@SpringBootApplication | |
public class Application implements CommandLineRunner { | |
private static final Logger LOG = LoggerFactory.getLogger(Application.class); | |
@Autowired | |
private JdbcTemplate jdbcTemplate; | |
public static void main(String[] args) { | |
SpringApplication.run(Application.class, args); | |
} | |
@Override | |
public void run(String... args) throws Exception { | |
URL url = Application.class.getClassLoader().getResource("com/acme/todo01/schema.ddl"); | |
try (Scanner sc = new Scanner(url.openStream())) { | |
sc.useDelimiter("#"); | |
while (sc.hasNext()) { | |
String line = sc.next().trim(); | |
jdbcTemplate.execute(line); | |
} | |
} catch (IOException e) { | |
LOG.error("An error occurred when reading schema DDL from " + url, e); | |
} | |
List<Object[]> descriptions = Arrays.asList( | |
new Object[]{"Javadoc"}, | |
new Object[]{"Application"} | |
); | |
jdbcTemplate.batchUpdate("INSERT INTO todos(description, done) VALUES (?, 0)", descriptions); | |
jdbcTemplate.query("SELECT * FROM todos", (RowMapper<Object>) (rs, i) -> new Todo(rs.getLong("id"), rs.getString("description"))) | |
.forEach(System.out::println); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>2.4.5</version> | |
</parent> | |
<groupId>com.acme</groupId> | |
<artifactId>springboot-todo01</artifactId> | |
<version>1.0.0-SNAPSHOT</version> | |
<name>todo01</name> | |
<description>Todo App</description> | |
<properties> | |
<java.version>11</java.version> | |
<maven.compiler.release>${java.version}</maven.compiler.release> | |
<oracle.jdbc.version>21.1.0.0</oracle.jdbc.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-jdbc</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>com.oracle.database.jdbc</groupId> | |
<artifactId>ojdbc8</artifactId> | |
<version>${oracle.jdbc.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>com.oracle.database.jdbc</groupId> | |
<artifactId>ucp</artifactId> | |
<version>${oracle.jdbc.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. ____ _ __ _ _ | |
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ | |
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ | |
\\/ ___)| |_)| | | | | || (_| | ) ) ) ) | |
' |____| .__|_| |_|_| |_\__, | / / / / | |
=========|_|==============|___/=/_/_/_/ | |
:: Spring Boot :: (v2.4.5) | |
INFO 8620 --- [ main] com.acme.todo01.Application : Starting Application using Java 11.0.11 on aalmiray-work.local with PID 8620 (/Users/aalmiray/springboot-todo01/target/classes started by aalmiray in /Users/aalmiray/springboot-todo01) | |
INFO 8620 --- [ main] com.acme.todo01.Application : No active profile set, falling back to default profiles: default | |
INFO 8620 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode. | |
INFO 8620 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 8 ms. Found 0 JDBC repository interfaces. | |
INFO 8620 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... | |
INFO 8620 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed. | |
INFO 8620 --- [ main] com.acme.todo01.Application : Started Application in 1.117 seconds (JVM running for 1.67) | |
Todo[id=1, description='Javadoc', done='false'] | |
Todo[id=2, description='Application', done='false'] | |
INFO 8620 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... | |
INFO 8620 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. | |
Process finished with exit code 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. ____ _ __ _ _ | |
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ | |
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ | |
\\/ ___)| |_)| | | | | || (_| | ) ) ) ) | |
' |____| .__|_| |_|_| |_\__, | / / / / | |
=========|_|==============|___/=/_/_/_/ | |
:: Spring Boot :: (v2.4.5) | |
INFO 9099 --- [ main] com.acme.todo01.Application : Starting Application using Java 11.0.11 on aalmiray-work.local with PID 9099 (/Users/aalmiray/springboot-todo01/target/classes started by aalmiray in /Users/aalmiray/springboot-todo01) | |
INFO 9099 --- [ main] com.acme.todo01.Application : No active profile set, falling back to default profiles: default | |
INFO 9099 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode. | |
INFO 9099 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 2 ms. Found 0 JDBC repository interfaces. | |
INFO 9099 --- [ main] com.acme.todo01.Application : Started Application in 1.272 seconds (JVM running for 1.785) | |
Todo[id=1, description='Javadoc', done='false'] | |
Todo[id=2, description='Application', done='false'] | |
Process finished with exit code 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.acme.todo01; | |
public class Todo { | |
private Long id; | |
private String description; | |
private boolean done; | |
public Todo() { | |
} | |
public Todo(Long id, String description) { | |
this.id = id; | |
this.description = description; | |
} | |
@Override | |
public String toString() { | |
return String.format( | |
"Todo[id=%d, description='%s', done='%s']", | |
id, description, done); | |
} | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
public String getDescription() { | |
return description; | |
} | |
public void setDescription(String description) { | |
this.description = description; | |
} | |
public boolean isDone() { | |
return done; | |
} | |
public void setDone(boolean done) { | |
this.done = done; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment