Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 44728c2

Browse files
Added sample code and script - Invoking Stored Procedures with JDBC CallableStatements (#222)
1 parent 918aff5 commit 44728c2

File tree

6 files changed

+312
-0
lines changed

6 files changed

+312
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## Java - Oracle Developers on Medium.com
2+
[Invoking Stored Procedures with JDBC CallableStatements](https://medium.com/oracledevs/getting-started-with-invoking-stored-procedures-using-jdbc-and-callablestatement-6a247fd1957a)

‎java/jdbc-callablestatement/pom.xml‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.oracle.dev.jdbc</groupId>
8+
<artifactId>jdbc-callablestatement</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>jdbc-callablestatement</name>
12+
<description>A simple jdbc-callablestatement.</description>
13+
<!-- FIXME change it to the project's website -->
14+
<url>http://www.example.com</url>
15+
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<maven.compiler.source>19</maven.compiler.source>
19+
<maven.compiler.target>19</maven.compiler.target>
20+
</properties>
21+
22+
<dependencies>
23+
<!-- Oracle JDBC JARs -->
24+
<dependency>
25+
<groupId>com.oracle.database.jdbc</groupId>
26+
<artifactId>ojdbc8-production</artifactId>
27+
<version>21.7.0.0</version>
28+
<type>pom</type>
29+
</dependency>
30+
</dependencies>
31+
32+
<build>
33+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
34+
<plugins>
35+
<plugin>
36+
<artifactId>maven-clean-plugin</artifactId>
37+
<version>3.1.0</version>
38+
</plugin>
39+
<plugin>
40+
<artifactId>maven-site-plugin</artifactId>
41+
<version>3.7.1</version>
42+
</plugin>
43+
<plugin>
44+
<artifactId>maven-project-info-reports-plugin</artifactId>
45+
<version>3.0.0</version>
46+
</plugin>
47+
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
48+
<plugin>
49+
<artifactId>maven-resources-plugin</artifactId>
50+
<version>3.0.2</version>
51+
</plugin>
52+
<plugin>
53+
<artifactId>maven-compiler-plugin</artifactId>
54+
<version>3.8.0</version>
55+
</plugin>
56+
<plugin>
57+
<artifactId>maven-surefire-plugin</artifactId>
58+
<version>2.22.1</version>
59+
</plugin>
60+
<plugin>
61+
<artifactId>maven-jar-plugin</artifactId>
62+
<version>3.0.2</version>
63+
</plugin>
64+
<plugin>
65+
<artifactId>maven-install-plugin</artifactId>
66+
<version>2.5.2</version>
67+
</plugin>
68+
<plugin>
69+
<artifactId>maven-deploy-plugin</artifactId>
70+
<version>2.8.2</version>
71+
</plugin>
72+
</plugins>
73+
</pluginManagement>
74+
</build>
75+
76+
<reporting>
77+
<plugins>
78+
<plugin>
79+
<artifactId>maven-project-info-reports-plugin</artifactId>
80+
</plugin>
81+
</plugins>
82+
</reporting>
83+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
CREATE USER JDBCSP_USER IDENTIFIED BY <JDBCSP_USER_PASSWORD>;
2+
GRANT DWROLE TO JDBCSP_USER;
3+
GRANT CREATE SESSION TO JDBCSP_USER;
4+
GRANT UNLIMITED TABLESPACE TO JDBCSP_USER;
5+
6+
CREATE TABLE EMPLOYEE
7+
(
8+
"EMP_ID" NUMBER NOT NULL ENABLE,
9+
"NAME" VARCHAR2(20 BYTE) DEFAULT NULL,
10+
"ROLE" VARCHAR2(20 BYTE) DEFAULT NULL,
11+
"DEPARTMENT" VARCHAR2(20 BYTE) DEFAULT NULL,
12+
"BUILDING" VARCHAR2(20 BYTE) DEFAULT NULL,
13+
PRIMARY KEY ("EMP_ID")
14+
);
15+
16+
CREATE OR REPLACE PROCEDURE INSERT_EMPLOYEE_PRC
17+
(
18+
in_emp_id IN EMPLOYEE.EMP_ID%TYPE,
19+
in_name IN EMPLOYEE.NAME%TYPE,
20+
in_role IN EMPLOYEE.ROLE%TYPE,
21+
in_department IN EMPLOYEE.DEPARTMENT%TYPE,
22+
in_building IN EMPLOYEE.BUILDING%TYPE,
23+
out_result OUT VARCHAR2)
24+
AS
25+
BEGIN
26+
INSERT INTO EMPLOYEE (EMP_ID, NAME, ROLE, DEPARTMENT, BUILDING)
27+
VALUES (in_emp_id, in_name, in_role, in_department, in_building);
28+
COMMIT;
29+
30+
out_result := 'TRUE';
31+
32+
EXCEPTION
33+
WHEN OTHERS THEN
34+
out_result := 'FALSE';
35+
ROLLBACK;
36+
END;
37+
38+
GRANT EXECUTE ON INSERT_EMPLOYEE_PRC TO JDBCSP_USER;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.io.IOException;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
27+
import java.util.Properties;
28+
29+
/**
30+
* <p>
31+
* Configuration for connecting code samples to an Oracle Database instance.
32+
* </p>
33+
*/
34+
public class DatabaseConfig {
35+
36+
private static final Properties CONFIG = new Properties();
37+
38+
static {
39+
try {
40+
var fileStream = Files
41+
.newInputStream(Path.of("C:\\java-projects\\jdbc-callablestatement\\src\\main\\resources\\config.properties"));
42+
CONFIG.load(fileStream);
43+
} catch (IOException e) {
44+
e.printStackTrace();
45+
}
46+
}
47+
48+
private static final String DB_USER = CONFIG.getProperty("DB_USER");
49+
50+
private static final String DB_URL = CONFIG.getProperty("DB_URL");
51+
52+
private static final String DB_PASSWORD = CONFIG.getProperty("DB_PASSWORD");
53+
54+
private static final String DB_SCHEMA = CONFIG.getProperty("DB_SCHEMA");
55+
56+
public static String getDbUser() {
57+
return DB_USER;
58+
}
59+
60+
public static String getDbUrl() {
61+
return DB_URL;
62+
}
63+
64+
public static String getDbPassword() {
65+
return DB_PASSWORD;
66+
}
67+
68+
public static String getDbSchema() {
69+
return DB_SCHEMA;
70+
}
71+
72+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# https://docs.oracle.com/en/cloud/paas/autonomous-database/adbsa/connect-jdbc-thin-wallet.html
2+
# jdbc:oracle:thin:@dbname_tpurgent?TNS_ADMIN=<PATH_TO_YOUR_WALLET>
3+
DB_URL=jdbc:oracle:thin:@callstmtdb_tpurgent?TNS_ADMIN=<PATH_TO_YOUR_WALLET>
4+
DB_USER=<DB_USER>
5+
DB_PASSWORD=<DB_PASSWORD>
6+
DB_SCHEMA=<DB_SCHEMA>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /