Skip to content

Commit 95c24aa

Browse files
authored
Merge pull request oracle-samples#150 from oracle/SpringBootApp
New Spring boot app
2 parents 50a68a5 + 6c48cb9 commit 95c24aa

File tree

12 files changed

+549
-0
lines changed

12 files changed

+549
-0
lines changed

java/SpringBootApp/Readme.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Spring Boot Sample
2+
This is a SpringBoot sample that uses Oracle JDBC and UCP to connect to Autonomous Database (ATP/ADW). The sample creates a SpringBoot application and performs different JDBC operations.
3+
4+
* ALL_TABLES: Displays 20 records from the ALL_TABLES table.
5+
* EMP: Displays the records from EMP table. You can create EMP and other tables and populate the data using the script [JDBCSampleData.sql](https://github.com/oracle/oracle-db-examples/blob/master/java/jdbc/BasicSamples/JDBCSampleData.sql)
6+
* Insert into EMP: Insert a new record into the table. The new row will not be deleted. It displays all the records of EMP table and you can see the new row that was created.
7+
8+
# Main Components of the code
9+
* **OracleJdbcAppication.java**: This is the main class where we have the methods to perform some database operations.
10+
* **EmployeeServiceImpl.java**: This is the bean where the business logic is present. It implements the interface EmployeeService.java.
11+
* **Employee.java and AllTables.java**: These are the DAO classes that represent 'Model' of the application.
12+
* **EmployeeDAOImpl.java and AllTablesDAOImpl.java**: These are the Data Access Object classes to access the data from the database.
13+
* **pom.xml**: Maven build script with all the necessary dependencies for the application to run.
14+
* **application.properties**: Contains all the database specific details such as database URL, database username, database password etc., This also contains the properties to UCP as a datasource. Make sure to have Oracle JDBC 21c driver in the class path to use UCP as a datasource.
15+
16+
## Directory Structure
17+
```
18+
SpringBootSample
19+
├── Readme.md
20+
├── pom.xml
21+
└── src
22+
└── main
23+
├── java
24+
│   └── com
25+
│   └── oracle
26+
│   └── springapp
27+
│   ├── OracleJdbcApplication.java
28+
│   ├── dao
29+
│   │   ├── AllTablesDAO.java
30+
│   │   ├── EmployeeDAO.java
31+
│   │   └── impl
32+
│   │   ├── AllTablesDAOImpl.java
33+
│   │   └── EmployeeDAOImpl.java
34+
│   ├── model
35+
│   │   ├── AllTables.java
36+
│   │   └── Employee.java
37+
│   └── service
38+
│   ├── EmployeeService.java
39+
│   └── impl
40+
│   └── EmployeeServiceImpl.java
41+
└── resources
42+
└── application.properties
43+
```

java/SpringBootApp/pom.xml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.oracle</groupId>
7+
<artifactId>springapp</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<name>springapp</name>
10+
<url>http://www.oracle.com</url>
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>2.2.6.RELEASE</version>
15+
</parent>
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<maven.compiler.source>1.8</maven.compiler.source>
19+
<maven.compiler.target>1.8</maven.compiler.target>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>junit</groupId>
24+
<artifactId>junit</artifactId>
25+
<scope>test</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-jdbc</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>javax.inject</groupId>
37+
<artifactId>javax.inject</artifactId>
38+
<version>1</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.oracle.database.jdbc</groupId>
42+
<artifactId>ojdbc8-production</artifactId>
43+
<version>21.1.0.0</version>
44+
<type>pom</type>
45+
</dependency>
46+
47+
</dependencies>
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-maven-plugin</artifactId>
53+
</plugin>
54+
<plugin>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-maven-plugin</artifactId>
57+
<configuration>
58+
<mainClass>com.oracle.springapp.OracleJdbcApplication</mainClass>
59+
</configuration>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
</project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.oracle.springapp;
2+
3+
4+
import java.sql.Date;
5+
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.CommandLineRunner;
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
11+
import com.oracle.springapp.model.Employee;
12+
import com.oracle.springapp.service.EmployeeService;
13+
14+
/**
15+
* SpringBoot application main class. It uses JdbcTemplate class which
16+
* internally uses UCP for connection check-outs and check-ins.
17+
*
18+
*/
19+
@SpringBootApplication
20+
public class OracleJdbcApplication implements CommandLineRunner {
21+
22+
@Autowired
23+
EmployeeService employeeService;
24+
25+
public static void main(String[] args) {
26+
SpringApplication.run(OracleJdbcApplication.class, args);
27+
}
28+
29+
@Override
30+
public void run(String... args) throws Exception {
31+
32+
// Displays 20 table names from ALL_TABLES
33+
employeeService.displayTableNames();
34+
System.out.println("List of employees");
35+
// Displays the list of employees from EMP table
36+
employeeService.displayEmployees();
37+
// Insert a new employee into EMP table
38+
employeeService.insertEmployee(new Employee(7954,"TAYLOR","MANAGER",7839, Date.valueOf("2020-03-20"),5300,0,10));
39+
System.out.println("List of Employees after the update");
40+
// Displays the list of employees after a new employee record is inserted
41+
employeeService.displayEmployees();
42+
}
43+
44+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.oracle.springapp.dao;
2+
3+
import java.util.List;
4+
5+
import com.oracle.springapp.model.AllTables;
6+
7+
/**
8+
* Simple DAO interface for EMP table.
9+
*
10+
*/
11+
public interface AllTablesDAO {
12+
public List<AllTables> getTableNames();
13+
}
14+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.oracle.springapp.dao;
2+
3+
import java.util.List;
4+
5+
import com.oracle.springapp.model.Employee;
6+
7+
/**
8+
* Simple DAO interface for EMP table.
9+
*
10+
*/
11+
public interface EmployeeDAO {
12+
public List<Employee> getAllEmployees();
13+
14+
void insertEmployee(Employee employee);
15+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.oracle.springapp.dao.impl;
2+
3+
import java.util.List;
4+
5+
import javax.annotation.PostConstruct;
6+
import javax.sql.DataSource;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.jdbc.core.support.JdbcDaoSupport;
10+
import org.springframework.stereotype.Repository;
11+
12+
import com.oracle.springapp.dao.AllTablesDAO;
13+
import com.oracle.springapp.model.AllTables;
14+
/**
15+
* Simple Java class which uses Spring's JdbcDaoSupport class to implement
16+
* business logic.
17+
*
18+
*/
19+
@Repository
20+
public class AllTablesDAOImpl extends JdbcDaoSupport implements AllTablesDAO {
21+
@Autowired
22+
private DataSource dataSource;
23+
24+
@PostConstruct
25+
public void initialize() {
26+
setDataSource(dataSource);
27+
System.out.println("Datasource used: " + dataSource);
28+
}
29+
30+
@Override
31+
public List<AllTables> getTableNames() {
32+
final String sql = "SELECT owner, table_name, status, num_rows FROM all_tables where rownum < 20";
33+
return getJdbcTemplate().query(sql,
34+
(rs, rowNum) -> new AllTables(rs.getString("owner"),
35+
rs.getString("table_name"),
36+
rs.getString("status"),
37+
rs.getInt("num_rows")
38+
));
39+
40+
}
41+
}
42+
43+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.oracle.springapp.dao.impl;
2+
3+
import java.util.List;
4+
5+
import javax.annotation.PostConstruct;
6+
import javax.sql.DataSource;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.jdbc.core.support.JdbcDaoSupport;
10+
import org.springframework.stereotype.Repository;
11+
12+
import com.oracle.springapp.dao.EmployeeDAO;
13+
import com.oracle.springapp.model.Employee;
14+
15+
/**
16+
* Simple Java class which uses Spring's JdbcDaoSupport class to implement
17+
* business logic.
18+
*
19+
*/
20+
@Repository
21+
public class EmployeeDAOImpl extends JdbcDaoSupport implements EmployeeDAO {
22+
23+
@Autowired
24+
private DataSource dataSource;
25+
26+
@PostConstruct
27+
public void initialize() {
28+
setDataSource(dataSource);
29+
System.out.println("Datasource used: " + dataSource);
30+
}
31+
32+
@Override
33+
public List<Employee> getAllEmployees() {
34+
final String sql = "SELECT empno, ename, job, mgr, hiredate, sal, comm, deptno FROM emp";
35+
return getJdbcTemplate().query(sql,
36+
(rs, rowNum) -> new Employee(rs.getInt("empno"),
37+
rs.getString("ename"),
38+
rs.getString("job"),
39+
rs.getInt("mgr"),
40+
rs.getDate("hiredate"),
41+
rs.getInt("sal"),
42+
rs.getInt("comm"),
43+
rs.getInt("deptno")
44+
));
45+
46+
47+
}
48+
49+
@Override
50+
public void insertEmployee(Employee employee) {
51+
String sql = "Select max(empno) from emp";
52+
int[] emp_no = new int[1];
53+
//getJdbcTemplate().query(sql, (rs, rowNum) -> emp_no[rowNum] = rs.getInt(1));
54+
getJdbcTemplate().query(sql, (resultSet, rowNumber) -> emp_no[rowNumber] = resultSet.getInt(1));
55+
System.out.println("Max emploee number is: " + emp_no[0]);
56+
57+
sql = "INSERT INTO EMP VALUES(?,?,?,?,?,?,?,?)";
58+
int newEmpNo = emp_no[0]+1;
59+
getJdbcTemplate().update(sql, new Object[]{
60+
newEmpNo,
61+
employee.getName()+(newEmpNo),
62+
employee.getJob(),
63+
employee.getManager(),
64+
employee.getHiredate(),
65+
employee.getSalary(),
66+
employee.getCommission(),
67+
employee.getDeptno()
68+
});
69+
}
70+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.oracle.springapp.model;
2+
3+
/**
4+
* Simple model for ALL_TABLES
5+
*/
6+
public class AllTables {
7+
private String owner;
8+
private String table_name;
9+
private String status;
10+
private int num_rows;
11+
12+
13+
public AllTables(String _owner,
14+
String _table_name,
15+
String _status,
16+
int _num_rows) {
17+
owner = _owner;
18+
table_name = _table_name;
19+
status = _status;
20+
num_rows = _num_rows;
21+
}
22+
23+
24+
public String getOwner() {
25+
return owner;
26+
}
27+
28+
public String getTableName() {
29+
return table_name;
30+
}
31+
32+
public String getStatus() {
33+
return status;
34+
}
35+
36+
public int getNumRows() {
37+
return num_rows;
38+
}
39+
40+
public String toString() {
41+
return String.format("%25s %25s %25s %20d", owner, table_name,
42+
status, num_rows);
43+
44+
}
45+
46+
}

0 commit comments

Comments
 (0)