|
| 1 | +/* |
| 2 | + Copyright (c) 2024, 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.jdbc.reactive; |
| 23 | + |
| 24 | +import java.sql.SQLException; |
| 25 | +import java.util.concurrent.Flow; |
| 26 | +import java.util.concurrent.SubmissionPublisher; |
| 27 | +import java.util.stream.IntStream; |
| 28 | + |
| 29 | +import oracle.jdbc.OracleConnection; |
| 30 | +import oracle.jdbc.OraclePreparedStatement; |
| 31 | +import oracle.jdbc.OracleResultSet; |
| 32 | +import oracle.jdbc.OracleStatement; |
| 33 | + |
| 34 | +public class SQLStatementWithAsynchronousJDBC { |
| 35 | + |
| 36 | + public static void main(String[] args) throws InterruptedException { |
| 37 | + SQLStatementWithAsynchronousJDBC asyncSQL = new SQLStatementWithAsynchronousJDBC(); |
| 38 | + try (OracleConnection conn = DatabaseConfig.getConnection()) { |
| 39 | + asyncSQL.createTable(conn); |
| 40 | + IntStream.rangeClosed(0, 3).forEach(i -> asyncSQL.insertData(conn, i, "Java " + i, "Duke " + i)); |
| 41 | + asyncSQL.readData(conn); |
| 42 | + Thread.sleep(5000); |
| 43 | + asyncSQL.dropTable(conn); |
| 44 | + } catch (SQLException e) { |
| 45 | + e.printStackTrace(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private Flow.Publisher<Boolean> insertData(OracleConnection connection, int id, String firstName, String lastName) { |
| 50 | + try { |
| 51 | + final OraclePreparedStatement insertStatement = (OraclePreparedStatement) connection |
| 52 | + .prepareStatement("INSERT INTO employee_names (id, first_name, last_name) VALUES (?, ?, ?)"); |
| 53 | + insertStatement.setInt(1, id); |
| 54 | + insertStatement.setString(2, firstName); |
| 55 | + insertStatement.setString(3, lastName); |
| 56 | + |
| 57 | + Flow.Publisher<Boolean> insertPublisher = insertStatement.unwrap(OraclePreparedStatement.class) |
| 58 | + .executeAsyncOracle(); |
| 59 | + |
| 60 | + insertPublisher.subscribe(new Flow.Subscriber<Boolean>() { |
| 61 | + private Flow.Subscription subscription; |
| 62 | + |
| 63 | + public void onSubscribe(Flow.Subscription subscription) { |
| 64 | + this.subscription = subscription; |
| 65 | + subscription.request(1L); |
| 66 | + } |
| 67 | + |
| 68 | + public void onNext(Boolean item) { |
| 69 | + } |
| 70 | + |
| 71 | + public void onError(Throwable throwable) { |
| 72 | + closeStatement(); |
| 73 | + throwable.printStackTrace(); |
| 74 | + } |
| 75 | + |
| 76 | + public void onComplete() { |
| 77 | + closeStatement(); |
| 78 | + } |
| 79 | + |
| 80 | + void closeStatement() { |
| 81 | + try { |
| 82 | + if (insertStatement != null && !insertStatement.isClosed()) { |
| 83 | + insertStatement.close(); |
| 84 | + } |
| 85 | + } catch (SQLException closeException) { |
| 86 | + closeException.printStackTrace(); |
| 87 | + } |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + return insertPublisher; |
| 92 | + } catch (SQLException e) { |
| 93 | + e.printStackTrace(); |
| 94 | + SubmissionPublisher<Boolean> publisher = new SubmissionPublisher<>(); |
| 95 | + publisher.close(); |
| 96 | + return publisher; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public Flow.Publisher<OracleResultSet> readData(OracleConnection connection) { |
| 101 | + try { |
| 102 | + final OraclePreparedStatement readStatement = (OraclePreparedStatement) connection |
| 103 | + .prepareStatement("SELECT * FROM employee_names WHERE first_name LIKE ?"); |
| 104 | + readStatement.setString(1, "Jav%"); |
| 105 | + |
| 106 | + Flow.Publisher<OracleResultSet> readPublisher = readStatement.unwrap(OraclePreparedStatement.class) |
| 107 | + .executeQueryAsyncOracle(); |
| 108 | + |
| 109 | + readPublisher.subscribe(new Flow.Subscriber<OracleResultSet>() { |
| 110 | + private Flow.Subscription subscription; |
| 111 | + |
| 112 | + public void onSubscribe(Flow.Subscription subscription) { |
| 113 | + this.subscription = subscription; |
| 114 | + subscription.request(Long.MAX_VALUE); |
| 115 | + } |
| 116 | + |
| 117 | + public void onNext(OracleResultSet resultSet) { |
| 118 | + try { |
| 119 | + while (resultSet.next()) { |
| 120 | + int id = resultSet.getInt("id"); |
| 121 | + String firstName = resultSet.getString("first_name"); |
| 122 | + String lastName = resultSet.getString("last_name"); |
| 123 | + System.out.println("ID: " + id + ", First Name: " + firstName + ", Last Name: " + lastName); |
| 124 | + } |
| 125 | + System.out.println("Finished receiving stream data successfully. \nPreparing to drop table..."); |
| 126 | + } catch (SQLException e) { |
| 127 | + onError(e); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public void onError(Throwable throwable) { |
| 132 | + closeStatement(); |
| 133 | + throwable.printStackTrace(); |
| 134 | + } |
| 135 | + |
| 136 | + public void onComplete() { |
| 137 | + closeStatement(); |
| 138 | + } |
| 139 | + |
| 140 | + void closeStatement() { |
| 141 | + try { |
| 142 | + if (readStatement != null && !readStatement.isClosed()) { |
| 143 | + readStatement.close(); |
| 144 | + } |
| 145 | + } catch (SQLException closeException) { |
| 146 | + closeException.printStackTrace(); |
| 147 | + } |
| 148 | + } |
| 149 | + }); |
| 150 | + return readPublisher; |
| 151 | + } catch (SQLException e) { |
| 152 | + e.printStackTrace(); |
| 153 | + SubmissionPublisher<OracleResultSet> publisher = new SubmissionPublisher<>(); |
| 154 | + publisher.close(); |
| 155 | + return publisher; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private void createTable(OracleConnection connection) { |
| 160 | + String createTableSQL = "CREATE TABLE employee_names (id NUMBER PRIMARY KEY, first_name VARCHAR2(50), last_name VARCHAR2(50))"; |
| 161 | + try (OracleStatement createTableStatement = (OracleStatement) connection.createStatement()) { |
| 162 | + createTableStatement.execute(createTableSQL); |
| 163 | + System.out.println("Table 'employee_names' created successfully."); |
| 164 | + } catch (SQLException e) { |
| 165 | + e.printStackTrace(); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private void dropTable(OracleConnection connection) { |
| 170 | + String dropTableSQL = "DROP TABLE employee_names"; |
| 171 | + try (OracleStatement dropTableStatement = (OracleStatement) connection.createStatement()) { |
| 172 | + dropTableStatement.execute(dropTableSQL); |
| 173 | + System.out.println("Table 'employee_names' dropped successfully."); |
| 174 | + } catch (SQLException e) { |
| 175 | + e.printStackTrace(); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments