BurningLuffy.DW 发表于 2013-2-3 13:59:01

Summary of Oracle Java Stored Procedure

 
1. Overview of Oracle Java StoredProcedure

Java is an object-oriented programming language efficient forapplication-level programs. Oracle provides all types of JDBC drivers andenhances database access from Java applications. Java Stored Procedures areportable and secure in terms of access control, and allow non-Java and legacyapplications to transparently invoke Java.
Storedprocedures are Java methods published to SQL and stored in the database forgeneral use. To publish Java methods, you write call specifications, which mapJava method names, parameter types, and return types to their SQL counterparts.
Whencalled by client applications, a stored procedure can accept arguments,reference Java classes, and return Java result values. Figure 1-1 shows a stored procedure being called by various applications.
 

http://dl.iteye.com/upload/attachment/0062/7190/57b92484-ae0e-3c51-afeb-c3048852166c.bmp
 

2. Java Stored Procedure Configuration

To configure the database to run Java stored procedures, you must decide onwhich of the following modes the database should run:
■ Dedicated server mode
You must configure the database and clients in dedicated server mode using OracleNet Services connections.
■ Shared server mode
You must configure the server for shared server mode with the DISPATCHERS parameter.
Java, SQL, or PL/SQL clients, which run Java stored procedures on theserver, connect to the database over an Oracle Net Services connection.
3. Java Stored Procedures Steps

You can run Java stored procedures in the same way as PL/SQL storedprocedures. Normally, a call to a Java stored procedure is a result of databasemanipulation, because it is usually the result of a trigger or SQL DML call. Tocall a Java stored procedure, you must publish it through a call specification.
Before you can call Java stored procedures, you must load them into theOracle Database instance and publish them to SQL. Loading and publishing areseparate tasks. Many Java classes, which are referenced only by other Javaclasses, are never published.
To load Java stored procedures automatically, you can use the loadjava command-lineutility. It loads Java source, class, and resource files into a system-generateddatabase table, and then uses the SQL CREATE JAVA {SOURCE | CLASS | RESOURCE}statement to load the Java files into the Oracle Database instance. You canupload Java files from file systems, popular Java IDEs, intranets, or theInternet.
The following steps are involved in creating, loading, and calling Javastored procedures:
 
Step 1: Create or Reuse the Java Classes

importjava.math.BigDecimal;
 
publicclass Paymaster {
                public static BigDecimalwages(BigDecimal  sal, BigDecimal comm)throws java.sql.SQLException {
                                BigDecimal pay = sal;
                                if (comm !=null)
                                                pay= pay.add(comm);
                                return pay;
                }
}
 
Save the class as Oscar.java. Using a Java compiler(For Oracle 11g, theversion of Java should be 1.5), compile the .java file on your client system,as follows:
javac Oscar.java
The compiler outputs a Java binary file, in this case, Oscar.class.
In a call specification, the corresponding SQL and Java parameters andfunction results must have compatible data types. Table 1–1 lists the legaldata type mappings. Oracle Database converts between the SQL types and Javaclasses automatically.
Table 1–1 Legal Data Type Mappings SQL TypeJava Class
CHAR, LONG, VARCHAR2  oracle.sql.CHAR
java.lang.String
java.sql.Date
java.sql.Time
java.sql.Timestamp
java.lang.Byte
java.lang.Short
java.lang.Integer
java.lang.Long
java.lang.Float
java.lang.Double
java.math.BigDecimal
byte, short, int, long, float, double
DATE                 oracle.sql.DATE
java.sql.Date
java.sql.Time
java.sql.Timestamp
java.lang.String
NUMBER               oracle.sql.NUMBER
java.lang.Byte
java.lang.Short
java.lang.Integer
java.lang.Long
java.lang.Float
java.lang.Double
java.math.BigDecimal
byte, short, int, long, float, double
OPAQUE              oracle.sql.OPAQUE
RAW, LONG RAW         oracle.sql.RAW
byte[]
ROWID                oracle.sql.CHAR
oracle.sql.ROWID
java.lang.String
BFILE                oracle.sql.BFILE
BLOB                 oracle.sql.BLOB
oracle.jdbc2.Blob
CLOB, NCLOBoracle.sql.CLOB
oracle.jdbc2.Clob
OBJECT               oracle.sql.STRUCT
Objecttypes                            java.sql.Struct
java.sql.SqlData
oracle.sql.ORAData
REF                  oracle.sql.REF
Referencetypes                       java.sql.Ref
oracle.sql.ORAData
TABLE, VARRAY         oracle.sql.ARRAY
Nestedtable types                                  
andVARRAY types oracle.sql.ORAData
java.sql.Array
anyof the preceding SQL   oracle.sql.CustomDatum
types                                        oracle.sql.Datum
 
Step 2: Load and Resolve the Java Classes

Using the loadjava utility, you can load Java source, class, and resourcefiles into an Oracle Database instance, where they are stored as Java schemaobjects. You can run loadjava from the command line or from an application, andyou can specify several options including a resolver.

http://dl.iteye.com/upload/attachment/0062/7192/4b7487a4-bb6f-31a5-bf2d-d6f2a6d4586a.bmp
Step 3: Publish the Java Classes


http://dl.iteye.com/upload/attachment/0062/7194/5c6260e8-50bc-3394-a529-286e7bdad45a.bmp
 

Step 4: Call the Stored Procedures

 

http://dl.iteye.com/upload/attachment/0062/7198/a63c7948-2d72-37a7-bcac-17ee09735b7e.bmp
 
Moreintroductions could be found in Oracle Database Java Developer’s Guide.pdf 
 
页: [1]
查看完整版本: Summary of Oracle Java Stored Procedure