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 8307edb

Browse files
juarezjuniorgithubjeandelavarene
andauthored
Code sample - 23c BOOLEAN data type with JDBC + PLSQL23c jdbc bool plsql (#311)
* Code sample - 23c BOOLEAN data type with JDBC + PLSQL * code sample adjustments * additional adjustments * additional adjustments * 2 spaces indentation + closing of JDBC resources double-checked * 2 spaces indentation for POM * 2 spaces indentation + try with resources * removed duplicate dir 23c-jdbc-bool-plsql --------- Co-authored-by: Jean de Lavarene <jean.de.lavarene@oracle.com>
1 parent bb3ff39 commit 8307edb

File tree

3 files changed

+231
-0
lines changed

3 files changed

+231
-0
lines changed

‎java/jdbc-bool-plsql/pom.xml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<groupId>com.oracle.dev.jdbc</groupId>
9+
<artifactId>jdbc-bool-plsql</artifactId>
10+
<version>1.0-SNAPSHOT</version>
11+
12+
<name>jdbc-bool-plsql</name>
13+
<description>The new BOOLEAN data type in Oracle Database 23c with PL/SQL and
14+
the JDBC Drivers (21c, 23c)</description>
15+
<url>
16+
https://medium.com/oracledevs/the-new-boolean-data-type-in-oracle-database-23c-with-pl-sql-and-the-jdbc-drivers-21c-23c-c83957e7e21e</url>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<maven.compiler.source>21</maven.compiler.source>
21+
<maven.compiler.target>21</maven.compiler.target>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>com.oracle.database.jdbc</groupId>
27+
<artifactId>ojdbc11-production</artifactId>
28+
<version>23.4.0.24.05</version>
29+
<type>pom</type>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.slf4j</groupId>
33+
<artifactId>slf4j-api</artifactId>
34+
<version>2.0.7</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.slf4j</groupId>
38+
<artifactId>slf4j-simple</artifactId>
39+
<version>2.0.7</version>
40+
</dependency>
41+
42+
</dependencies>
43+
44+
<build>
45+
<pluginManagement><!-- lock down plugins versions to avoid using Maven
46+
defaults (may be moved to parent pom) -->
47+
<plugins>
48+
<plugin>
49+
<artifactId>maven-clean-plugin</artifactId>
50+
<version>3.1.0</version>
51+
</plugin>
52+
<plugin>
53+
<artifactId>maven-site-plugin</artifactId>
54+
<version>3.7.1</version>
55+
</plugin>
56+
<plugin>
57+
<artifactId>maven-project-info-reports-plugin</artifactId>
58+
<version>3.0.0</version>
59+
</plugin>
60+
<!-- see
61+
http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
62+
<plugin>
63+
<artifactId>maven-resources-plugin</artifactId>
64+
<version>3.0.2</version>
65+
</plugin>
66+
<plugin>
67+
<artifactId>maven-compiler-plugin</artifactId>
68+
<version>3.8.0</version>
69+
</plugin>
70+
<plugin>
71+
<artifactId>maven-surefire-plugin</artifactId>
72+
<version>2.22.1</version>
73+
</plugin>
74+
<plugin>
75+
<artifactId>maven-jar-plugin</artifactId>
76+
<version>3.0.2</version>
77+
</plugin>
78+
<plugin>
79+
<artifactId>maven-install-plugin</artifactId>
80+
<version>2.5.2</version>
81+
</plugin>
82+
<plugin>
83+
<artifactId>maven-deploy-plugin</artifactId>
84+
<version>2.8.2</version>
85+
</plugin>
86+
</plugins>
87+
</pluginManagement>
88+
</build>
89+
90+
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
CREATE USER JDBCSP_USER IDENTIFIED BY <JDBCSP_USER_PASSWORD>;
2+
GRANT DB_DEVELOPER_ROLE TO JDBCSP_USER;
3+
GRANT CREATE SESSION TO JDBCSP_USER;
4+
GRANT UNLIMITED TABLESPACE TO JDBCSP_USER;
5+
6+
CREATE TABLE HQ_EMPLOYEE
7+
(
8+
"EMP_ID" NUMBER NOT NULL ENABLE,
9+
"NAME" VARCHAR2(20 BYTE) DEFAULT NULL,
10+
"ROLE" VARCHAR2(20 BYTE) DEFAULT NULL,
11+
"ACTIVE" BOOLEAN,
12+
PRIMARY KEY ("EMP_ID")
13+
);
14+
COMMIT;
15+
16+
CREATE OR REPLACE PROCEDURE INSERT_HQ_EMPLOYEE_PRC
17+
(
18+
in_emp_id IN HQ_EMPLOYEE.EMP_ID%TYPE,
19+
in_name IN HQ_EMPLOYEE.NAME%TYPE,
20+
in_role IN HQ_EMPLOYEE.ROLE%TYPE,
21+
in_active IN HQ_EMPLOYEE.ACTIVE%TYPE,
22+
out_result OUT VARCHAR2)
23+
AS
24+
BEGIN
25+
INSERT INTO HQ_EMPLOYEE (EMP_ID, NAME, ROLE, ACTIVE)
26+
VALUES (in_emp_id, in_name, in_role, in_active);
27+
COMMIT;
28+
29+
out_result := 'TRUE';
30+
31+
EXCEPTION
32+
WHEN OTHERS THEN
33+
out_result := 'FALSE';
34+
ROLLBACK;
35+
END;
36+
37+
GRANT EXECUTE ON INSERT_HQ_EMPLOYEE_PRC TO JDBCSP_USER;
38+
39+
--- SELECT * FROM HQ_EMPLOYEE;
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Copyright (c) 2024 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 JDBCStoredProcHqEmployee {
33+
34+
private final static String DB_URL = "jdbc:oracle:thin:@<DB_HOST>:<DB_PORT>/<DB_NAME>";
35+
private final static String DB_USER = "<DB_USER>";
36+
private final static String DB_PASSWORD = "<DB_PASSWORD>";
37+
38+
public static void main(String[] args) throws SQLException {
39+
40+
System.out.println("--------------------");
41+
System.out.println("Input parameters");
42+
System.out.println("--------------------");
43+
44+
int id = ThreadLocalRandom.current().nextInt();
45+
System.out.println("ID: " + id);
46+
47+
String name = "Duke";
48+
System.out.println("Name: " + name);
49+
50+
String role = "Mascott";
51+
System.out.println("Role: " + role);
52+
53+
Properties info = new Properties();
54+
info.put(OracleConnection.CONNECTION_PROPERTY_USER_NAME, DB_USER);
55+
info.put(OracleConnection.CONNECTION_PROPERTY_PASSWORD, DB_PASSWORD);
56+
info.put(OracleConnection.CONNECTION_PROPERTY_FAN_ENABLED, false);
57+
58+
// JDBC datasource
59+
OracleDataSource ods = new OracleDataSource();
60+
ods.setURL(DB_URL);
61+
ods.setConnectionProperties(info);
62+
63+
// JDBC connection
64+
try (OracleConnection con = (OracleConnection) ods.getConnection();
65+
66+
// CallableStatement
67+
// https://docs.oracle.com/en/java/javase/19/docs/api/java.sql/java/sql/CallableStatement.html
68+
CallableStatement stmt = con
69+
.prepareCall("{call INSERT_HQ_EMPLOYEE_PRC(?,?,?,?,?)}");) {
70+
71+
// set IN parameters
72+
stmt.setInt(1, id);
73+
stmt.setString(2, name);
74+
stmt.setString(3, role);
75+
76+
/*
77+
* Please note that there was a PLSQL BOOLEAN type before 23c. What's new
78+
* in 23c is the BOOLEAN type in SQL. What's interesting here is that
79+
* we're using a BOOLEAN table column type as a parameter type in a PLSQL
80+
* procedure
81+
*/
82+
stmt.setBoolean(4, true);
83+
84+
// register OUT parameter
85+
stmt.registerOutParameter(5, java.sql.Types.VARCHAR);
86+
87+
stmt.executeUpdate();
88+
89+
// get OUT parameter
90+
String result = stmt.getString(5);
91+
92+
System.out.println("--------------------\n");
93+
System.out.println("Output parameter");
94+
System.out.println("--------------------");
95+
System.out.println("Procedured executed : " + result);
96+
97+
} catch (Exception e) {
98+
e.printStackTrace();
99+
}
100+
101+
}
102+
}

0 commit comments

Comments
(0)

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