diff --git a/java/jdbc-reactive-extensions-intro/pom.xml b/java/jdbc-reactive-extensions-intro/pom.xml new file mode 100644 index 00000000..02d46333 --- /dev/null +++ b/java/jdbc-reactive-extensions-intro/pom.xml @@ -0,0 +1,79 @@ + + + + 4.0.0 + + com.oracle.jdbc.reactive + jdbc-reactive-extensions-intro + 1.0-SNAPSHOT + + jdbc-reactive-extensions-intro + Introduction to JDBC Reactive Extensions with the Oracle + Database + + https://juarezjunior.medium.com/introduction-to-jdbc-reactive-extensions-with-the-oracle-database-23c-free-developer-release-b5a64ac5d194/ + + + UTF-8 + 21 + 21 + + + + + com.oracle.database.jdbc + ojdbc11-production + 23.4.0.24.05 + pom + + + + + + + + maven-clean-plugin + 3.1.0 + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + + + diff --git a/java/jdbc-reactive-extensions-intro/src/main/java/com/oracle/jdbc/reactive/SQLStatementWithAsynchronousJDBC.java b/java/jdbc-reactive-extensions-intro/src/main/java/com/oracle/jdbc/reactive/SQLStatementWithAsynchronousJDBC.java new file mode 100644 index 00000000..9748b905 --- /dev/null +++ b/java/jdbc-reactive-extensions-intro/src/main/java/com/oracle/jdbc/reactive/SQLStatementWithAsynchronousJDBC.java @@ -0,0 +1,97 @@ +/* + Copyright (c) 2024, Oracle and/or its affiliates. + + This software is dual-licensed to you under the Universal Permissive License + (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License + 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose + either license. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.oracle.jdbc.reactive; + +import java.sql.SQLException; +import java.util.concurrent.Flow; + +import oracle.jdbc.OracleConnection; +import oracle.jdbc.OraclePreparedStatement; +import oracle.jdbc.pool.OracleDataSource; + +public class SQLStatementWithAsynchronousJDBC { + + public static void main(String[] args) throws SQLException { + OracleDataSource ods = new OracleDataSource(); + // jdbc:oracle:thin@[hostname]:[port]/[DB service/name] + ods.setURL("jdbc:oracle:thin@[hostname]:[port]/[DB service/name"); + ods.setUser("[Username]"); + ods.setPassword("[Password]"); + OracleConnection conn = (OracleConnection) ods.getConnection(); + SQLStatementWithAsynchronousJDBC asyncSQL = new SQLStatementWithAsynchronousJDBC(); + // Execute a SQL DDL statement to create a database table + // asynchronously + asyncSQL.createTable(conn); + } + + /** + * Asynchronously creates a new table by executing a DDL SQL statement + * + * @param connection + * Connection to a database where the table is created + * @return A Publisher that emits the result of executing DDL SQL + * @throws SQLException + * If a database access error occurs before the DDL SQL can be + * executed + */ + private Flow.Publisher createTable(OracleConnection connection) + throws SQLException { + + OraclePreparedStatement createTableStatement = (OraclePreparedStatement) connection + .prepareStatement( + "CREATE TABLE employee_names (" + "id NUMBER PRIMARY KEY, " + + "first_name VARCHAR(50), " + "last_name VARCHAR2(50))"); + + Flow.Publisher createTablePublisher = createTableStatement + .unwrap(OraclePreparedStatement.class).executeAsyncOracle(); + + createTablePublisher.subscribe( + // This subscriber will close the PreparedStatement + new Flow.Subscriber() { + public void onSubscribe(Flow.Subscription subscription) { + subscription.request(1L); + } + + public void onNext(Boolean item) { + } + + public void onError(Throwable throwable) { + closeStatement(); + } + + public void onComplete() { + closeStatement(); + } + + void closeStatement() { + try { + createTableStatement.close(); + } catch (SQLException closeException) { + closeException.printStackTrace(); + } + } + }); + + return createTablePublisher; + } + +} \ No newline at end of file