Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions java/jdbc-callablestatement/README.md
Original file line number Diff line number Diff line change
@@ -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)
83 changes: 83 additions & 0 deletions java/jdbc-callablestatement/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.oracle.dev.jdbc</groupId>
<artifactId>jdbc-callablestatement</artifactId>
<version>1.0-SNAPSHOT</version>

<name>jdbc-callablestatement</name>
<description>A simple jdbc-callablestatement.</description>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
</properties>

<dependencies>
<!-- Oracle JDBC JARs -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8-production</artifactId>
<version>21.7.0.0</version>
<type>pom</type>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>

<reporting>
<plugins>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin>
</plugins>
</reporting>
</project>
38 changes: 38 additions & 0 deletions java/jdbc-callablestatement/script/StoredProcEmployee.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
CREATE USER JDBCSP_USER IDENTIFIED BY <JDBCSP_USER_PASSWORD>;
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;
Original file line number Diff line number Diff line change
@@ -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;

/**
* <p>
* Configuration for connecting code samples to an Oracle Database instance.
* </p>
*/
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;
}

}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -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=<PATH_TO_YOUR_WALLET>
DB_URL=jdbc:oracle:thin:@callstmtdb_tpurgent?TNS_ADMIN=<PATH_TO_YOUR_WALLET>
DB_USER=<DB_USER>
DB_PASSWORD=<DB_PASSWORD>
DB_SCHEMA=<DB_SCHEMA>