|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.spanner.jdbc; |
| 18 | + |
| 19 | +// [START spanner_jdbc_json_insert_data] |
| 20 | +import com.google.cloud.spanner.jdbc.JsonType; |
| 21 | +import java.sql.Connection; |
| 22 | +import java.sql.DriverManager; |
| 23 | +import java.sql.PreparedStatement; |
| 24 | +import java.sql.SQLException; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +class JsonInsertDataExample { |
| 29 | + // Class to contain Venue sample data. |
| 30 | + static class Venue { |
| 31 | + final long venueId; |
| 32 | + final String venueDetails; |
| 33 | + |
| 34 | + Venue(long venueId, String venueDetails) { |
| 35 | + this.venueId = venueId; |
| 36 | + this.venueDetails = venueDetails; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + static final List<Venue> VENUES = |
| 41 | + Arrays.asList( |
| 42 | + new Venue( |
| 43 | + 4, |
| 44 | + "[{\"name\":\"room 1\",\"open\":\"true\"}," |
| 45 | + + "{\"name\":\"room 2\",\"open\":\"false\"}]"), |
| 46 | + new Venue(19, "{\"rating\":\"9\",\"open\":\"true\"}"), |
| 47 | + new Venue( |
| 48 | + 42, |
| 49 | + "{\"name\":null," |
| 50 | + + "\"open\":{\"Monday\":\"true\",\"Tuesday\":\"false\"}," |
| 51 | + + "\"tags\":[\"large\",\"airy\"]}")); |
| 52 | + |
| 53 | + static void insertJsonData() throws SQLException { |
| 54 | + // TODO(developer): Replace these variables before running the sample. |
| 55 | + String projectId = "my-project"; |
| 56 | + String instanceId = "my-instance"; |
| 57 | + String databaseId = "my-database"; |
| 58 | + insertJsonData(projectId, instanceId, databaseId); |
| 59 | + } |
| 60 | + |
| 61 | + static void insertJsonData(String projectId, String instanceId, String databaseId) |
| 62 | + throws SQLException { |
| 63 | + String connectionUrl = |
| 64 | + String.format( |
| 65 | + "jdbc:cloudspanner:/projects/%s/instances/%s/databases/%s", |
| 66 | + projectId, instanceId, databaseId); |
| 67 | + try (Connection connection = DriverManager.getConnection(connectionUrl)) { |
| 68 | + try (PreparedStatement ps = |
| 69 | + connection.prepareStatement("INSERT INTO Venues\n" |
| 70 | + + "(VenueId, VenueDetails)\n" |
| 71 | + + "VALUES\n" |
| 72 | + + "(?, ?)")) { |
| 73 | + for (Venue venue : VENUES) { |
| 74 | + ps.setLong(1, venue.venueId); |
| 75 | + // Tell the JDBC driver that we want to set a JSON value and not a STRING value |
| 76 | + // by specifying the JsonType SQL type. |
| 77 | + ps.setObject(2, venue.venueDetails, JsonType.INSTANCE); |
| 78 | + ps.addBatch(); |
| 79 | + } |
| 80 | + int[] updateCounts = ps.executeBatch(); |
| 81 | + System.out.printf("Insert counts: %s%n", Arrays.toString(updateCounts)); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | +// [END spanner_jdbc_json_insert_data] |
0 commit comments