Documente Academic
Documente Profesional
Documente Cultură
Oracle Day 4
Objectives
Integration of Java and Oracle
What is Java?
Java is an object-oriented programming language developed by Sun Microsystems.
Stored in a database in compiled form so that one can share it between a number of programs
An open and portable alternative to proprietary procedural SQL extensions (PL/SQL), for implementing stored procedures.
Java stored procedures are Java classes, stored as Oracle schema objects, made accessible to Oracle SQL and PL/SQL through call specifications.
Since Oracle8i, the Oracle database embeds a Java VM, which lets run Java directly in the database
The latest Oracle 10g server is compliant with J2SE (Java 2 Server Edition) 1.4x
Java Applet
JDBC Driver
le ac Or Ne
Oracle Database
Fat Client(s)
Relational Data
SQL * Plus
PRO * Client
ODBC Client
Oracle Net
O ra cle
Ne
Oracle Forms
Oracle Reports
Should I forget all the things I've been told about PL/SQL and move on to the greener Java pastures?
Well suited for encapsulating SQL operations with procedural logic and for manipulating all database object types.
Extensively used by a large community of Oracle developers and customers for implementing database operations and packages.
Robustness
As opposed to PL/SQL, Java requires declaring exceptions that can be thrown by methods in any class, thereby making Java stored procedures more robust.
10
11
Use Java as an open alternative to PL/SQL for database independence, and for integrating and bridging the worlds of SQL, XML, J2EE, and Web services.
12
13
// Hello World Program public class Hello{ public static String helloWorld(){
14
Load the Java class into Oracle-LOADJAVA utility STEP 2/5 Utility for loading Java source files, Java class files, and Java resource files
Invoked from the command line loadjava u username/password@oracleSID -r v <classfile> For our Hello class run the following command from the command line:
15
CREATE$JAVA$LOB$TABLE
JAVA$CLASS$MD5$TABLE LOADLOBS
16
-resolve | r
-force | f
17
18
DROPJAVA utility
Reverses the action of loadjava
Converts filename into the name of schema objects, drops the schema objects, and finally deletes their digest table rows.
To drop Hello class from Oracle execute the following command from command line: dropjava u scott/tiger@training Hello.class
Copyright 2005, Infosys Technologies Ltd ER/CORP/CRS/DB25/003 Version No. 2.0
19
Following PL/SQL wrapper program is required to publish the Java method helloWorld:
RETURN VARCHAR2
AS LANGUAGE JAVA NAME 'Hello.helloWorld() return java.lang.String';
20
21
. Optional Step
JAVAUSERPRIV: To read or write a file, one only need the JAVAUSERPRIV role.
22
Define the variable to get the message returned by function SQL> VARIABLE var_msg VARCHAR2(20);
Call the Function (get the return value into the variable)
23
SQL DML statements (INSERT, UPDATE, DELETE, SELECT, CALL, EXPLAIN PLAN, LOCK TABLE, and MERGE )
Database triggers
24
If one calls a method from an INSERT, UPDATE, or DELETE statement, the method cannot query or modify any database tables modified by that statement.
If one calls a method from a SELECT, INSERT, UPDATE, or DELETE statement, the method cannot execute: SQL transaction control statements e.g. COMMIT SQL session control statements e.g. SET ROLE DDL statements.
25
26
27
28
29
30
Conclusion
The integration of the database with a JVM that complies with J2SE allows extending database features and programmability through Java Stored Procedures.
31
Summary
What is Java Stored Procedure? PL/SQL vs. Java Stored Procedures. What are the steps to create a Java Stored Procedure? How to load Java code elements? What are Call specs.? How to call Java Stored Procedures?
32
Thank You!
Copyright 2005, Infosys Technologies Ltd
33