Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Sample for spring_jdbc Getting started #198

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions java/spring_jdbc_example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SpringBoot JDBC Code Sample

This is the code sample that accompanies the "Getting Started with SpringBoot & JDBC"
video. This showcases using JDBC with UCP in a simple application, utilizing JPA to
retrieve the data from the Autonomous Database.

# Main Components of the Code

* TestController.java: Has one get mapping that uses StudentService to retrieve all the Students
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you plz rework this sentence?

* Student.java: Models the Student class to the Students table in the database
* StudentRepository.java: Extends JpaRepository to utilize built-in queries
* StudentService.java: Implements one method that retrieves all students
* application.properties: provides database connection information along with UCP

50 changes: 50 additions & 0 deletions java/spring_jdbc_example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11-production</artifactId>
<version>21.1.0.0</version>
<type>pom</type>
</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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
Licensed under the Universal Permissive License v 1.0
as shown at http://oss.oracle.com/licenses/upl */

/*
DESCRIPTION
This is the main class that runs the springboot application

Peter Song 05/11/2022 - Creation
*/

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
Licensed under the Universal Permissive License v 1.0
as shown at http://oss.oracle.com/licenses/upl */

/*
DESCRIPTION
The code sample maps uses the jpaRepository to return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rework this sentence?

all students

Peter Song 05/11/2022 - Creation
*/

package com.example.demo.Service;


import com.example.demo.model.Student;
import com.example.demo.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
public List<Student> findAll(){
List<Student> students = studentRepository.findAll();
return students;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
Licensed under the Universal Permissive License v 1.0
as shown at http://oss.oracle.com/licenses/upl */

/*
DESCRIPTION
This code sample calls upon a rest endpoint to retrieve all students.
It is part of the greater

peter song 05/11/2022 - Creation
*/

package com.example.demo.controller;


import com.example.demo.Service.StudentService;
import com.example.demo.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TestController {
@Autowired
private StudentService studentService;
@GetMapping(value = "/students")
public List<Student> getAllStudents(){
return studentService.findAll();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
Licensed under the Universal Permissive License v 1.0
as shown at http://oss.oracle.com/licenses/upl */

/*
DESCRIPTION
The code sample maps the students table from Autonomous Database
to this students class, using JPA

Peter Song 05/11/2022 - Creation
*/

package com.example.demo.model;


import javax.persistence.*;

@Entity
@Table(name = "Students")
public class Student {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int ID;
@Column(name = "first_name")
String first_name;
@Column(name = "last_name")
String last_name;
public Student(){

}
public Student(int ID, String first_name, String last_name) {
this.ID = ID;
this.first_name = first_name;
this.last_name = last_name;
}

public int getID() {
return ID;
}

public void setID(int ID) {
this.ID = ID;
}

public String getFirst_name() {
return first_name;
}

public void setFirst_name(String first_name) {
this.first_name = first_name;
}

public String getLast_name() {
return last_name;
}

public void setLast_name(String last_name) {
this.last_name = last_name;
}

@Override
public String toString() {
return "Student{" +
"ID=" + ID +
", first_name='" + first_name + '\'' +
", last_name='" + last_name + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
Licensed under the Universal Permissive License v 1.0
as shown at http://oss.oracle.com/licenses/upl */

/*
DESCRIPTION
The code sample extends the JpaRepository
to take advantage of built-in queries provided
by JPA

Peter Song 05/11/2022 - Creation
*/

package com.example.demo.repository;


import com.example.demo.model.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.transaction.Transactional;

@Repository
@Transactional
@EnableTransactionManagement
public interface StudentRepository extends JpaRepository<Student, Integer> {


}
26 changes: 26 additions & 0 deletions java/spring_jdbc_example/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
#Licensed under the Universal Permissive License v 1.0
#as shown at http://oss.oracle.com/licenses/upl */
#
#
#DESCRIPTION
# This application.properties class provides the relevant information to connect
# to an Oracle Autonomous Database.
#Peter Song 05/11/2022 - Creation
#

spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@adbps_medium?TNS_ADMIN=/Users/psong/Desktop/JDBC_vid_series/Wallet_ADBPS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a placeholder just like we have for username and password.

spring.datasource.username=ADMIN
spring.datasource.password=WELcome__12345
spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect
oracle.jdbc.fanEnabled=false

#UCP Properties
spring.datasource.type=oracle.ucp.jdbc.PoolDataSource
spring.datasource.oracleucp.connection-factory-class-name=oracle.jdbc.pool.OracleDataSource
spring.datasource.oracleucp.sql-for-validate-connection=select * from dual
spring.datasource.oracleucp.connection-pool-name=connectionPoolName1
spring.datasource.oracleucp.initial-pool-size=15
spring.datasource.oracleucp.min-pool-size=10
spring.datasource.oracleucp.max-pool-size=30
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {

@Test
void contextLoads() {
}

}