|
16 | 16 | */ |
17 | 17 | package org.apache.ignite.snippets; |
18 | 18 |
|
| 19 | +import java.io.ByteArrayInputStream; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.sql.Blob; |
| 23 | +import java.sql.Clob; |
19 | 24 | import java.sql.Connection; |
20 | 25 | import java.sql.DriverManager; |
21 | 26 | import java.sql.PreparedStatement; |
22 | 27 | import java.sql.ResultSet; |
23 | 28 | import java.sql.SQLException; |
24 | 29 |
|
| 30 | +import org.apache.ignite.Ignite; |
25 | 31 | import org.apache.ignite.IgniteJdbcThinDataSource; |
| 32 | +import org.apache.ignite.Ignition; |
26 | 33 |
|
27 | 34 | public class JDBCThinDriver { |
28 | 35 |
|
@@ -132,6 +139,61 @@ void partitionAwareness() throws ClassNotFoundException, SQLException { |
132 | 139 | conn.close(); |
133 | 140 | } |
134 | 141 |
|
| 142 | + void blobAndClob() throws ClassNotFoundException, SQLException { |
| 143 | + |
| 144 | + Connection conn = getConnection(); |
| 145 | + // tag::blob-clob[] |
| 146 | + // CLOB values are stored in VARCHAR columns, and BLOB values are stored in BINARY columns. |
| 147 | + conn.createStatement().executeUpdate( |
| 148 | + "CREATE TABLE IF NOT EXISTS DocumentStore (id INT PRIMARY KEY, payload BINARY, body VARCHAR)"); |
| 149 | + |
| 150 | + byte[] payload = "binary payload".getBytes(StandardCharsets.UTF_8); |
| 151 | + |
| 152 | + PreparedStatement insert = conn.prepareStatement( |
| 153 | + "INSERT INTO DocumentStore(id, payload, body) VALUES (?, ?, ?)"); |
| 154 | + |
| 155 | + Blob blob = conn.createBlob(); |
| 156 | + blob.setBytes(1, payload); |
| 157 | + |
| 158 | + Clob clob = conn.createClob(); |
| 159 | + clob.setString(1, "large text body"); |
| 160 | + |
| 161 | + insert.setInt(1, 1); |
| 162 | + insert.setBlob(2, blob); |
| 163 | + insert.setClob(3, clob); |
| 164 | + insert.executeUpdate(); |
| 165 | + |
| 166 | + PreparedStatement streamInsert = conn.prepareStatement( |
| 167 | + "INSERT INTO DocumentStore(id, payload, body) VALUES (?, ?, ?)"); |
| 168 | + |
| 169 | + streamInsert.setInt(1, 2); |
| 170 | + streamInsert.setBlob(2, new ByteArrayInputStream(payload), payload.length); |
| 171 | + streamInsert.setString(3, "large text body"); |
| 172 | + streamInsert.executeUpdate(); |
| 173 | + |
| 174 | + PreparedStatement select = conn.prepareStatement("SELECT payload, body FROM DocumentStore WHERE id = ?"); |
| 175 | + |
| 176 | + select.setInt(1, 1); |
| 177 | + |
| 178 | + ResultSet rs = select.executeQuery(); |
| 179 | + |
| 180 | + if (rs.next()) { |
| 181 | + Blob selectedBlob = rs.getBlob("payload"); |
| 182 | + byte[] selectedPayload = selectedBlob.getBytes(1, (int)selectedBlob.length()); |
| 183 | + |
| 184 | + Clob selectedClob = rs.getClob("body"); |
| 185 | + String selectedBody = selectedClob.getSubString(1, (int)selectedClob.length()); |
| 186 | + |
| 187 | + InputStream selectedPayloadStream = rs.getBinaryStream("payload"); |
| 188 | + |
| 189 | + assert selectedPayload.length == payload.length; |
| 190 | + assert selectedBody.equals("large text body"); |
| 191 | + assert selectedPayloadStream != null; |
| 192 | + } |
| 193 | + // end::blob-clob[] |
| 194 | + conn.close(); |
| 195 | + } |
| 196 | + |
135 | 197 | void handleException() throws ClassNotFoundException, SQLException { |
136 | 198 |
|
137 | 199 | Connection conn = getConnection(); |
@@ -217,21 +279,16 @@ void errorCodes() throws ClassNotFoundException, SQLException { |
217 | 279 | } |
218 | 280 |
|
219 | 281 | public static void main(String[] args) throws Exception { |
220 | | - // Ignite ignite = Util.startNode(); |
| 282 | + Ignite ignite = Ignition.start(); |
221 | 283 | try { |
222 | 284 | JDBCThinDriver j = new JDBCThinDriver(); |
223 | 285 |
|
224 | | - // j.getConnection(); |
225 | | - // j.multipleEndpoints(); |
226 | | - // j.connectionFromDatasource(); |
227 | | - // j.partitionAwareness(); |
228 | | - |
229 | | - j.ssl(); |
| 286 | + j.blobAndClob(); |
230 | 287 | } catch (Exception e) { |
231 | 288 | e.printStackTrace(); |
232 | 289 | System.exit(1); |
233 | 290 | } finally { |
234 | | - // ignite.close(); |
| 291 | + ignite.close(); |
235 | 292 | } |
236 | 293 | } |
237 | 294 | } |
0 commit comments