diff --git a/java/jdbc-callablestatement/README.md b/java/jdbc-callablestatement/README.md new file mode 100644 index 00000000..36c77228 --- /dev/null +++ b/java/jdbc-callablestatement/README.md @@ -0,0 +1,2 @@ +## Java - Oracle Developers on Medium.com +[Invoking Stored Procedures with JDBC CallableStatements](https://medium.com/oracledevs/getting-started-with-invoking-stored-procedures-using-jdbc-and-callablestatement-6a247fd1957a) diff --git a/java/jdbc-callablestatement/pom.xml b/java/jdbc-callablestatement/pom.xml new file mode 100644 index 00000000..3f8d3c39 --- /dev/null +++ b/java/jdbc-callablestatement/pom.xml @@ -0,0 +1,83 @@ + + + + 4.0.0 + + com.oracle.dev.jdbc + jdbc-callablestatement + 1.0-SNAPSHOT + + jdbc-callablestatement + A simple jdbc-callablestatement. + + http://www.example.com + + + UTF-8 + 19 + 19 + + + + + + com.oracle.database.jdbc + ojdbc8-production + 21.7.0.0 + pom + + + + + + + + maven-clean-plugin + 3.1.0 + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + + + + + + maven-project-info-reports-plugin + + + + diff --git a/java/jdbc-callablestatement/script/StoredProcEmployee.sql b/java/jdbc-callablestatement/script/StoredProcEmployee.sql new file mode 100644 index 00000000..317e06b8 --- /dev/null +++ b/java/jdbc-callablestatement/script/StoredProcEmployee.sql @@ -0,0 +1,38 @@ +CREATE USER JDBCSP_USER IDENTIFIED BY ; +GRANT DWROLE TO JDBCSP_USER; +GRANT CREATE SESSION TO JDBCSP_USER; +GRANT UNLIMITED TABLESPACE TO JDBCSP_USER; + +CREATE TABLE EMPLOYEE +( + "EMP_ID" NUMBER NOT NULL ENABLE, + "NAME" VARCHAR2(20 BYTE) DEFAULT NULL, + "ROLE" VARCHAR2(20 BYTE) DEFAULT NULL, + "DEPARTMENT" VARCHAR2(20 BYTE) DEFAULT NULL, + "BUILDING" VARCHAR2(20 BYTE) DEFAULT NULL, + PRIMARY KEY ("EMP_ID") +); + +CREATE OR REPLACE PROCEDURE INSERT_EMPLOYEE_PRC +( + in_emp_id IN EMPLOYEE.EMP_ID%TYPE, + in_name IN EMPLOYEE.NAME%TYPE, + in_role IN EMPLOYEE.ROLE%TYPE, + in_department IN EMPLOYEE.DEPARTMENT%TYPE, + in_building IN EMPLOYEE.BUILDING%TYPE, + out_result OUT VARCHAR2) +AS +BEGIN + INSERT INTO EMPLOYEE (EMP_ID, NAME, ROLE, DEPARTMENT, BUILDING) + VALUES (in_emp_id, in_name, in_role, in_department, in_building); + COMMIT; + + out_result := 'TRUE'; + +EXCEPTION + WHEN OTHERS THEN + out_result := 'FALSE'; + ROLLBACK; +END; + +GRANT EXECUTE ON INSERT_EMPLOYEE_PRC TO JDBCSP_USER; diff --git a/java/jdbc-callablestatement/src/main/java/com/oracle/dev/jdbc/DatabaseConfig.java b/java/jdbc-callablestatement/src/main/java/com/oracle/dev/jdbc/DatabaseConfig.java new file mode 100644 index 00000000..913eb184 --- /dev/null +++ b/java/jdbc-callablestatement/src/main/java/com/oracle/dev/jdbc/DatabaseConfig.java @@ -0,0 +1,72 @@ +/* + Copyright (c) 2021, 2022, Oracle and/or its affiliates. + + This software is dual-licensed to you under the Universal Permissive License + (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License + 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose + either license. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.oracle.dev.jdbc; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Properties; + +/** + *

+ * Configuration for connecting code samples to an Oracle Database instance. + *

