|
| 1 | +REM |
| 2 | +REM |
| 3 | +REM ------------------------------------------------------------------------------ |
| 4 | +REM Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
| 5 | +REM |
| 6 | +REM Portions Copyright 2006-2015, Kuassi Mensah. All rights reserved. |
| 7 | +REM https://www.amazon.com/dp/1555583296 |
| 8 | +REM Adapated from existing JDBC demo. |
| 9 | +REM |
| 10 | +REM ------------------------------------------------------------------------------ |
| 11 | +REM DESCRIPTION |
| 12 | +REM The following code sample runs directly in the database (using OJVM). |
| 13 | +REM It retrieves a worker from a database, then updates its position and salary. |
| 14 | +REM |
| 15 | +create or replace and resolve java source named Workers as |
| 16 | + |
| 17 | +import java.sql.*; |
| 18 | +import oracle.jdbc.driver.*; |
| 19 | + |
| 20 | +public class Workers |
| 21 | +{ |
| 22 | + |
| 23 | + public static void main (String args []) throws SQLException { |
| 24 | + |
| 25 | + String name = null; |
| 26 | + String pos = null; |
| 27 | + int sal; |
| 28 | + int id; |
| 29 | + long t0,t1; |
| 30 | + Connection conn = null; |
| 31 | + Statement stmt = null; |
| 32 | + PreparedStatement pstmt = null; |
| 33 | + |
| 34 | + if ( args.length < 1 ) { |
| 35 | + System.err.println("Usage: Java Workers <wid> <new position> <new salary>"); |
| 36 | + System.exit(1); |
| 37 | + } |
| 38 | + |
| 39 | + // Get parameters value |
| 40 | + id = Integer.parseInt(args[0]); |
| 41 | + pos = args[1]; |
| 42 | + sal = Integer.parseInt(args[2]); |
| 43 | + |
| 44 | + /* |
| 45 | + * Where is your code running: in the database or outside? |
| 46 | + */ |
| 47 | + if (System.getProperty("oracle.jserver.version") != null) |
| 48 | + { |
| 49 | + /* |
| 50 | + * You are in the database, already connected, use the default |
| 51 | + * connection |
| 52 | + */ |
| 53 | + conn = DriverManager.getConnection("jdbc:default:connection:"); |
| 54 | + System.out.println ("Running in OracleJVM, in the database!"); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + /* |
| 59 | + * You are not in the database, you need to connect to |
| 60 | + * the database |
| 61 | + */ |
| 62 | + |
| 63 | + DriverManager.registerDriver(new oracle.jdbc.OracleDriver()); |
| 64 | + conn = DriverManager.getConnection("jdbc:oracle:thin:", |
| 65 | + "scott", "tiger"); |
| 66 | + System.out.println ("Running in JDK VM, outside the database!"); |
| 67 | + } |
| 68 | + |
| 69 | + /* |
| 70 | + * Auto commit is off by default in OJVM |
| 71 | + * Set auto commit off in client JDBC |
| 72 | + */ |
| 73 | + conn.setAutoCommit (false); |
| 74 | + |
| 75 | + // Start timing |
| 76 | + t0=System.currentTimeMillis(); |
| 77 | + |
| 78 | + /* |
| 79 | + * find the name of the workers given his id number |
| 80 | + */ |
| 81 | + |
| 82 | + // create statement |
| 83 | + stmt = conn.createStatement(); |
| 84 | + |
| 85 | + // find the name of the worker |
| 86 | + ResultSet rset = stmt.executeQuery( |
| 87 | + "SELECT WNAME FROM workers WHERE wid = " + id); |
| 88 | + |
| 89 | + // retrieve and print the result (we are only expecting 1 row |
| 90 | + while (rset.next()) |
| 91 | + { |
| 92 | + name = rset.getString(1); |
| 93 | + } |
| 94 | + |
| 95 | + // return the name of the worker who has the given worker number |
| 96 | + System.out.println ("Worker Name: "+ name); |
| 97 | + |
| 98 | + /* |
| 99 | + * update the position and salary of the retrieved worker |
| 100 | + */ |
| 101 | + |
| 102 | + // prepare the update statement |
| 103 | + pstmt = conn.prepareStatement("UPDATE WORKERS SET WPOSITION = ?, " + |
| 104 | + " WSALARY = ? WHERE WNAME = ?"); |
| 105 | + |
| 106 | + // set up bind values and execute the update |
| 107 | + pstmt.setString(1, pos); |
| 108 | + pstmt.setInt(2, sal); |
| 109 | + pstmt.setString(3, name); |
| 110 | + pstmt.execute(); |
| 111 | + |
| 112 | + // double-check (retrieve) the updated position and salary |
| 113 | + rset = stmt.executeQuery( |
| 114 | + "SELECT WPOSITION, WSALARY FROM WORKERS WHERE WNAME = '" + |
| 115 | + name + "'"); |
| 116 | + while (rset.next()) |
| 117 | + { |
| 118 | + pos = rset.getString ("wposition"); |
| 119 | + sal = rset.getInt ("wsalary"); |
| 120 | + } |
| 121 | + System.out.println ("Worker: Id = " + id + ", Name = " + name + |
| 122 | + ", Position = " + pos + ", Salary = " + sal); |
| 123 | + |
| 124 | + // Close the ResultSet |
| 125 | + rset.close(); |
| 126 | + |
| 127 | + // Close the Statement |
| 128 | + stmt.close(); |
| 129 | + |
| 130 | + // Stop timing |
| 131 | + t1=System.currentTimeMillis(); |
| 132 | + System.out.println ("====> Duration: "+(int)(t1-t0)+ " Milliseconds"); |
| 133 | + |
| 134 | + // Close the connection |
| 135 | + conn.close(); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | +/ |
| 140 | +show errors; |
| 141 | + |
| 142 | +create or replace procedure WorkerSP (wid IN varchar2,wpos IN |
| 143 | +varchar2, wsal IN varchar2) as |
| 144 | +language java name 'Workers.main(java.lang.String[])'; |
| 145 | +/ |
| 146 | +show errors; |
| 147 | + |
| 148 | +set serveroutput on |
| 149 | +call dbms_java.set_output(50000); |
| 150 | +call WorkerSp('621', 'Senior VP', '650000'); |
0 commit comments