Skip to content

Commit 1c5a3f1

Browse files
author
Nirmala Sundarappa
committed
Added new files
1 parent 703e26a commit 1c5a3f1

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

java/ojvm/ServersideConnect.java

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.*/
2+
/*
3+
DESCRIPTION
4+
The server-side Type 2 (T2S) driver (aka KPRB driver) is for Java in the
5+
database. Java running in the database session uses the KPRB driver or
6+
T2S driver to access data, locally.
7+
We furnish the server-side thin JDBC (aka Type 4 server driver) for
8+
accessing data in other session in the same database or a remote Oracle
9+
database.
10+
11+
Step 1: Connect to SQLPLUS using the database USER/PASSWORD.
12+
Make sure to have ServersideConnect.sql accessible on the
13+
client side to execute.
14+
Step 2: Run the SQL file after connecting to DB "@ServersideConnect.sql"
15+
16+
NOTES
17+
Use JDK 1.6 and above
18+
19+
MODIFIED (MM/DD/YY)
20+
nbsundar 03/23/15 - Creation (kmensah - Contributor)
21+
*/
22+
import java.sql.Connection;
23+
import java.sql.ResultSet;
24+
import java.sql.SQLException;
25+
import java.sql.Statement;
26+
27+
import oracle.jdbc.driver.OracleDriver;
28+
import oracle.jdbc.pool.OracleDataSource;
29+
30+
31+
public class ServersideConnect {
32+
33+
static public void jrun() throws SQLException {
34+
// For testing ServersideConnect
35+
// test("jdbc:oracle:kprb:@");
36+
method1("jdbc:default:connection");
37+
method2();
38+
}
39+
/*
40+
* Shows using the server side Type 2 driver a.k.a KPRB driver
41+
*/
42+
static public void method1(String url) throws SQLException {
43+
Connection connection = null;
44+
try {
45+
System.out.println("Connecting to URL " + url);
46+
// Method 1: Using OracleDataSource
47+
OracleDataSource ods = new OracleDataSource();
48+
ods.setURL(url);
49+
connection = ods.getConnection();
50+
System.out.println("Method 1: Getting Default Connection "
51+
+ "using OracleDataSource");
52+
// Perform database operation
53+
printEmployees(connection);
54+
55+
} catch (SQLException e) {
56+
e.printStackTrace();
57+
}
58+
}
59+
60+
61+
/*
62+
* Method to show that a connection is obtained without specifying the URL.
63+
* This method uses the default connection to establish the connection.
64+
*/
65+
66+
static public void method2() throws SQLException {
67+
Connection connection = null;
68+
// Method 2: Using defaultConnection() method
69+
OracleDriver ora = new OracleDriver();
70+
connection = ora.defaultConnection();
71+
System.out.println("Method 2: Getting Default Connection "
72+
+ "using OracleDriver");
73+
// Perform database operation
74+
printEmployees(connection);
75+
}
76+
/*
77+
78+
/*
79+
* Displays employee_id and first_name from the employees table.
80+
*/
81+
static public void printEmployees(Connection connection) throws SQLException {
82+
ResultSet resultSet = null;
83+
Statement statement = null;
84+
try {
85+
statement = connection.createStatement();
86+
resultSet = statement.executeQuery("SELECT employee_id, first_name FROM "
87+
+ "employees order by employee_id");
88+
while (resultSet.next()) {
89+
System.out.println("Emp no: " + resultSet.getInt(1) + " Emp name: "
90+
+ resultSet.getString(2));
91+
}
92+
}
93+
catch (SQLException ea) {
94+
System.out.println("Error during execution: " + ea);
95+
ea.printStackTrace();
96+
}
97+
finally {
98+
if (resultSet != null) resultSet.close();
99+
if (statement != null) statement.close();
100+
}
101+
}
102+
}
103+
104+

java/ojvm/ServersideConnect.sql

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Rem ServersideConnect.sql
2+
Rem
3+
Rem Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
4+
Rem
5+
Rem NAME
6+
Rem ServersideConnect.sql
7+
Rem
8+
Rem DESCRIPTION
9+
Rem SQL for invoking the method which gets a server side connection to
10+
Rem internal T2 Driver
11+
Rem
12+
Rem MODIFIED (MM/DD/YY)
13+
Rem nbsundar 03/23/15 - Created
14+
Rem kmensah 03/23/15 - Contributor
15+
16+
rem Reads the content of the Java source from ServersideConnect.java
17+
rem then compiles it
18+
connect hr/hr
19+
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED ServersideConnect_src AS
20+
@ ServersideConnect.java
21+
/
22+
23+
show error
24+
25+
rem A wrapper (a.k.a. Call Spec), to invoke Java
26+
rem function in the database from SQL, PL/SQL, and client applications
27+
CREATE OR REPLACE PROCEDURE ServersideConnect_proc AS
28+
LANGUAGE JAVA NAME 'ServersideConnect.jrun ()';
29+
/
30+
31+
rem Running the sample
32+
connect hr/hr
33+
SET SERVEROUTPUT ON SIZE 10000
34+
CALL dbms_java.set_output (10000);
35+
36+
execute ServersideConnect_proc;
37+

0 commit comments

Comments
 (0)