Download Java Stored Procedure

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Microsoft Jet Database Engine wikipedia , lookup

Database model wikipedia , lookup

Clusterpoint wikipedia , lookup

Relational model wikipedia , lookup

Oracle Database wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Object-relational impedance mismatch wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
Java Stored Procedures
• Introduction
• Benefits of Java Stored Procedures
• Deployment of Java Stored Procedures (in
four steps).
Prepared by: Prem Jayaraman
Revised by: M V Ramakrishna
Introduction
• Java stored procedures are Java classes, stored
as Oracle schema objects, made accessible to
Oracle SQL and PL/SQL through call
specifications. The java classes would include
SQL statements.
• Java enables development of database-neutral
code.
• A stored procedure is a program that is stored
and executed within database server. The
procedure may be called from a Java class, from
a PL/SQL programming block etc.
CSE5200 - Java Stored Procedure
– Starting with Oracle 8i, a fully functional JVM
has been integrated into the Oracle database,
which enables Java classes to be executed
as stored procedures within an Oracle
Database Environment
– Thus database application developers have
the ability to harness the power of Java.
Benefits
•
•
•
•
In view of Java's popularity, it is likely that the
members of a development team are more
proficient in Java than PL/SQL.
Java stored procedures enables the database
programmers to code in their preferred Java
language.
Experienced PL/SQL developers, can take
advantage of the Java language to extend the
functionality of database applications.
Thus PL/SQL and Java can coexist in an
application
CSE5200 - Java Stored Procedure
Java Stored Procedure - Step by Step
1. Writing the Java Class
• Develop the Java classes using Oracle JDeveloper.
• Compile, and even unit test your Java code.
import java.sql.*;
import oracle.jdbc.*;
public class EmpManager
{
//Add an employee to the database.
public static void addEmp(int emp_id, String emp_f_name, String emp_l_name,float emp_salary,
int dept_id)
{
System.out.println("Creating new employee...");
try
{
Connection conn = DriverManager.getConnection("jdbc:default:connection:");
String sql = "INSERT INTO emp VALUES(" + emp_id + "," + "'" + emp_f_name +
"'," + "'" + emp_l_name + "'," + emp_salary + "," + dept_id + ")";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.executeUpdate();
pstmt.close();
}
catch(SQLException e)
{
System.err.println("ERROR! Adding Employee: " + e.getMessage());
}
}
CSE5200 - Java Stored Procedure
}
Java Stored Procedure - Step by Step
2. Loading the Java Class
• Load the Java Class into the Oracle Database using JDeveloper
• Use the LoadJava and Java Stored Procedure option in the deployment
profile.
CSE5200 - Java Stored Procedure
Java Stored Procedure - Step by Step
3. Publishing the Java Stored Procedure
• Any class that will be directly called from SQL or PL/SQL must be
published
• A Java class is published by creating and compiling a call specification for
it.
• The call specification, often referred to as a call spec or even a PL/SQL
wrapper, maps a Java method's parameters and return type to Oracle
SQL types. These operations are performed by JDeveloper
CSE5200 - Java Stored Procedure
Java Stored Procedure - Step by Step
4. Calling the Procedures
• We have developed, loaded and published the Java class as a Java
Stored Procedure. Now we test the same by executing the procedure in
SQL * PLUS
• The DBMS_JAVA package, an Oracle-supplied package with utilities for
managing server-side Java has a method for redirecting output to
SQL*Plus
SQL> SET SERVEROUTPUT ON
SQL> CALL dbms_java.set_output(2000);
Now, Java output will displayed upon execution.
SQL> EXECUTE add_emp(1,'Joe',
'Smith',40000.00,1); Creating new employee...
PL/SQL procedure successfully completed.
CSE5200 - Java Stored Procedure