Skip to content

Commit 555fea5

Browse files
author
Nirmala Sundarappa
committed
Checking in new sample
1 parent c045c22 commit 555fea5

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

java/ojvm/JavaStoredProcSample.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.sql.DriverManager;
2+
import java.sql.Connection;
3+
import java.sql.PreparedStatement;
4+
import java.sql.ResultSet;
5+
import java.sql.SQLException;
6+
7+
8+
/* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.*/
9+
10+
/*
11+
DESCRIPTION
12+
A very basic Java stored procedure sample. For more complex Java stored procedure samples,
13+
please explore https://github.com/oracle/oracle-db-examples/tree/master/java/ojvm directory.
14+
Java stored procedure in the database executed using the KPRB JDBC driver in the Oracle JVM instance.
15+
To run the sample:
16+
1. loadjava -user 'jdbcuser/jdbcuser123' -oci8 -resolve -force -verbose JavaStoredProcSample.java
17+
This loads a java stored procedure in the database.
18+
2. sqlplus jdbcuser/jdbcuser123 @JavaStoredProcSample.sql
19+
This calls java stored procedure from sqlplus and print number of emplyoees in the department number 20.
20+
*/
21+
22+
public class JavaStoredProcSample {
23+
24+
final static String DEFAULT_URL = "jdbc:default:connection:";
25+
26+
// Get the total number of employees for a given department.
27+
public static int getEmpCountByDept(int deptNo) {
28+
int count = 0;
29+
30+
try {
31+
// Get default connection on the current session from the client
32+
Connection conn = DriverManager.getConnection(DEFAULT_URL);
33+
34+
// Execute a SQL query
35+
String sql = "SELECT COUNT(1) FROM EMP WHERE DEPTNO = ?";
36+
37+
// Gets the result value
38+
try(PreparedStatement pstmt = conn.prepareStatement(sql)) {
39+
pstmt.setInt(1, deptNo);
40+
try (ResultSet rs = pstmt.executeQuery()) {
41+
if (rs.next()) {
42+
count = rs.getInt(1);
43+
}
44+
}
45+
}
46+
catch(SQLException sqe) {
47+
System.out.println(sqe);
48+
}
49+
}
50+
catch(SQLException sqe) {
51+
System.out.println(sqe);
52+
}
53+
54+
// Returns the calculated result value
55+
return count;
56+
}
57+
}

0 commit comments

Comments
 (0)