Skip to content

Commit 1d4d541

Browse files
authored
Add files via upload
1 parent d76001a commit 1d4d541

File tree

3 files changed

+306
-0
lines changed

3 files changed

+306
-0
lines changed

java/ojvm/Workers_OJVM.sql

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
REM
2+
REM
3+
REM ------------------------------------------------------------------------------
4+
REM Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
5+
REM
6+
REM Portions Copyright 2006-2015, Kuassi Mensah. All rights reserved.
7+
REM https://www.amazon.com/dp/1555583296
8+
REM Adapated from existing JDBC demo.
9+
REM
10+
REM ------------------------------------------------------------------------------
11+
REM DESCRIPTION
12+
REM The following code sample runs directly in the database (using OJVM).
13+
REM It retrieves a worker from a database, then updates its position and salary.
14+
REM
15+
create or replace and resolve java source named Workers as
16+
17+
import java.sql.*;
18+
import oracle.jdbc.driver.*;
19+
20+
public class Workers
21+
{
22+
23+
public static void main (String args []) throws SQLException {
24+
25+
String name = null;
26+
String pos = null;
27+
int sal;
28+
int id;
29+
long t0,t1;
30+
Connection conn = null;
31+
Statement stmt = null;
32+
PreparedStatement pstmt = null;
33+
34+
if ( args.length < 1 ) {
35+
System.err.println("Usage: Java Workers <wid> <new position> <new salary>");
36+
System.exit(1);
37+
}
38+
39+
// Get parameters value
40+
id = Integer.parseInt(args[0]);
41+
pos = args[1];
42+
sal = Integer.parseInt(args[2]);
43+
44+
/*
45+
* Where is your code running: in the database or outside?
46+
*/
47+
if (System.getProperty("oracle.jserver.version") != null)
48+
{
49+
/*
50+
* You are in the database, already connected, use the default
51+
* connection
52+
*/
53+
conn = DriverManager.getConnection("jdbc:default:connection:");
54+
System.out.println ("Running in OracleJVM, in the database!");
55+
}
56+
else
57+
{
58+
/*
59+
* You are not in the database, you need to connect to
60+
* the database
61+
*/
62+
63+
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
64+
conn = DriverManager.getConnection("jdbc:oracle:thin:",
65+
"scott", "tiger");
66+
System.out.println ("Running in JDK VM, outside the database!");
67+
}
68+
69+
/*
70+
* Auto commit is off by default in OJVM
71+
* Set auto commit off in client JDBC
72+
*/
73+
conn.setAutoCommit (false);
74+
75+
// Start timing
76+
t0=System.currentTimeMillis();
77+
78+
/*
79+
* find the name of the workers given his id number
80+
*/
81+
82+
// create statement
83+
stmt = conn.createStatement();
84+
85+
// find the name of the worker
86+
ResultSet rset = stmt.executeQuery(
87+
"SELECT WNAME FROM workers WHERE wid = " + id);
88+
89+
// retrieve and print the result (we are only expecting 1 row
90+
while (rset.next())
91+
{
92+
name = rset.getString(1);
93+
}
94+
95+
// return the name of the worker who has the given worker number
96+
System.out.println ("Worker Name: "+ name);
97+
98+
/*
99+
* update the position and salary of the retrieved worker
100+
*/
101+
102+
// prepare the update statement
103+
pstmt = conn.prepareStatement("UPDATE WORKERS SET WPOSITION = ?, " +
104+
" WSALARY = ? WHERE WNAME = ?");
105+
106+
// set up bind values and execute the update
107+
pstmt.setString(1, pos);
108+
pstmt.setInt(2, sal);
109+
pstmt.setString(3, name);
110+
pstmt.execute();
111+
112+
// double-check (retrieve) the updated position and salary
113+
rset = stmt.executeQuery(
114+
"SELECT WPOSITION, WSALARY FROM WORKERS WHERE WNAME = '" +
115+
name + "'");
116+
while (rset.next())
117+
{
118+
pos = rset.getString ("wposition");
119+
sal = rset.getInt ("wsalary");
120+
}
121+
System.out.println ("Worker: Id = " + id + ", Name = " + name +
122+
", Position = " + pos + ", Salary = " + sal);
123+
124+
// Close the ResultSet
125+
rset.close();
126+
127+
// Close the Statement
128+
stmt.close();
129+
130+
// Stop timing
131+
t1=System.currentTimeMillis();
132+
System.out.println ("====> Duration: "+(int)(t1-t0)+ " Milliseconds");
133+
134+
// Close the connection
135+
conn.close();
136+
}
137+
}
138+
139+
/
140+
show errors;
141+
142+
create or replace procedure WorkerSP (wid IN varchar2,wpos IN
143+
varchar2, wsal IN varchar2) as
144+
language java name 'Workers.main(java.lang.String[])';
145+
/
146+
show errors;
147+
148+
set serveroutput on
149+
call dbms_java.set_output(50000);
150+
call WorkerSp('621', 'Senior VP', '650000');

