forked from oracle-samples/oracle-db-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVARRAYSample.java
More file actions
294 lines (241 loc) · 8.24 KB
/
VARRAYSample.java
File metadata and controls
294 lines (241 loc) · 8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.*/
/*
DESCRIPTION
This code sample demonstrates usage of VARRAY data structure in PL/SQL.
This capability was added in the JDBC thin driver in 18c.
To run the sample, you must enter the DB user's password from the
console, and optionally specify the DB user and/or connect URL on
the command-line. You can also modify these values in this file
and recompile the code.
java VARRAYSample -l <url> -u <user>
*/
/*import java.sql.*;
import oracle.sql.*;
import oracle.jdbc.*; */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.sql.Array;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.OracleArray;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.pool.OracleDataSource;
public class VARRAYSample {
final static String DEFAULT_URL = "jdbc:oracle:thin:@//myhost:myport/myservice";
final static String DEFAULT_USER = "myuser";
final static String DEFAULT_PASSWORD = "mypassword";
// You must provide non-default values for all 3 parameters to execute the program
static String url = DEFAULT_URL;
static String user = DEFAULT_USER;
static String password = DEFAULT_PASSWORD;
// Connection object for VARRAY sample.
// The sample uses only one connection for all VARRAY
// operations in this demo program.
private Connection conn;
/**
* Entry point of the sample.
*
* @param args
* Command line arguments. Supported command line options: -l <url>
* -u <user>
* @throws Exception
*/
public static void main(String args[]) throws Exception {
VARRAYSample vArraySample = new VARRAYSample();
getRealUserPasswordUrl(args);
// Get a connection and initialize the schema.
vArraySample.setup();
// Shows VARRAY usage
vArraySample.demoVARRAY();
// Drop table and disconnect from the database.
vArraySample.cleanup();
}
/**
* Creates Connection to database and drops the demo table if exists
* @throws SQLException
*/
void setup() throws SQLException {
conn = getConnection();
dropTableAndType();
}
/**
* Drop the table, type and close the connection
* @throws SQLException
*/
void cleanup() throws SQLException {
if (conn != null) {
dropTableAndType();
conn.close();
conn = null;
}
}
/**
* Shows how to use VArray PLSQL collection
* @throws SQLException
*/
void demoVARRAY() throws SQLException {
// Utility method to create table and type required for demo
createTableAndType();
demoVARRAYWihtoutBind();
demoVARRAYWithBind();
}
/**
* Usage of VARRAY with constant object
* @throws SQLException
*/
private void demoVARRAYWihtoutBind() throws SQLException {
showln("======== demoVARRAYWihtoutBind ========");
try (Statement stmt = conn.createStatement()) {
// Insert multiple values of type num_array into single row
stmt.execute("INSERT INTO varray_table VALUES (num_varray(100, 200))");
ResultSet rs = stmt.executeQuery("SELECT col1 FROM varray_table");
showArrayResultSet(rs);
}
}
/**
* Usage of VARRAY with parametric values
* @throws SQLException
*/
private void demoVARRAYWithBind() throws SQLException {
showln("======== demoVARRAYWithBind =======");
try (Statement stmt = conn.createStatement()) {
// Insert a new row with 4 values
int elements[] = { 300, 400, 500, 600 };
// Create a new Array with the above elements with given type
OracleArray newArray = (OracleArray) ((OracleConnection) conn).createOracleArray("NUM_VARRAY", elements);
// Create prepared statement to insert values
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO varray_table VALUES (?)")) {
// Bind the array values to statement
ps.setArray(1, newArray);
ps.execute();
ResultSet rs = stmt.executeQuery("SELECT col1 FROM varray_table");
showArrayResultSet(rs);
}
}
}
// ==============================Utility Methods==============================
/**
* Creates table with VARRAY type object
* @throws SQLException
*/
private void createTableAndType() throws SQLException {
try (Statement stmt = conn.createStatement()) {
// create num_varray with array of elements of type NUMBER
String sql = "CREATE TYPE num_varray AS VARRAY(10) OF NUMBER(12, 2)";
stmt.execute(sql);
// Create table with num_varray with column as VARRAY type
sql = "CREATE TABLE varray_table (col1 num_varray)";
stmt.execute(sql);
}
}
/**
* Drop the table and type
* @throws SQLException
*/
private void dropTableAndType() throws SQLException {
try (Statement stmt = conn.createStatement()) {
// Drop the table and type if exists
stmt.execute("DROP TABLE varray_table");
stmt.execute("DROP TYPE num_varray");
} catch (SQLException e) {
// the above drop statements will throw exceptions
// if the types and tables did not exist before. Just ignore it.
if (e.getErrorCode() != 942)
throw new RuntimeException(e);
else
showln("INFO: " + e.getMessage());
}
}
/**
* Gets the connection object
* @throws SQLException
*/
private Connection getConnection() throws SQLException {
// Create an OracleDataSource instance and set properties
OracleDataSource ods = new OracleDataSource();
ods.setUser(user);
ods.setPassword(password);
ods.setURL(url);
return ods.getConnection();
}
static void getRealUserPasswordUrl(String args[]) throws Exception {
// URL can be modified in file, or taken from command-line
url = getOptionValue(args, "-l", DEFAULT_URL);
// DB user can be modified in file, or taken from command-line
user = getOptionValue(args, "-u", DEFAULT_USER);
// DB user's password can be modified in file, or explicitly entered
readPassword(" Password for " + user + ": ");
}
// Get specified option value from command-line, or use default value
static String getOptionValue(String args[], String optionName, String defaultVal) {
String argValue = "";
try {
int i = 0;
String arg = "";
boolean found = false;
while (i < args.length) {
arg = args[i++];
if (arg.equals(optionName)) {
if (i < args.length)
argValue = args[i++];
if (argValue.startsWith("-") || argValue.equals("")) {
argValue = defaultVal;
}
found = true;
}
}
if (!found) {
argValue = defaultVal;
}
} catch (Exception e) {
showError("getOptionValue", e);
}
return argValue;
}
static void readPassword(String prompt) throws Exception {
if (System.console() == null) {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
showln(prompt);
password = r.readLine();
} else {
char[] pchars = System.console().readPassword("\n[%s]", prompt);
if (pchars != null) {
password = new String(pchars);
java.util.Arrays.fill(pchars, ' ');
}
}
}
// Show message line without new line
private static void showln(String msg) {
System.out.println(msg);
}
static void showError(String msg, Throwable exc) {
System.out.println(msg + " hit error: " + exc.getMessage());
}
/**
* Display array elements to console.
* @param rs
* @throws SQLException
*/
public static void showArrayResultSet(ResultSet rs) throws SQLException {
int line = 0;
while (rs.next()) {
line++;
showln("Row " + line + " : ");
Array array = rs.getArray(1);
showln("Array is of type " + ((OracleArray) array).getSQLTypeName());
showln("Array element is of type code " + array.getBaseType());
showln("Array is of length " + ((OracleArray) array).length());
// get Array elements
BigDecimal[] values = (BigDecimal[]) array.getArray();
for (int i = 0; i < values.length; i++) {
BigDecimal value = values[i];
showln(">>Array index " + i + " = " + value.intValue());
}
}
}
}