-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathplsqlJDBC.java
558 lines (432 loc) · 14.3 KB
/
plsqlJDBC.java
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
548
549
550
551
552
553
554
555
556
557
558
/*
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Universal Permissive License v 1.0 as shown
* at http://oss.oracle.com/licenses/upl
*/
//-----------------------------------------------------------
//
// plsqlJDBC.java
//
// Call PLSQL procedure and function from JDBC
//
//----------------------------------------------------------
import java.sql.*;
import java.io.*;
import java.text.*;
import java.lang.*;
import java.util.*;
import com.timesten.jdbc.*;
class plsqlJDBC
{
// class constants
static int JDBCPLSQL_ORACLE = 0;
static int JDBCPLSQL_TIMESTEN = 1;
//---------------------------------------------------
// class variables
// All set to a default value, the default values
// can be overwritten using command line options
//---------------------------------------------------
// Used to turn the Jdbc tracing ON
static public boolean trace = false;
// JDBC URL string
static public String url = null;
// JDBC defaultDSN string
static public String defaultDSN = "sampledb";
// JDBC user string
static public String username = null;
// JDBC password string
static public String password = null;
// DBMS type
static public int dbms = JDBCPLSQL_TIMESTEN;
// Callable PL/SQL statement for plsql procedure callProc
static public CallableStatement callProc = null;
// Callable PL/SQL statement for plsql function callFunc
static public CallableStatement callFunc = null;
// Callable PL/SQL statement for plsql anonymous block
static public CallableStatement anonBlock = null;
// Callable PL/SQL statement for plsql procedure openRefCursor
static public CallableStatement openRefCursor = null;
// connection for the thread
static public Connection connection;
//--------------------------------------------------
// class constants
//--------------------------------------------------
public static final String exec_plsql_func =
"begin " +
":luckyEmp := sample_pkg.getEmpName(:empNo, :errCode, :errText); " +
"end;";
public static final String exec_plsql_proc =
"begin " +
"emp_pkg.givePayRaise(:numEmps, :luckyEmp, :errCode, :errText); " +
"end;";
public static final String exec_OpenSalesPeopleCursor =
"begin " +
"emp_pkg.OpenSalesPeopleCursor(:cmdRefCursor, :errCode, :errText); " +
"end;";
public static final String exec_anon_block =
"begin " +
"SELECT job, hiredate, sal, deptno " +
"INTO :jobtype, :hired, :salary, :dept " +
"FROM emp " +
"WHERE ename = UPPER(:luckyEmp); " +
"SELECT count(*) " +
"INTO :worked_longer " +
"FROM emp " +
"WHERE hiredate < :hired; " +
"SELECT count(*) " +
"INTO :higher_sal " +
"FROM emp " +
"WHERE sal > :salary; " +
"SELECT count(*) " +
"INTO :total_in_dept " +
"FROM emp " +
"WHERE deptno = :dept; " +
":errCode := 0; " + ":errText := 'OK'; " +
" EXCEPTION " +
" WHEN OTHERS THEN " +
" :errCode := SQLCODE; " +
" :errText := SUBSTR(SQLERRM, 1, 200); " +
"end;";
//--------------------------------------------------
// Function: main
//--------------------------------------------------
public static void main(String arg[])
{
Connection conn = null;
// parse arguments
parseArgs(arg);
// Turn the JDBC tracing on
if (plsqlJDBC.trace)
DriverManager.setLogWriter(new PrintWriter(System.out, true));
// Load the JDBC driver
try
{
if (dbms == JDBCPLSQL_ORACLE)
{
Class.forName("oracle.jdbc.OracleDriver");
}
else
{
Class.forName("com.timesten.jdbc.TimesTenDriver");
Class.forName("com.timesten.jdbc.TimesTenClientDriver");
}
}
catch (java.lang.ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
System.out.println();
System.out.println("Connecting to the database");
//Prompt for the Username and Password
AccessControl ac = new AccessControl();
username = ac.getUsername();
password = ac.getPassword();
System.out.println("Connected");
// Connect and prepare the PLSQL block
initialize();
// Execute the PLSQL
execute();
// De-allocate resources
cleanup();
}
//--------------------------------------------------
// usage()
//--------------------------------------------------
static private void usage()
{
System.err.print(
"\n" +
"Usage: plsqlJDBC [<-url url_string>] " +
"[-dbms <dbms_name>]" +
"[-h] [-help] [?]\n\n" +
" -h Prints this message and exits.\n" +
" -help Same as -h.\n" +
" -url <url_string> Specifies JDBC url string of the database\n" +
" to connect to\n" +
" -dbms <dbms_name> Use timesten/oracle. Timesten is default\n");
System.exit(1);
}
//--------------------------------------------------
//
// Function: getConsoleString
//
// Description: Read a String from the console and return it
//
//--------------------------------------------------
static private String getConsoleString(String what)
{
String temp = null;
try
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
temp = br.readLine();
}
catch (IOException e)
{
System.out.println();
System.out.println("Problem reading the " + what);
System.out.println();
}
return temp;
}
//--------------------------------------------------
//
// Function: parseArgs
//
// Parses command line arguments
//
//--------------------------------------------------
private static void parseArgs(String[] args)
{
int i = 0;
// System.out.println("args.length: " + args.length);
while (i < args.length)
{
// Command line help
if (args[i].equalsIgnoreCase("-h") || args[i].equalsIgnoreCase("-help"))
{
usage();
}
// JDBC url string
else if (args[i].equalsIgnoreCase("-url"))
{
if (i > args.length)
{
usage();
}
url = args[i + 1];
i += 2;
}
else if (args[i].equalsIgnoreCase("-dbms"))
{
if (i > args.length)
{
usage();
}
if (args[i + 1].equalsIgnoreCase("oracle"))
dbms = JDBCPLSQL_ORACLE;
i += 2;
}
// JDBC tracing
else if (args[i].equalsIgnoreCase("-trace"))
{
if (i > args.length)
{
usage();
}
trace = true;
i += 1;
}
else
{
usage();
}
}
if (url == null)
{
// Default the URL
url = "jdbc:timesten:" + defaultDSN;
}
}
public static void printSQLException(SQLException ex)
{
for (; ex != null; ex = ex.getNextException())
{
System.out.println(ex.getMessage());
System.out.println("Vendor Error Code: " + ex.getErrorCode());
System.out.println("SQL State: " + ex.getSQLState());
ex.printStackTrace();
}
}
//--------------------------------------------------
// Method : execute
// Execute the PLSQL routine
//--------------------------------------------------
public static void execute()
{
String luckyEmp = null;
String hired = null;
String job = null;
int empNo = 0;
int dept = 0;
double salary = 0.0;
int numEmps = 0;
int worked_longer = 0;
int higher_sal = 0;
int total_in_dept = 0;
int errCode = 0;
String errText = null;
ResultSet cursor = null;
try
{
// Setup the IN and OUT parameters for the procedure emp_pkg.givePayRaise
numEmps = 10;
errCode = 0;
errText = "OK";
callProc.setInt(1, numEmps); // Bind input param
callProc.registerOutParameter(2, Types.VARCHAR); // the return value
callProc.registerOutParameter(3, Types.INTEGER); // errCode
callProc.registerOutParameter(4, Types.VARCHAR); // errText
if (plsqlJDBC.trace)
{
System.out.println("excuting PL/SQL procedure emp_pkg.givePayRaise");
}
callProc.executeUpdate();
luckyEmp = callProc.getString(2);
System.out.println();
System.out.println("The employee who got the 10% pay raise was " + luckyEmp);
System.out.println();
// Setup the IN and OUT parameters for the anonymous PLSQL block
anonBlock.registerOutParameter(1, Types.VARCHAR); // the jobtype
anonBlock.registerOutParameter(2, Types.VARCHAR); // the hire date
anonBlock.registerOutParameter(3, Types.DOUBLE); // the salary
anonBlock.registerOutParameter(4, Types.INTEGER); // the deptartment
anonBlock.setString(5, luckyEmp); // Input empName
anonBlock.registerOutParameter(6, Types.INTEGER); // worked longer
anonBlock.registerOutParameter(7, Types.INTEGER); // Higer salary
anonBlock.registerOutParameter(8, Types.INTEGER); // Number in department
anonBlock.registerOutParameter(9, Types.INTEGER); // errCode
anonBlock.registerOutParameter(10, Types.VARCHAR); // errText
errCode = 0;
errText = "OK";
if (plsqlJDBC.trace)
{
System.out.println("executing PL/SQL anonymous block");
}
anonBlock.executeUpdate();
errCode = anonBlock.getInt(9);
if (0 == errCode)
{
job = anonBlock.getString(1);
hired = anonBlock.getString(2);
salary = anonBlock.getDouble(3);
dept = anonBlock.getInt(4);
worked_longer = anonBlock.getInt(6);
higher_sal = anonBlock.getInt(7);
total_in_dept = anonBlock.getInt(8);
System.out.printf("Name: %s\n", luckyEmp);
System.out.printf("Job: %s\n", job);
System.out.printf("Hired: %s\t(%2d people have served longer).\n",
hired, worked_longer);
System.out.printf("Salary: $%.2f\t\t\t(%2d people have a higher salary).\n",
salary, higher_sal);
System.out.printf("Department: %d\t\t\t\t(%2d people in the department).\n\n",
dept, total_in_dept);
}
else
{
System.out.println("Problem with anonymous block");
System.out.println("Error code : " + errCode);
System.out.println("Error text : " + anonBlock.getString(10));
System.out.println();
}
// Setup the IN and OUT parameters for the function sample_pkg.getEmpName
empNo = 7839;
callFunc.registerOutParameter(1, Types.VARCHAR); // the return value
callFunc.setInt(2, empNo); // Bind 2nd parameter
callFunc.registerOutParameter(3, Types.INTEGER); // errCode
callFunc.registerOutParameter(4, Types.VARCHAR); // errText
if (plsqlJDBC.trace)
{
System.out.println("excuting PL/SQL function sample_pkg.getEmpName");
}
callFunc.executeUpdate();
System.out.println("Employee name for empid " + empNo + " is : " + callFunc.getString(1));
System.out.println();
// Setup the OUT parameters for the procedure emp_pkg.OpenSalesPeopleCursor
openRefCursor.registerOutParameter(1, TimesTenTypes.CURSOR); // the return value
openRefCursor.registerOutParameter(2, Types.INTEGER); // errCode
openRefCursor.registerOutParameter(3, Types.VARCHAR); // errText
if (plsqlJDBC.trace)
{
System.out.println("excuting PL/SQL procedure emp_pkg.OpenSalesPeopleCursor");
}
openRefCursor.executeUpdate();
cursor = ((TimesTenCallableStatement)openRefCursor).getCursor(1);
if (null != cursor)
{
/* Iterate over the ref cursors result set */
System.out.println("The sales people are:");
System.out.println();
System.out.println(" EMPNO ENAME SALARY");
System.out.println(" ===== ========== ==========");
while (cursor.next())
{
empNo = cursor.getInt(1);
luckyEmp = cursor.getString(2);
salary = cursor.getDouble(3);
System.out.printf(" %5d %-10s %10.2f\n", empNo, luckyEmp, salary);
}
cursor.close();
openRefCursor.close();
System.out.println();
}
}
catch (SQLException e)
{
printSQLException(e);
}
}
//--------------------------------------------------
// Method: initialize
// Gets the connection for the thread and prepares
// all the statements for the thread so it is
// ready to execute
//--------------------------------------------------
static private void initialize()
{
try
{
// Connect to the data store
if (plsqlJDBC.trace)
System.out.println("Connecting");
if (plsqlJDBC.dbms == plsqlJDBC.JDBCPLSQL_ORACLE)
{
connection = DriverManager.getConnection(plsqlJDBC.url, plsqlJDBC.username, plsqlJDBC.password);
}
else
{
connection = DriverManager.getConnection(plsqlJDBC.url, plsqlJDBC.username, plsqlJDBC.password);
}
// set autocommit off
connection.setAutoCommit(false);
connection.commit();
if (plsqlJDBC.trace)
System.out.println("Preparing PLSQL calls");
callFunc = connection.prepareCall(plsqlJDBC.exec_plsql_func);
callProc = connection.prepareCall(plsqlJDBC.exec_plsql_proc);
openRefCursor = connection.prepareCall(plsqlJDBC.exec_OpenSalesPeopleCursor);
anonBlock = connection.prepareCall(plsqlJDBC.exec_anon_block);
connection.commit();
}
catch (SQLException e)
{
printSQLException(e);
}
}
//--------------------------------------------------
// method: cleanup
// closes all the prepared statements and the connection
//--------------------------------------------------
static public void cleanup()
{
try
{
// commit outstanding activity to avoid function sequence error
connection.commit();
if (plsqlJDBC.trace)
System.out.println("Closing statements");
callFunc.close();
System.out.println("Disconnecting");
connection.close();
System.out.println("Disconnected");
System.out.println();
}
catch (SQLException e)
{
plsqlJDBC.printSQLException(e);
}
}
} /* plsqlJDBC */