|
| 1 | +import java.sql.DriverManager; |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.sql.Connection; |
| 5 | +import java.sql.PreparedStatement; |
| 6 | +import java.sql.ResultSet; |
| 7 | +import java.sql.SQLException; |
| 8 | +import java.sql.Statement; |
| 9 | + |
| 10 | +import oracle.jdbc.pool.OracleDataSource; |
| 11 | + |
| 12 | + |
| 13 | +/* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.*/ |
| 14 | + |
| 15 | +/* |
| 16 | + DESCRIPTION |
| 17 | + A very basic Java stored procedure sample. For more complex Java stored procedure samples, |
| 18 | + please explore https://github.com/oracle/oracle-db-examples/tree/master/java/ojvm directory. |
| 19 | + Java stored procedure in the database executed using the KPRB JDBC driver in the Oracle JVM instance. |
| 20 | + To run the sample: |
| 21 | + 1. loadjava -r -v -user jdbcuser/jdbcuser123 JavaStoredProcSample.java |
| 22 | + This loads a java stored procedure in the database. |
| 23 | + 2. sqlplus jdbcuser/jdbcuser123 @JavaStoredProcSample.sql |
| 24 | + This script first creates a wrapper stored procedure for the java function. |
| 25 | + This calls java stored procedure from sqlplus and print number of emplyoees in the department number 20. |
| 26 | + */ |
| 27 | +public class JavaStoredProcSample { |
| 28 | + |
| 29 | + // This stored procedure executes on same client connection/session in the database. |
| 30 | + // So special JDBC URL use with default connection. |
| 31 | + final static String DEFAULT_URL_IN_DB = "jdbc:default:connection:"; |
| 32 | + |
| 33 | + // Get the total number of employees for a given department. |
| 34 | + // This method calls from the java stored procedure wrapper |
| 35 | + // define in the JavaStoredProcSample.sql script. |
| 36 | + public static int getEmpCountByDept(int deptNo) { |
| 37 | + int count = 0; |
| 38 | + |
| 39 | + try { |
| 40 | + // Get default connection on the current session from the client |
| 41 | + Connection conn = DriverManager.getConnection(DEFAULT_URL_IN_DB); |
| 42 | + |
| 43 | + count = getEmpCountByDept(conn, deptNo); |
| 44 | + } |
| 45 | + catch(SQLException sqe) { |
| 46 | + showError("getEmpCountByDept", sqe); |
| 47 | + } |
| 48 | + |
| 49 | + // Returns the calculated result value |
| 50 | + return count; |
| 51 | + } |
| 52 | + |
| 53 | + // Get the total number of employees for a given department. |
| 54 | + // This is a common method call from in database or out of database |
| 55 | + // running of this sample. |
| 56 | + static int getEmpCountByDept(Connection conn, int deptNo) { |
| 57 | + int count = 0; |
| 58 | + |
| 59 | + // Execute a SQL query |
| 60 | + String sql = "SELECT COUNT(1) FROM EMP WHERE DEPTNO = ?"; |
| 61 | + |
| 62 | + // Gets the result value |
| 63 | + try(PreparedStatement pstmt = conn.prepareStatement(sql)) { |
| 64 | + pstmt.setInt(1, deptNo); |
| 65 | + try (ResultSet rs = pstmt.executeQuery()) { |
| 66 | + if (rs.next()) { |
| 67 | + count = rs.getInt(1); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + catch(SQLException sqe) { |
| 72 | + showError("getEmpCountByDept", sqe); |
| 73 | + } |
| 74 | + |
| 75 | + // Returns the calculated result value |
| 76 | + return count; |
| 77 | + } |
| 78 | + |
| 79 | + // Display error message |
| 80 | + static void showError(String msg, Throwable exc) { |
| 81 | + System.out.println(msg + " hit error: " + exc.getMessage()); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + //================ All of the following code only for running this sample out of the database ======================================== |
| 86 | + |
| 87 | + final static String DEFAULT_URL = "jdbc:oracle:thin:@//myhost:myport/myservice"; |
| 88 | + final static String DEFAULT_USER = "myuser"; |
| 89 | + final static String DEFAULT_PASSWORD = "mypassword"; |
| 90 | + |
| 91 | + // You must provide non-default values for ALL 3 to execute the program |
| 92 | + static String url = DEFAULT_URL; |
| 93 | + static String user = DEFAULT_USER; |
| 94 | + static String password = DEFAULT_PASSWORD; |
| 95 | + |
| 96 | + private Connection conn; |
| 97 | + |
| 98 | + |
| 99 | + /** |
| 100 | + * Entry point of the sample for running it out of the database. |
| 101 | + * |
| 102 | + * @param args |
| 103 | + * Command line arguments. Supported command line options: -l <url> |
| 104 | + * -u <user> |
| 105 | + * @throws Exception |
| 106 | + */ |
| 107 | + public static void main(String args[]) throws Exception { |
| 108 | + JavaStoredProcSample javaProc = new JavaStoredProcSample(); |
| 109 | + |
| 110 | + getRealUserPasswordUrl(args); |
| 111 | + |
| 112 | + // Get connection and initialize schema. |
| 113 | + javaProc.setup(); |
| 114 | + |
| 115 | + // Call java stored proc out of database run |
| 116 | + int deptNo = 20; |
| 117 | + |
| 118 | + int count = javaProc.getEmpCountByDept(javaProc.conn, deptNo); |
| 119 | + show("" + count); |
| 120 | + |
| 121 | + // Disconnect from the database. |
| 122 | + javaProc.cleanup(); |
| 123 | + } |
| 124 | + |
| 125 | + // Gets connection to the database |
| 126 | + void setup() throws SQLException { |
| 127 | + conn = getConnection(); |
| 128 | + } |
| 129 | + |
| 130 | + // Disconnect from the database |
| 131 | + void cleanup() throws SQLException { |
| 132 | + if (conn != null) { |
| 133 | + conn.close(); |
| 134 | + conn = null; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + // ==============================Utility Methods============================== |
| 140 | + |
| 141 | + private Connection getConnection() throws SQLException { |
| 142 | + // Create an OracleDataSource instance and set properties |
| 143 | + OracleDataSource ods = new OracleDataSource(); |
| 144 | + ods.setUser(user); |
| 145 | + ods.setPassword(password); |
| 146 | + ods.setURL(url); |
| 147 | + |
| 148 | + return ods.getConnection(); |
| 149 | + } |
| 150 | + |
| 151 | + static void getRealUserPasswordUrl(String args[]) throws Exception { |
| 152 | + // URL can be modified in file, or taken from command-line |
| 153 | + url = getOptionValue(args, "-l", DEFAULT_URL); |
| 154 | + |
| 155 | + // DB user can be modified in file, or taken from command-line |
| 156 | + user = getOptionValue(args, "-u", DEFAULT_USER); |
| 157 | + |
| 158 | + // DB user's password can be modified in file, or explicitly entered |
| 159 | + readPassword(" Password for " + user + ": "); |
| 160 | + } |
| 161 | + |
| 162 | + // Get specified option value from command-line, or use default value |
| 163 | + static String getOptionValue(String args[], String optionName, |
| 164 | + String defaultVal) { |
| 165 | + String argValue = ""; |
| 166 | + |
| 167 | + try { |
| 168 | + int i = 0; |
| 169 | + String arg = ""; |
| 170 | + boolean found = false; |
| 171 | + |
| 172 | + while (i < args.length) { |
| 173 | + arg = args[i++]; |
| 174 | + if (arg.equals(optionName)) { |
| 175 | + if (i < args.length) |
| 176 | + argValue = args[i++]; |
| 177 | + if (argValue.startsWith("-") || argValue.equals("")) { |
| 178 | + argValue = defaultVal; |
| 179 | + } |
| 180 | + found = true; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + if (!found) { |
| 185 | + argValue = defaultVal; |
| 186 | + } |
| 187 | + } catch (Exception e) { |
| 188 | + showError("getOptionValue", e); |
| 189 | + } |
| 190 | + return argValue; |
| 191 | + } |
| 192 | + |
| 193 | + static void readPassword(String prompt) throws Exception { |
| 194 | + if (System.console() == null) { |
| 195 | + BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); |
| 196 | + showln(prompt); |
| 197 | + password = r.readLine(); |
| 198 | + } else { |
| 199 | + char[] pchars = System.console().readPassword("\n[%s]", prompt); |
| 200 | + if (pchars != null) { |
| 201 | + password = new String(pchars); |
| 202 | + java.util.Arrays.fill(pchars, ' '); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + private static void show(String msg) { |
| 208 | + System.out.println(msg); |
| 209 | + } |
| 210 | + |
| 211 | + // Show message line without new line |
| 212 | + private static void showln(String msg) { |
| 213 | + System.out.print(msg); |
| 214 | + } |
| 215 | +} |
0 commit comments