java/ojvm/Workers_jdbc.java

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
/------------------------------------------------------------------------------
3+
/ Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
4+
/
5+
/ Portions Copyright 2006-2015, Kuassi Mensah. All rights reserved.
6+
/ https://www.amazon.com/dp/1555583296
7+
/ Adapated from existing JDBC demo.
8+
/
9+
/------------------------------------------------------------------------------
10+
/ DESCRIPTION
11+
/ The following code sample runs as a stand-alone JDBC client usingJ RE or JDK.
12+
/ It retrieves a worker from a database, then updates its position and salary.
13+
*/
14+
15+
import java.sql.*;
16+
import oracle.jdbc.driver.*;
17+
18+
public class Workers_jdbc
19+
{
20+
21+
public static void main (String args []) throws SQLException {
22+
23+
String name = null;
24+
String pos = null;
25+
int sal;
26+
int id;
27+
long t0,t1;
28+
Connection conn = null;
29+
Statement stmt = null;
30+
PreparedStatement pstmt = null;
31+
32+
if ( args.length < 1 ) {
33+
System.err.println("Usage: Java Workers <wid> <new position> <new salary>");
34+
System.exit(1);
35+
}
36+
37+
// Get parameters value
38+
id = Integer.parseInt(args[0]);
39+
pos = args[1];
40+
sal = Integer.parseInt(args[2]);
41+
42+
/*
43+
* Where is your code running: in the database or outside?
44+
*/
45+
if (System.getProperty("oracle.jserver.version") != null)
46+
{
47+
/*
48+
* You are in the database, already connected, use the default
49+
* connection
50+
*/
51+
conn = DriverManager.getConnection("jdbc:default:connection:");
52+
System.out.println ("Running in OracleJVM, in the database!");
53+
}
54+
else
55+
{
56+
/*
57+
* You are not in the database, you need to connect to
58+
* the database
59+
*/
60+
61+
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
62+
conn = DriverManager.getConnection("jdbc:oracle:thin:",
63+
"scott", "tiger");
64+
System.out.println ("Running in JDK VM, outside the database!");
65+
}
66+
67+
68+
/*
69+
* Auto commit is off by default in OracleJVM (not supported)
70+
* Set auto commit off in JDBC case
71+
*/
72+
conn.setAutoCommit (false);
73+
74+
// Start timing
75+
t0=System.currentTimeMillis();
76+
77+
/*
78+
* find the name of the workers given his id number
79+
*/
80+
81+
// create statement
82+
stmt = conn.createStatement();
83+
84+
// find the name of the worker
85+
ResultSet rset = stmt.executeQuery(
86+
"SELECT WNAME FROM workers WHERE wid = " + id);
87+
88+
// retrieve and print the result (we are only expecting 1 row
89+
while (rset.next())
90+
{
91+
name = rset.getString(1);
92+
}
93+
94+
// return the name of the worker who has the given worker number
95+
System.out.println ("Worker Name: "+ name);
96+
97+
/*
98+
* update the position and salary of the retrieved worker
99+
*/
100+
101+
// prepare the update statement
102+
pstmt = conn.prepareStatement("UPDATE WORKERS SET WPOSITION = ?, " +
103+
" WSALARY = ? WHERE WNAME = ?");
104+
105+
// set up bind values and execute the update
106+
pstmt.setString(1, pos);
107+
pstmt.setInt(2, sal);
108+
pstmt.setString(3, name);
109+
pstmt.execute();
110+
111+
// double-check (retrieve) the updated position and salary
112+
rset = stmt.executeQuery(
113+
"SELECT WPOSITION, WSALARY FROM WORKERS WHERE WNAME = '" +
114+
name + "'");
115+
while (rset.next())
116+
{
117+
pos = rset.getString ("wposition");
118+
sal = rset.getInt ("wsalary");
119+
}
120+
System.out.println ("Worker: Id = " + id + ", Name = " + name +
121+
", Position = " + pos + ", Salary = " + sal);
122+
123+
// Close the ResultSet
124+
rset.close();
125+
126+
// Close the Statement
127+
stmt.close();
128+
129+
// Stop timing
130+
t1=System.currentTimeMillis();
131+
System.out.println ("====> Duration: "+(int)(t1-t0)+ " Milliseconds");
132+
133+
// Close the connection
134+
conn.close();
135+
}
136+
}

java/ojvm/Workers_table.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
rem
2+
rem Workers.sql
3+
rem
4+
connect hr/hr@pdb1
5+
6+
rem
7+
rem Set up the Workers table
8+
rem
9+
10+
drop table workers;
11+
create table workers (wid int, wname varchar2(20),
12+
wposition varchar2(25), wsalary int);
13+
14+
insert into workers values (103, 'Adams Tagnon', 'Janitor', 10000);
15+
insert into workers values (201, 'John Doe', 'Secretary', 20000);
16+
insert into workers values (323, 'Racine Johnson', 'Junior Staff Member', 30000);
17+
insert into workers values (418, 'Abraham Wilson', 'Senior Staff Member', 40000);
18+
insert into workers values (521, 'Jesus Nucci', 'Engineer', 50000);
19+
insert into workers values (621, 'Jean Francois', 'Engineer', 60000);
20+
commit;

0 commit comments

Comments
 (0)