+ */ +public class DatabaseConfig { + + private static final Properties CONFIG = new Properties(); + + static { + try { + var fileStream = Files + .newInputStream(Path.of("C:\\java-projects\\jdbc-callablestatement\\src\\main\\resources\\config.properties")); + CONFIG.load(fileStream); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static final String DB_USER = CONFIG.getProperty("DB_USER"); + + private static final String DB_URL = CONFIG.getProperty("DB_URL"); + + private static final String DB_PASSWORD = CONFIG.getProperty("DB_PASSWORD"); + + private static final String DB_SCHEMA = CONFIG.getProperty("DB_SCHEMA"); + + public static String getDbUser() { + return DB_USER; + } + + public static String getDbUrl() { + return DB_URL; + } + + public static String getDbPassword() { + return DB_PASSWORD; + } + + public static String getDbSchema() { + return DB_SCHEMA; + } + +} diff --git a/java/jdbc-callablestatement/src/main/java/com/oracle/dev/jdbc/JDBCStoredProcEmployee.java b/java/jdbc-callablestatement/src/main/java/com/oracle/dev/jdbc/JDBCStoredProcEmployee.java new file mode 100644 index 00000000..cab4f0e8 --- /dev/null +++ b/java/jdbc-callablestatement/src/main/java/com/oracle/dev/jdbc/JDBCStoredProcEmployee.java @@ -0,0 +1,111 @@ +/* + Copyright (c) 2021, 2022, Oracle and/or its affiliates. + + This software is dual-licensed to you under the Universal Permissive License + (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License + 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose + either license. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package com.oracle.dev.jdbc; + +import java.sql.CallableStatement; +import java.sql.SQLException; +import java.util.Properties; +import java.util.concurrent.ThreadLocalRandom; + +import oracle.jdbc.OracleConnection; +import oracle.jdbc.pool.OracleDataSource; + +public class JDBCStoredProcEmployee { + + private final static String DB_URL = DatabaseConfig.getDbUrl(); + private final static String DB_USER = DatabaseConfig.getDbUser(); + private final static String DB_PASSWORD = DatabaseConfig.getDbPassword(); + private static OracleConnection con; + private static CallableStatement stmt; + + public static void main(String[] args) { + + System.out.println("--------------------"); + System.out.println("Input parameters"); + System.out.println("--------------------"); + int id = ThreadLocalRandom.current().nextInt(); + System.out.println("ID: " + id); + + String name = "Duke"; + System.out.println("Name: " + name); + + String role = "Mascott"; + System.out.println("Role: " + role); + + String department = "Dev Evangelism"; + System.out.println("Department: " + department); + + String building = "Block A"; + System.out.println("Building: " + building); + + try { + + Properties info = new Properties(); + info.put(OracleConnection.CONNECTION_PROPERTY_USER_NAME, DB_USER); + info.put(OracleConnection.CONNECTION_PROPERTY_PASSWORD, DB_PASSWORD); + info.put(OracleConnection.CONNECTION_PROPERTY_FAN_ENABLED, false); + + // JDBC datasource + OracleDataSource ods = new OracleDataSource(); + ods.setURL(DB_URL); + ods.setConnectionProperties(info); + + // JDBC connection + con = (OracleConnection) ods.getConnection(); + + // CallableStatement + // https://docs.oracle.com/en/java/javase/19/docs/api/java.sql/java/sql/CallableStatement.html + stmt = con.prepareCall("{call ADMIN.INSERT_EMPLOYEE_PRC(?,?,?,?,?,?)}"); + + // set IN parameters + stmt.setInt(1, id); + stmt.setString(2, name); + stmt.setString(3, role); + stmt.setString(4, department); + stmt.setString(5, building); + + // register OUT parameter + stmt.registerOutParameter(6, java.sql.Types.VARCHAR); + + stmt.executeUpdate(); + + // get OUT parameter + String result = stmt.getString(6); + + System.out.println("--------------------\n"); + System.out.println("Output parameter"); + System.out.println("--------------------"); + System.out.println("Procedured executed : " + result); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + stmt.close(); + con.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + +} \ No newline at end of file diff --git a/java/jdbc-callablestatement/src/main/resources/config.properties b/java/jdbc-callablestatement/src/main/resources/config.properties new file mode 100644 index 00000000..cc0fc964 --- /dev/null +++ b/java/jdbc-callablestatement/src/main/resources/config.properties @@ -0,0 +1,6 @@ +# https://docs.oracle.com/en/cloud/paas/autonomous-database/adbsa/connect-jdbc-thin-wallet.html +# jdbc:oracle:thin:@dbname_tpurgent?TNS_ADMIN= +DB_URL=jdbc:oracle:thin:@callstmtdb_tpurgent?TNS_ADMIN= +DB_USER= +DB_PASSWORD= +DB_SCHEMA= \ No newline at end of file