diff --git a/ojdbc-provider-samples/example-configuration.properties b/ojdbc-provider-samples/example-configuration.properties index 524a253e..ceae20c2 100644 --- a/ojdbc-provider-samples/example-configuration.properties +++ b/ojdbc-provider-samples/example-configuration.properties @@ -111,4 +111,9 @@ azure_redirect_url=http://localhost:7071 # Google Cloud Platform gcp_secret_version_name=projects/138028249883/secrets/test-secret/versions/2 gcp_secret_version_name_config=projects/138028249883/secrets/config-secret/versions/4 -gcp_cloud_storage_properties=project=onyx-eye-426013-i5;bucket=fm-test-bucket-123564;object=testObjectStorage.json \ No newline at end of file +gcp_cloud_storage_properties=project=onyx-eye-426013-i5;bucket=fm-test-bucket-123564;object=testObjectStorage.json + +# Jackson OSON samples properties +jackson_oson_url=jdbc:oracle:thin:@... +jackson_oson_username=USERNAME +jackson_oson_password=*********** \ No newline at end of file diff --git a/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/AccessJsonColumnUsingJacksonObjectNode.java b/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/AccessJsonColumnUsingJacksonObjectNode.java index 56ad9e53..997c4fd8 100644 --- a/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/AccessJsonColumnUsingJacksonObjectNode.java +++ b/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/AccessJsonColumnUsingJacksonObjectNode.java @@ -54,7 +54,9 @@ /** *
* A standalone example that inserts and retrieves JSON data to the Oracle database - * using an {@link OsonFactory} and {@link ObjectNode}. + * using an {@link OsonFactory} and {@link ObjectNode}. Recommended for applications + * that already consume {@link ObjectNode}, for other application it is recommended + * to use a POJO instead. *
* The JSON object is created using Jackson API's {@link ObjectNode}. The {@link OsonFactory} * and {@link OsonGenerator} are used to write the JSON Object to an OutputStream. The data diff --git a/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/AccessJsonColumnUsingPOJOAndJsonProvider.java b/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/AccessJsonColumnUsingPOJOAndJsonProvider.java index 122a794a..3058511b 100644 --- a/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/AccessJsonColumnUsingPOJOAndJsonProvider.java +++ b/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/AccessJsonColumnUsingPOJOAndJsonProvider.java @@ -39,10 +39,10 @@ import oracle.jdbc.OracleConnection; import oracle.jdbc.OracleType; -import oracle.jdbc.provider.oson.JacksonOsonProvider; import oracle.jdbc.provider.oson.sample.model.Emp; import java.sql.*; +import java.util.Properties; /** *
@@ -86,10 +86,10 @@ public static void retrieveFromDatabase(Connection conn) throws SQLException {
public static void main(String[] args) {
try {
- // set the system property to the class implementing the "oracle.jdbc.spi.JsonProvider interface"
- System.setProperty(OracleConnection.CONNECTION_PROPERTY_PROVIDER_JSON, JacksonOsonProvider.PROVIDER_NAME);
+ Properties properties = new Properties();
+ properties.setProperty(OracleConnection.CONNECTION_PROPERTY_PROVIDER_JSON, "jackson-json-provider");
- Connection conn = JacksonOsonSampleUtil.createConnection();
+ Connection conn = JacksonOsonSampleUtil.createConnectionWithProperties(properties);
JacksonOsonSampleUtil.createTable(conn);
insertIntoDatabase(conn);
retrieveFromDatabase(conn);
diff --git a/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/JacksonOsonSampleUtil.java b/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/JacksonOsonSampleUtil.java
index 011469b8..f7810d17 100644
--- a/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/JacksonOsonSampleUtil.java
+++ b/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/JacksonOsonSampleUtil.java
@@ -51,6 +51,7 @@
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
+import java.util.Properties;
public class JacksonOsonSampleUtil {
@@ -66,10 +67,19 @@ public static Connection createConnection() throws SQLException {
return ods.getConnection();
}
+ public static Connection createConnectionWithProperties(Properties properties) throws SQLException {
+ OracleDataSource ods = new OracleDataSource();
+ ods.setURL(URL);
+ ods.setUser(USER);
+ ods.setPassword(PASSWORD);
+ ods.setConnectionProperties(properties);
+ return ods.getConnection();
+ }
+
public static void createTable(Connection conn) throws SQLException {
try (Statement stmt = conn.createStatement()) {
stmt.addBatch("drop table if exists jackson_oson_sample");
- stmt.addBatch("create table jackson_oson_sample(id number, json_value JSON) tablespace tbs1");
+ stmt.addBatch("create table jackson_oson_sample(id number, json_value JSON)");
stmt.executeBatch();
}
}
diff --git a/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/NestedStructurePOJO.java b/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/NestedStructurePOJO.java
new file mode 100644
index 00000000..5a8683c6
--- /dev/null
+++ b/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/NestedStructurePOJO.java
@@ -0,0 +1,102 @@
+/*
+ ** Copyright (c) 2024 Oracle and/or its affiliates.
+ **
+ ** The Universal Permissive License (UPL), Version 1.0
+ **
+ ** Subject to the condition set forth below, permission is hereby granted to any
+ ** person obtaining a copy of this software, associated documentation and/or data
+ ** (collectively the "Software"), free of charge and under any and all copyright
+ ** rights in the Software, and any and all patent rights owned or freely
+ ** licensable by each licensor hereunder covering either (i) the unmodified
+ ** Software as contributed to or provided by such licensor, or (ii) the Larger
+ ** Works (as defined below), to deal in both
+ **
+ ** (a) the Software, and
+ ** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
+ ** one is included with the Software (each a "Larger Work" to which the Software
+ ** is contributed by such licensors),
+ **
+ ** without restriction, including without limitation the rights to copy, create
+ ** derivative works of, display, perform, and distribute the Software and make,
+ ** use, sell, offer for sale, import, export, have made, and have sold the
+ ** Software and the Larger Work(s), and to sublicense the foregoing rights on
+ ** either these or other terms.
+ **
+ ** This license is subject to the following condition:
+ ** The above copyright notice and either this complete permission notice or at
+ ** a minimum a reference to the UPL must be included in all copies or
+ ** substantial portions of the Software.
+ **
+ ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ ** SOFTWARE.
+ */
+
+package oracle.jdbc.provider.oson.sample;
+import java.math.BigDecimal;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.time.OffsetDateTime;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+
+import oracle.jdbc.OracleConnection;
+import oracle.jdbc.OracleType;
+import oracle.jdbc.provider.oson.sample.model.Movie;
+import oracle.jdbc.provider.oson.sample.model.Image;
+
+public class NestedStructurePOJO {
+ public static void main(String[] args) throws SQLException {
+ Properties properties = new Properties();
+ properties.setProperty(OracleConnection.CONNECTION_PROPERTY_PROVIDER_JSON, "jackson-json-provider");
+ properties.setProperty(OracleConnection.CONNECTION_PROPERTY_JSON_DEFAULT_GET_OBJECT_TYPE, "oracle.sql.json.OracleJsonValue");
+ try (Connection con = JacksonOsonSampleUtil.createConnectionWithProperties(properties)) {
+ Statement stmt = con.createStatement();
+ stmt.execute("drop table if exists movie");
+ stmt.execute("create json collection table movie");
+
+ Movie matrix = new Movie(
+ 123,
+ "The Matrix",
+ "Sci-fi",
+ BigDecimal.valueOf(353212323),
+ OffsetDateTime.now(),
+ Arrays.asList(
+ new Image("poster.png", "The Matrix"),
+ new Image("showtimes.png", "Matrix Showtimes")
+ )
+ );
+
+ Movie shawshank = new Movie(
+ 456,
+ "The Shawshank Redemption",
+ "Drama",
+ BigDecimal.valueOf(453212345),
+ OffsetDateTime.now(),
+ null
+ );
+
+ PreparedStatement insert = con.prepareStatement("insert into movie values (:1)");
+ insert.setObject(1, matrix, OracleType.JSON);
+ insert.execute();
+
+ insert.setObject(1, shawshank, OracleType.JSON);
+ insert.execute();
+
+ ResultSet rs = stmt.executeQuery("select * from movie m where m.data.title = 'The Matrix'");
+ rs.next();
+ Movie result = rs.getObject(1, Movie.class);
+ System.out.println("Objects are equal: " + matrix.equals(result));
+ }
+ }
+}
diff --git a/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/model/Image.java b/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/model/Image.java
new file mode 100644
index 00000000..153a0090
--- /dev/null
+++ b/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/model/Image.java
@@ -0,0 +1,27 @@
+package oracle.jdbc.provider.oson.sample.model;
+
+import java.util.Objects;
+
+public class Image {
+ public String location;
+ public String description;
+
+ public Image() {}
+ public Image (String location, String description) {
+ this.description = description;
+ this.location = location;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ Image image = (Image) o;
+ return location.equals(image.location) && description.equals(image.description);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(location, description);
+ }
+}
\ No newline at end of file
diff --git a/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/model/Movie.java b/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/model/Movie.java
new file mode 100644
index 00000000..c3ee894a
--- /dev/null
+++ b/ojdbc-provider-samples/src/main/java/oracle/jdbc/provider/oson/sample/model/Movie.java
@@ -0,0 +1,85 @@
+/*
+ ** Copyright (c) 2024 Oracle and/or its affiliates.
+ **
+ ** The Universal Permissive License (UPL), Version 1.0
+ **
+ ** Subject to the condition set forth below, permission is hereby granted to any
+ ** person obtaining a copy of this software, associated documentation and/or data
+ ** (collectively the "Software"), free of charge and under any and all copyright
+ ** rights in the Software, and any and all patent rights owned or freely
+ ** licensable by each licensor hereunder covering either (i) the unmodified
+ ** Software as contributed to or provided by such licensor, or (ii) the Larger
+ ** Works (as defined below), to deal in both
+ **
+ ** (a) the Software, and
+ ** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
+ ** one is included with the Software (each a "Larger Work" to which the Software
+ ** is contributed by such licensors),
+ **
+ ** without restriction, including without limitation the rights to copy, create
+ ** derivative works of, display, perform, and distribute the Software and make,
+ ** use, sell, offer for sale, import, export, have made, and have sold the
+ ** Software and the Larger Work(s), and to sublicense the foregoing rights on
+ ** either these or other terms.
+ **
+ ** This license is subject to the following condition:
+ ** The above copyright notice and either this complete permission notice or at
+ ** a minimum a reference to the UPL must be included in all copies or
+ ** substantial portions of the Software.
+ **
+ ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ ** SOFTWARE.
+ */
+package oracle.jdbc.provider.oson.sample.model;
+
+import java.math.BigDecimal;
+import java.time.OffsetDateTime;
+import java.util.List;
+import java.util.Objects;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+public class Movie {
+ @JsonProperty("_id")
+ public int id;
+ public String title;
+ public String genre;
+ public BigDecimal gross;
+ public OffsetDateTime released;
+ public List