Skip to content

Commit a616702

Browse files
Added sample code - ODSA for Java Devs - JDBC with FaaS P5 (#250)
1 parent 5193132 commit a616702

File tree

7 files changed

+299
-0
lines changed

7 files changed

+299
-0
lines changed

cd

Whitespace-only changes.

java/odsa-faas/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Java - Oracle Developers on Medium.com
2+
[ODSA for Java Developers (Part 5) - Connecting to Oracle ADB from a FaaS App with Oracle JDBC on Azure Functions](https://medium.com/@juarezjunior/odsa-for-java-developers-part-5-connecting-to-oracle-adb-from-a-faas-app-with-spring-data-jpa-5083793ec168)

java/odsa-faas/host.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"version": "2.0",
3+
"extensionBundle": {
4+
"id": "Microsoft.Azure.Functions.ExtensionBundle",
5+
"version": "[3.*, 4.0.0)"
6+
}
7+
}

java/odsa-faas/pom.xml

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.oracle.dev.jdbc</groupId>
6+
<artifactId>odsa-faas</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>Azure Java Functions</name>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<java.version>17</java.version>
15+
<azure.functions.maven.plugin.version>1.22.0</azure.functions.maven.plugin.version>
16+
<azure.functions.java.library.version>2.2.0</azure.functions.java.library.version>
17+
<functionAppName><YOUR_AZURE_FUNCTION_APP_NAME></functionAppName>
18+
</properties>
19+
20+
<dependencies>
21+
<!-- Oracle JDBC JARs -->
22+
<dependency>
23+
<groupId>com.oracle.database.jdbc</groupId>
24+
<artifactId>ojdbc11-production</artifactId>
25+
<version>21.7.0.0</version>
26+
<type>pom</type>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>com.microsoft.azure.functions</groupId>
31+
<artifactId>azure-functions-java-library</artifactId>
32+
<version>${azure.functions.java.library.version}</version>
33+
</dependency>
34+
<!-- Test -->
35+
<dependency>
36+
<groupId>org.junit.jupiter</groupId>
37+
<artifactId>junit-jupiter</artifactId>
38+
<version>5.4.2</version>
39+
<scope>test</scope>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.mockito</groupId>
44+
<artifactId>mockito-core</artifactId>
45+
<version>2.23.4</version>
46+
<scope>test</scope>
47+
</dependency>
48+
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-compiler-plugin</artifactId>
56+
<version>3.8.1</version>
57+
<configuration>
58+
<source>${java.version}</source>
59+
<target>${java.version}</target>
60+
<encoding>${project.build.sourceEncoding}</encoding>
61+
</configuration>
62+
</plugin>
63+
<plugin>
64+
<groupId>com.microsoft.azure</groupId>
65+
<artifactId>azure-functions-maven-plugin</artifactId>
66+
<version>${azure.functions.maven.plugin.version}</version>
67+
<configuration>
68+
<!-- function app name -->
69+
<appName>${functionAppName}</appName>
70+
<!-- function app resource group -->
71+
<resourceGroup><YOUR_AZURE_RESOURCE_GROUP></resourceGroup>
72+
<!-- function app service plan name -->
73+
<appServicePlanName><YOUR_AZURE_APP_SERVICE_PLAN_NAME></appServicePlanName>
74+
<!-- function app region-->
75+
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details#supported-regions for all valid values -->
76+
<region><YOUR_AZURE_REGION></region>
77+
<!-- function pricingTier, default to be consumption if not specified -->
78+
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details#supported-pricing-tiers for all valid values -->
79+
<!-- <pricingTier></pricingTier> -->
80+
<!-- Whether to disable application insights, default is false -->
81+
<!-- refers https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details for all valid configurations for application insights-->
82+
<!-- <disableAppInsights></disableAppInsights> -->
83+
<runtime>
84+
<!-- runtime os, could be windows, linux or docker-->
85+
<os>windows</os>
86+
<javaVersion>17</javaVersion>
87+
</runtime>
88+
<appSettings>
89+
<property>
90+
<name>FUNCTIONS_EXTENSION_VERSION</name>
91+
<value>~4</value>
92+
</property>
93+
</appSettings>
94+
</configuration>
95+
<executions>
96+
<execution>
97+
<id>package-functions</id>
98+
<goals>
99+
<goal>package</goal>
100+
</goals>
101+
</execution>
102+
</executions>
103+
</plugin>
104+
<!--Remove obj folder generated by .NET SDK in maven clean-->
105+
<plugin>
106+
<artifactId>maven-clean-plugin</artifactId>
107+
<version>3.1.0</version>
108+
<configuration>
109+
<filesets>
110+
<fileset>
111+
<directory>obj</directory>
112+
</fileset>
113+
</filesets>
114+
</configuration>
115+
</plugin>
116+
</plugins>
117+
</build>
118+
</project>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Copyright (c) 2021, 2022, Oracle and/or its affiliates.
3+
4+
This software is dual-licensed to you under the Universal Permissive License
5+
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License
6+
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
either license.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
https://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package com.oracle.dev.jdbc;
23+
24+
import java.io.IOException;
25+
import java.io.InputStream;
26+
import java.sql.Connection;
27+
import java.sql.DatabaseMetaData;
28+
import java.sql.PreparedStatement;
29+
import java.sql.ResultSet;
30+
import java.sql.SQLException;
31+
import java.util.Properties;
32+
33+
import oracle.jdbc.OracleConnection;
34+
import oracle.jdbc.pool.OracleDataSource;
35+
36+
public class JdbcConnectionToOracleAtpOnOdsa {
37+
38+
private String url;
39+
private String databaseUser;
40+
private String databasePassword;
41+
private String databaseQueryResults;
42+
private final String queryStatement = "SELECT CUST_CITY FROM SH.CUSTOMERS WHERE CUST_FIRST_NAME = ? AND CUST_YEAR_OF_BIRTH = ?";
43+
44+
public JdbcConnectionToOracleAtpOnOdsa() {
45+
ClassLoader classLoader = getClass().getClassLoader();
46+
InputStream inputStream = classLoader.getResourceAsStream("config.properties");
47+
Properties jdbcConnectionProperties = new Properties();
48+
try {
49+
jdbcConnectionProperties.load(inputStream);
50+
} catch (IOException e) {
51+
e.printStackTrace();
52+
}
53+
url = jdbcConnectionProperties.getProperty("DB_URL");
54+
databaseUser = jdbcConnectionProperties.getProperty("DB_USER");
55+
databasePassword = jdbcConnectionProperties.getProperty("DB_PASSWORD");
56+
}
57+
58+
public String runQuery(String name, int year) throws SQLException {
59+
Properties info = new Properties();
60+
info.put(OracleConnection.CONNECTION_PROPERTY_USER_NAME, databaseUser);
61+
info.put(OracleConnection.CONNECTION_PROPERTY_PASSWORD, databasePassword);
62+
info.put(OracleConnection.CONNECTION_PROPERTY_FAN_ENABLED, false);
63+
OracleDataSource ods = new OracleDataSource();
64+
ods.setURL(url);
65+
ods.setConnectionProperties(info);
66+
OracleConnection connection = (OracleConnection) ods.getConnection();
67+
DatabaseMetaData dbmd = connection.getMetaData();
68+
System.out.println("Driver Name: " + dbmd.getDriverName());
69+
System.out.println("Driver Version: " + dbmd.getDriverVersion());
70+
System.out.println();
71+
try {
72+
databaseQueryResults = doSQLWork(connection, queryStatement, name, year);
73+
} catch (SQLException ex) {
74+
ex.printStackTrace();
75+
}
76+
return databaseQueryResults;
77+
}
78+
79+
private String doSQLWork(Connection conn, String queryStatement, String name, int year) throws SQLException {
80+
conn.setAutoCommit(false);
81+
StringBuilder queryResult = new StringBuilder();
82+
PreparedStatement statement = conn.prepareStatement(queryStatement);
83+
statement.setString(1, name);
84+
statement.setInt(2, year);
85+
ResultSet resultSet = statement.executeQuery();
86+
while (resultSet.next()) {
87+
queryResult.append(resultSet.getString(1)).toString();
88+
}
89+
statement.close();
90+
resultSet.close();
91+
return queryResult.toString();
92+
}
93+
94+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright (c) 2021, 2022, Oracle and/or its affiliates.
3+
4+
This software is dual-licensed to you under the Universal Permissive License
5+
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License
6+
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
7+
either license.
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
https://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package com.oracle.dev.jdbc;
23+
24+
import java.sql.SQLException;
25+
import java.util.Map;
26+
import java.util.Optional;
27+
28+
import com.microsoft.azure.functions.ExecutionContext;
29+
import com.microsoft.azure.functions.HttpMethod;
30+
import com.microsoft.azure.functions.HttpRequestMessage;
31+
import com.microsoft.azure.functions.HttpResponseMessage;
32+
import com.microsoft.azure.functions.HttpStatus;
33+
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
34+
import com.microsoft.azure.functions.annotation.FunctionName;
35+
import com.microsoft.azure.functions.annotation.HttpTrigger;
36+
37+
/**
38+
* Azure JDBC Function with HTTP Trigger.
39+
*/
40+
public class JdbcFunction {
41+
/**
42+
* This function listens at endpoint "/api/JdbcFunction". Two ways to invoke it
43+
* using "curl" command in bash: 1. curl -d "HTTP Body" {your
44+
* host}/api/JdbcFunction 2. curl "{your
45+
* host}/api/JdbcFunction?name=<QUERY_NAME>&year=<QUERY_YEAR>"
46+
*/
47+
@FunctionName("JdbcFunction")
48+
public HttpResponseMessage run(@HttpTrigger(name = "req", methods = { HttpMethod.GET,
49+
HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
50+
final ExecutionContext context) {
51+
context.getLogger().info("JdbcFunction - Java HTTP trigger processed a request.");
52+
Map<String, String> httpRequestQueryParameters = request.getQueryParameters();
53+
Optional<String> httpRequestBody = request.getBody();
54+
String nameQueryString = httpRequestQueryParameters.get("name");
55+
String nameBody = httpRequestBody.orElse(nameQueryString);
56+
String yearQueryString = httpRequestQueryParameters.get("year");
57+
String yearBody = httpRequestBody.orElse(yearQueryString);
58+
String databaseQueryResults = null;
59+
if (nameBody == null || yearBody == null) {
60+
return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
61+
.body("Please pass a name and a year on the query string or in the request body").build();
62+
} else {
63+
JdbcConnectionToOracleAtpOnOdsa jdbc = new JdbcConnectionToOracleAtpOnOdsa();
64+
try {
65+
databaseQueryResults = jdbc.runQuery(nameQueryString, Integer.parseInt(yearQueryString));
66+
context.getLogger().info("JDBC Query Results: " + databaseQueryResults);
67+
} catch (SQLException e) {
68+
e.printStackTrace();
69+
}
70+
return request.createResponseBuilder(HttpStatus.OK).body("The city is: " + databaseQueryResults).build();
71+
}
72+
}
73+
74+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# https://docs.oracle.com/en-us/iaas/autonomous-database-shared/doc/support-tls-mtls-authentication.html
2+
DB_URL=<DB_URL>
3+
DB_USER=<DB_USER>
4+
DB_PASSWORD=<DB_PASSWORD>

0 commit comments

Comments
 (0)