forked from oracle-samples/oracle-db-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPLSQLSample.java
More file actions
executable file
·548 lines (456 loc) · 19.9 KB
/
PLSQLSample.java
File metadata and controls
executable file
·548 lines (456 loc) · 19.9 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.*/
/*
* DESCRIPTION
*
* This sample demonstrates the usage of PL/SQL Stored Procedures and Functions in JDBC.
*
* Each unit of this sample demonstrates the following:
* 1. creating a PL/SQL Stored Procedure/Function,
* 2. invoking the Stored Procedure/Function with IN, OUT, IN OUT parameters,
* 3. and the correspondence of IN/OUT parameter with get/set/register methods.
*
* It is required that applications have Oracle JDBC driver jar (ojdbc8.jar) in
* the class-path, and that the database back end supports SQL (this sample uses
* an Oracle Database).
*
* To run the sample, you must provide non-default and working values for ALL 3
* of user, password, and URL. This can be done by either updating
* this file directly or supplying the 3 values as command-line options
* and user input. The password is read from console or standard input.
* java PLSQLSample -l <url> -u <user>
* If you do not update all the defaults, the program proceeds but
* will hit error when connecting.
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import oracle.jdbc.pool.OracleDataSource;
public class PLSQLSample {
private final static String DEFAULT_URL = "jdbc:oracle:thin:@//myhost:myport/myservice";
private final static String DEFAULT_USER = "myuser";
private final static String DEFAULT_PASSWORD = "mypassword";
//You must provide non-default values for ALL 3 to execute the program
private static String url = DEFAULT_URL;
private static String user = DEFAULT_USER;
private static String password = DEFAULT_PASSWORD;
private static final String TABLE_NAME = "PLSQL_JDBC_SAMPLE";
public static void main(String args[]) throws Exception {
Util.getRealUserPasswordUrl(args);
PLSQLSample sample = new PLSQLSample();
sample.run();
}
private void run() {
try (Connection conn = getConnection()) {
// Initialize the table
init(conn);
// Demonstrate how a no arg PLSQL procedure can be invoked.
demoPlsqlProcedureNoParams(conn);
// Demonstrate how a PLSQL procedure with IN parameters can be invoked.
demoPlsqlProcedureINParams(conn);
// Demonstrate how a PLSQL procedure with OUT parameters can be invoked.
demoPlsqlProcedureOUTParams(conn);
// Demonstrate how a PLSQL procedure with IN OUT parameters can be invoked.
demoPlsqlProcedureINOUTParams(conn);
// Demonstrate how a no arg PLSQL function can be invoked.
demoPlsqlFunctionNoParams(conn);
// Demonstrate how a PLSQL function with IN parameters can be invoked.
demoPlsqlFunctionINParams(conn);
// Demonstrate how a PLSQL function with OUT parameters can be invoked.
demoPlsqlFunctionOUTParams(conn);
// Demonstrate how a PLSQL function with IN OUT parameters can be invoked.
demoPlsqlFunctionINOUTParams(conn);
// Cleanup the database after the demo.
truncateTable(conn);
} catch (SQLException sqlEx) {
Util.showError("run", sqlEx);
}
}
private void init(Connection conn) throws SQLException {
// Truncate the table.
truncateTable(conn);
// Load the table with few rows.
loadTable(conn);
}
private void loadTable(Connection conn) throws SQLException {
String insertDml = "INSERT INTO "+TABLE_NAME+" (NUM, NAME, INSERTEDBY) VALUES (?,?,?)";
try (PreparedStatement prepStmt = conn.prepareStatement(insertDml)) {
prepStmt.setInt(1, 1);
prepStmt.setString(2, "ONE");
prepStmt.setString(3, "default");
prepStmt.addBatch();
prepStmt.setInt(1, 2);
prepStmt.setString(2, "TWO");
prepStmt.setString(3, "default");
prepStmt.addBatch();
prepStmt.setInt(1, 3);
prepStmt.setString(2, "THREE");
prepStmt.setString(3, "default");
prepStmt.addBatch();
prepStmt.setInt(1, 4);
prepStmt.setString(2, "FOUR");
prepStmt.setString(3, "default");
prepStmt.addBatch();
prepStmt.executeBatch();
}
// Display initial set of rows loaded into the table.
Util.show("Table '"+TABLE_NAME+"' is loaded with: ");
displayRows(conn, "default");
}
private void truncateTable(Connection conn) {
String sql = "TRUNCATE TABLE " + TABLE_NAME;
Util.show(sql);
Util.trySql(conn, sql);
}
private void demoPlsqlProcedureNoParams(Connection conn) throws SQLException {
// Create a PLSQL stored procedure that takes no arguments.
final String PROC_NAME = "ProcNoParams";
String sql = "CREATE OR REPLACE PROCEDURE "+PROC_NAME+" IS "
+ "BEGIN "
+ "INSERT INTO "+TABLE_NAME+" VALUES (5, 'FIVE', '"+PROC_NAME+"'); "
+ "INSERT INTO "+TABLE_NAME+" VALUES (6, 'SIX', '"+PROC_NAME+"'); "
+ "INSERT INTO "+TABLE_NAME+" VALUES (7, 'SEVEN', '"+PROC_NAME+"'); "
+ "INSERT INTO "+TABLE_NAME+" VALUES (8, 'EIGHT', '"+PROC_NAME+"'); "
+ "END; ";
Util.show(sql);
Util.doSql(conn, sql);
// Invoke the stored procedure.
sql = "CALL "+PROC_NAME+"()";
try (CallableStatement callStmt = conn.prepareCall(sql)) {
callStmt.execute();
// Display rows inserted by the above stored procedure call.
Util.show("Rows inserted by the stored procedure '"+PROC_NAME+"' are: ");
displayRows(conn, PROC_NAME);
} catch (SQLException sqlEx) {
Util.showError("demoPlsqlProcedureNoArgs", sqlEx);
} finally {
// Drop the procedure when done with it.
Util.doSql(conn, "DROP PROCEDURE "+PROC_NAME);
}
}
private void demoPlsqlProcedureINParams(Connection conn) throws SQLException {
// Create a PLSQL stored procedure with IN parameters.
final String PROC_NAME = "ProcINParams";
String sql = "CREATE OR REPLACE PROCEDURE "+PROC_NAME+"(num IN NUMBER, name IN VARCHAR2, insertedBy IN VARCHAR2) IS "
+ "BEGIN "
+ "INSERT INTO "+TABLE_NAME+" VALUES (num, name, insertedBy); "
+ "END; ";
Util.show(sql);
Util.doSql(conn, sql);
// Invoke the stored procedure.
sql = "CALL "+PROC_NAME+"(?,?,?)";
try (CallableStatement callStmt = conn.prepareCall(sql)) {
callStmt.setInt(1, 9);
callStmt.setString(2, "NINE");
callStmt.setString(3, PROC_NAME);
callStmt.addBatch();
callStmt.setInt(1, 10);
callStmt.setString(2, "TEN");
callStmt.setString(3, PROC_NAME);
callStmt.addBatch();
callStmt.setInt(1, 11);
callStmt.setString(2, "ELEVEN");
callStmt.setString(3, PROC_NAME);
callStmt.addBatch();
callStmt.setInt(1, 12);
callStmt.setString(2, "TWELVE");
callStmt.setString(3, PROC_NAME);
callStmt.addBatch();
callStmt.executeBatch();
// Display rows inserted by the above stored procedure call.
Util.show("Rows inserted by the stored procedure '"+PROC_NAME+"' are: ");
displayRows(conn, PROC_NAME);
} catch (SQLException sqlEx) {
Util.showError("demoPlsqlProcedureINParams", sqlEx);
} finally {
// Drop the procedure when done with it.
Util.doSql(conn, "DROP PROCEDURE "+PROC_NAME);
}
}
private void demoPlsqlProcedureOUTParams(Connection conn) throws SQLException {
// Create a PLSQL stored procedure with OUT parameters.
final String PROC_NAME = "ProcOUTParams";
String sql = "CREATE OR REPLACE PROCEDURE "+PROC_NAME+"(num IN NUMBER, name IN VARCHAR2, insertedBy IN VARCHAR2, numInserted OUT NUMBER) IS "
+ "BEGIN "
+ "INSERT INTO "+TABLE_NAME+" VALUES (num, name, insertedBy); "
+ "numInserted := num; "
+ "END; ";
Util.show(sql);
Util.doSql(conn, sql);
// Invoke the stored procedure.
sql = "CALL "+PROC_NAME+"(?,?,?,?)";
try (CallableStatement callStmt = conn.prepareCall(sql)) {
callStmt.setInt(1, 13);
callStmt.setString(2, "THIRTEEN");
callStmt.setString(3, PROC_NAME);
callStmt.registerOutParameter(4, Types.INTEGER);
callStmt.execute();
// Display rows inserted by the above stored procedure call.
Util.show("Rows inserted by the stored procedure '"+PROC_NAME+"' are: ");
displayRows(conn, PROC_NAME);
// Show the value of OUT parameter after the stored procedure call.
Util.show("The out parameter value of stored procedure '"+PROC_NAME+"' returned "+callStmt.getInt(4)+".");
} catch (SQLException sqlEx) {
Util.showError("demoPlsqlProcedureOUTParams", sqlEx);
} finally {
// Drop the procedure when done with it.
Util.doSql(conn, "DROP PROCEDURE "+PROC_NAME);
}
}
private void demoPlsqlProcedureINOUTParams(Connection conn) throws SQLException {
// Create a PLSQL stored procedure with IN OUT parameters.
final String PROC_NAME = "ProcINOUTParams";
String sql = "CREATE OR REPLACE PROCEDURE "+PROC_NAME+"(num IN OUT NUMBER, name IN OUT VARCHAR2, insertedBy IN VARCHAR2) IS "
+ "BEGIN "
+ "INSERT INTO "+TABLE_NAME+" VALUES (num, name, insertedBy); "
+ "num := 0; "
+ "name := 'ZERO'; "
+ "END; ";
Util.show(sql);
Util.doSql(conn, sql);
// Invoke the stored procedure.
sql = "CALL "+PROC_NAME+"(?,?,?)";
try (CallableStatement callStmt = conn.prepareCall(sql)) {
callStmt.setInt(1, 14);
callStmt.registerOutParameter(1, Types.INTEGER);
callStmt.setString(2, "FOURTEEN");
callStmt.registerOutParameter(2, Types.VARCHAR);
callStmt.setString(3, PROC_NAME);
callStmt.execute();
// Display rows inserted by the above stored procedure call.
Util.show("Rows inserted by the stored procedure '"+PROC_NAME+"' are: ");
displayRows(conn, PROC_NAME);
// Show the values of OUT parameters after the stored procedure call.
Util.show("Out parameter values of stored procedure '" + PROC_NAME + "': num = " + callStmt.getInt(1)
+ ", name = " + callStmt.getString(2) + ".");
} catch (SQLException sqlEx) {
Util.showError("demoPlsqlProcedureINOUTParams", sqlEx);
} finally {
// Drop the procedure when done with it.
Util.doSql(conn, "DROP PROCEDURE "+PROC_NAME);
}
}
private void demoPlsqlFunctionNoParams(Connection conn) throws SQLException {
// Create a PLSQL function that takes no arguments.
final String FUNC_NAME = "FuncNoParams";
String sql = "CREATE OR REPLACE FUNCTION "+FUNC_NAME+" RETURN NUMBER IS "
+ "BEGIN "
+ "INSERT INTO "+TABLE_NAME+" VALUES (15, 'FIFTEEN', '"+FUNC_NAME+"'); "
+ "INSERT INTO "+TABLE_NAME+" VALUES (16, 'SIXTEEN', '"+FUNC_NAME+"'); "
+ "INSERT INTO "+TABLE_NAME+" VALUES (17, 'SEVENTEEN', '"+FUNC_NAME+"'); "
+ "INSERT INTO "+TABLE_NAME+" VALUES (18, 'EIGHTEEN', '"+FUNC_NAME+"'); "
+ "RETURN 4;" // Return number of row inserted into the table.
+ "END; ";
Util.show(sql);
Util.doSql(conn, sql);
// Invoke the PLSQL function.
sql = "BEGIN ? := "+FUNC_NAME+"; end;";
try (CallableStatement callStmt = conn.prepareCall(sql)) {
callStmt.registerOutParameter (1, Types.INTEGER);
callStmt.execute();
// Display rows inserted by the above PLSQL function call.
Util.show("Rows inserted by the PLSQL function '"+FUNC_NAME+"' are: ");
displayRows(conn, FUNC_NAME);
// Show the value returned by the PLSQL function.
Util.show("The value returned by the PLSQL function '"+FUNC_NAME+"' is "+callStmt.getInt(1)+".");
} catch (SQLException sqlEx) {
Util.showError("demoPlsqlFunctionNoParams", sqlEx);
} finally {
// Drop the function when done with it.
Util.doSql(conn, "DROP FUNCTION "+FUNC_NAME);
}
}
private void demoPlsqlFunctionINParams(Connection conn) throws SQLException {
// Create a PLSQL function with IN parameters.
final String FUNC_NAME = "FuncINParams";
String sql = "CREATE OR REPLACE FUNCTION "+FUNC_NAME+"(num IN NUMBER, name IN VARCHAR2, insertedBy IN VARCHAR2) RETURN NUMBER IS "
+ "BEGIN "
+ "INSERT INTO "+TABLE_NAME+" VALUES (num, name, insertedBy); "
+ "RETURN 1;" // Return number of row inserted into the table.
+ "END; ";
Util.show(sql);
Util.doSql(conn, sql);
// Invoke the PLSQL function.
sql = "BEGIN ? := "+FUNC_NAME+"(?,?,?); end;";
try (CallableStatement callStmt = conn.prepareCall(sql)) {
callStmt.registerOutParameter (1, Types.INTEGER);
callStmt.setInt(2, 19);
callStmt.setString(3, "NINETEEN");
callStmt.setString(4, FUNC_NAME);
callStmt.execute();
// Display rows inserted by the above PLSQL function call.
Util.show("Rows inserted by the PLSQL function '"+FUNC_NAME+"' are: ");
displayRows(conn, FUNC_NAME);
// Show the value returned by the PLSQL function.
Util.show("The value returned by the PLSQL function '"+FUNC_NAME+"' is "+callStmt.getInt(1)+".");
} catch (SQLException sqlEx) {
Util.showError("demoPlsqlFunctionINParams", sqlEx);
} finally {
// Drop the function when done with it.
Util.doSql(conn, "DROP FUNCTION "+FUNC_NAME);
}
}
private void demoPlsqlFunctionOUTParams(Connection conn) throws SQLException {
// Create a PLSQL function with IN parameters.
final String FUNC_NAME = "FuncOUTParams";
String sql = "CREATE OR REPLACE FUNCTION "+FUNC_NAME+"(num IN NUMBER, name IN VARCHAR2, insertedBy IN VARCHAR2, numInserted OUT NUMBER) RETURN NUMBER IS "
+ "BEGIN "
+ "INSERT INTO "+TABLE_NAME+" VALUES (num, name, insertedBy); "
+ "numInserted := num; "
+ "RETURN 1;" // Return number of row inserted into the table.
+ "END; ";
Util.show(sql);
Util.doSql(conn, sql);
// Invoke the PLSQL function.
sql = "BEGIN ? := "+FUNC_NAME+"(?,?,?,?); end;";
try (CallableStatement callStmt = conn.prepareCall(sql)) {
callStmt.registerOutParameter (1, Types.INTEGER);
callStmt.setInt(2, 20);
callStmt.setString(3, "TWENTY");
callStmt.setString(4, FUNC_NAME);
callStmt.registerOutParameter(5, Types.INTEGER);
callStmt.execute();
// Display rows inserted by the above PLSQL function call.
Util.show("Rows inserted by the PLSQL function '"+FUNC_NAME+"' are: ");
displayRows(conn, FUNC_NAME);
// Show the value returned by the PLSQL function.
Util.show("The value returned by the PLSQL function '"+FUNC_NAME+"' is "+callStmt.getInt(1)+".");
// Show the values of OUT parameters after the PLSQL function call.
Util.show("Out parameter value of PLSQL function '" + FUNC_NAME + "': num = " + callStmt.getInt(5) + ".");
} catch (SQLException sqlEx) {
Util.showError("demoPlsqlFunctionOUTParams", sqlEx);
} finally {
// Drop the function when done with it.
Util.doSql(conn, "DROP FUNCTION "+FUNC_NAME);
}
}
private void demoPlsqlFunctionINOUTParams(Connection conn) throws SQLException {
// Create a PLSQL function with IN OUT parameters.
final String FUNC_NAME = "FuncINOUTParams";
String sql = "CREATE OR REPLACE FUNCTION "+FUNC_NAME+"(num IN OUT NUMBER, name IN OUT VARCHAR2, insertedBy IN VARCHAR2) RETURN NUMBER IS "
+ "BEGIN "
+ "INSERT INTO "+TABLE_NAME+" VALUES (num, name, insertedBy); "
+ "num := 0; "
+ "name := 'ZERO'; "
+ "RETURN 1;" // Return number of row inserted into the table.
+ "END; ";
Util.show(sql);
Util.doSql(conn, sql);
// Invoke the PLSQL function.
sql = "BEGIN ? := "+FUNC_NAME+"(?,?,?); end;";
try (CallableStatement callStmt = conn.prepareCall(sql)) {
callStmt.registerOutParameter (1, Types.INTEGER);
callStmt.registerOutParameter (2, Types.INTEGER);
callStmt.setInt(2, 20);
callStmt.registerOutParameter (3, Types.VARCHAR);
callStmt.setString(3, "TWENTY");
callStmt.setString(4, FUNC_NAME);
callStmt.execute();
// Display rows inserted by the above PLSQL function call.
Util.show("Rows inserted by the PLSQL function '"+FUNC_NAME+"' are: ");
displayRows(conn, FUNC_NAME);
// Show the value returned by the PLSQL function.
Util.show("The value returned by the PLSQL function '"+FUNC_NAME+"' is "+callStmt.getInt(1)+".");
// Show the values of OUT parameters after the PLSQL function call.
Util.show("Out parameter values of PLSQL function '" + FUNC_NAME + "': num = " + callStmt.getInt(2)
+ ", name = " + callStmt.getString(3) + ".");
} catch (SQLException sqlEx) {
Util.showError("demoPlsqlFunctionINOUTParams", sqlEx);
} finally {
// Drop the function when done with it.
Util.doSql(conn, "DROP FUNCTION "+FUNC_NAME);
}
}
private void displayRows(Connection conn, String insertedByBind) throws SQLException {
String sql = "SELECT * FROM "+TABLE_NAME+" WHERE insertedBy = ?";
try (PreparedStatement prepStmt = conn.prepareStatement(sql)) {
prepStmt.setString(1, insertedByBind);
ResultSet rs = prepStmt.executeQuery();
while (rs.next()) {
Util.show(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
}
}
}
// Get a connection using the driver data source.
private Connection getConnection() throws SQLException {
OracleDataSource ods = new OracleDataSource();
ods.setURL(url);
ods.setUser(user);
ods.setPassword(password);
// Creates a physical connection to the database.
return ods.getConnection();
}
// Utility methods.
private static class Util {
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 + ": ");
}
public static void show(String msg) {
System.out.println(msg);
}
public static void showError(String msg, Throwable exc) {
System.err.println(msg + " hit error: " + exc.getMessage());
}
// Get specified option value from command-line.
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) {
char[] pchars = System.console().readPassword("\n[%s]", prompt);
if (pchars != null) {
password = new String(pchars);
java.util.Arrays.fill(pchars, ' ');
}
} else {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
show(prompt);
password = r.readLine();
}
}
public static void trySql(Connection conn, String sql) {
try (Statement stmt = conn.createStatement()) {
stmt.execute(sql);
} catch (SQLException e) {
// Ignore the exception.
}
}
public static void doSql(Connection conn, String sql) throws SQLException {
try (Statement stmt = conn.createStatement()) {
stmt.execute(sql);
}
}
}
}