|
| 1 | +/* |
| 2 | +Copyright (c) 2021, 2022, Oracle and/or its affiliates. |
| 3 | +
|
| 4 | +This software is dual-licensed to you under the Universal Permissive License |
| 5 | +(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License |
| 6 | +2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose |
| 7 | +either license. |
| 8 | +
|
| 9 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +you may not use this file except in compliance with the License. |
| 11 | +You may obtain a copy of the License at |
| 12 | +
|
| 13 | +https://www.apache.org/licenses/LICENSE-2.0 |
| 14 | +
|
| 15 | +Unless required by applicable law or agreed to in writing, software |
| 16 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | +See the License for the specific language governing permissions and |
| 19 | +limitations under the License. |
| 20 | +*/ |
| 21 | + |
| 22 | +package com.oracle.dev.jdbc; |
| 23 | + |
| 24 | +import java.sql.CallableStatement; |
| 25 | +import java.sql.SQLException; |
| 26 | +import java.util.Properties; |
| 27 | +import java.util.concurrent.ThreadLocalRandom; |
| 28 | + |
| 29 | +import oracle.jdbc.OracleConnection; |
| 30 | +import oracle.jdbc.pool.OracleDataSource; |
| 31 | + |
| 32 | +public class JDBCStoredProcEmployee { |
| 33 | + |
| 34 | +private final static String DB_URL = DatabaseConfig.getDbUrl(); |
| 35 | +private final static String DB_USER = DatabaseConfig.getDbUser(); |
| 36 | +private final static String DB_PASSWORD = DatabaseConfig.getDbPassword(); |
| 37 | +private static OracleConnection con; |
| 38 | +private static CallableStatement stmt; |
| 39 | + |
| 40 | +public static void main(String[] args) { |
| 41 | + |
| 42 | +System.out.println("--------------------"); |
| 43 | +System.out.println("Input parameters"); |
| 44 | +System.out.println("--------------------"); |
| 45 | +int id = ThreadLocalRandom.current().nextInt(); |
| 46 | +System.out.println("ID: " + id); |
| 47 | + |
| 48 | +String name = "Duke"; |
| 49 | +System.out.println("Name: " + name); |
| 50 | + |
| 51 | +String role = "Mascott"; |
| 52 | +System.out.println("Role: " + role); |
| 53 | + |
| 54 | +String department = "Dev Evangelism"; |
| 55 | +System.out.println("Department: " + department); |
| 56 | + |
| 57 | +String building = "Block A"; |
| 58 | +System.out.println("Building: " + building); |
| 59 | + |
| 60 | +try { |
| 61 | + |
| 62 | +Properties info = new Properties(); |
| 63 | +info.put(OracleConnection.CONNECTION_PROPERTY_USER_NAME, DB_USER); |
| 64 | +info.put(OracleConnection.CONNECTION_PROPERTY_PASSWORD, DB_PASSWORD); |
| 65 | +info.put(OracleConnection.CONNECTION_PROPERTY_FAN_ENABLED, false); |
| 66 | + |
| 67 | +// JDBC datasource |
| 68 | +OracleDataSource ods = new OracleDataSource(); |
| 69 | +ods.setURL(DB_URL); |
| 70 | +ods.setConnectionProperties(info); |
| 71 | + |
| 72 | +// JDBC connection |
| 73 | +con = (OracleConnection) ods.getConnection(); |
| 74 | + |
| 75 | +// CallableStatement |
| 76 | +// https://docs.oracle.com/en/java/javase/19/docs/api/java.sql/java/sql/CallableStatement.html |
| 77 | +stmt = con.prepareCall("{call ADMIN.INSERT_EMPLOYEE_PRC(?,?,?,?,?,?)}"); |
| 78 | + |
| 79 | +// set IN parameters |
| 80 | +stmt.setInt(1, id); |
| 81 | +stmt.setString(2, name); |
| 82 | +stmt.setString(3, role); |
| 83 | +stmt.setString(4, department); |
| 84 | +stmt.setString(5, building); |
| 85 | + |
| 86 | +// register OUT parameter |
| 87 | +stmt.registerOutParameter(6, java.sql.Types.VARCHAR); |
| 88 | + |
| 89 | +stmt.executeUpdate(); |
| 90 | + |
| 91 | +// get OUT parameter |
| 92 | +String result = stmt.getString(6); |
| 93 | + |
| 94 | +System.out.println("--------------------\n"); |
| 95 | +System.out.println("Output parameter"); |
| 96 | +System.out.println("--------------------"); |
| 97 | +System.out.println("Procedured executed : " + result); |
| 98 | + |
| 99 | +} catch (Exception e) { |
| 100 | +e.printStackTrace(); |
| 101 | +} finally { |
| 102 | +try { |
| 103 | +stmt.close(); |
| 104 | +con.close(); |
| 105 | +} catch (SQLException e) { |
| 106 | +e.printStackTrace(); |
| 107 | +} |
| 108 | +} |
| 109 | +} |
| 110 | + |
| 111 | +} |
0 commit comments