Java
  Home arrow Java arrow Page 5 - Java Stored Procedure in Oracle, Database ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Java Stored Procedure in Oracle, Database Interaction
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 27
    2005-11-01

    Table of Contents:
  • Java Stored Procedure in Oracle, Database Interaction
  • A Simple Java Class
  • Getting Java Classes into Oracle Database
  • How to make Classes available to Oracle
  • How to execute the Java Stored Procedures

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Java Stored Procedure in Oracle, Database Interaction - How to execute the Java Stored Procedures


    (Page 5 of 5 )

    We have developed, loaded, and published our Java classes. The final step is to execute them.  The following statements would make you see the Java based output.

    SQL> SET SERVEROUTPUT ON
    SQL> CALL dbms_java.set_output(2000);
    SQL> EXECUTE add_emp(1,'Jag',40000.00,1);
    Creating new employee...

    PL/SQL procedure successfully completed.

    The first two statements are necessary one time (after the first login), because it allows the redirection of tracing.

    As you can see, from the caller's perspective, there is no difference between calls made to Java stored procedures and calls to a PL/SQL procedure or function.

    VARIABLE x NUMBER;
    CALL getEmpCountByDept(1) INTO :x;
    Getting Number of Employees for Dept...

    Call completed.

    PRINT x

             X
    ----------
             1

    The SQLException class has the getErrorCode() and getErrorMessage() methods to help report errors. Any uncaught exception in a Java Stored Procedure results in an 'ORA-29532 Java call terminated by uncaught Java exception' for the caller. How you choose to handle errors will vary by application. The addEmp method simply catches and displays the error. We receive an error message when we attempt to add an emp record with an invalid dept_id.

    SQL> execute addEmp(2,'aaa', 45000.00,2);
    Creating new employee...
    ERROR! Adding Employee : ORA-02291: integrity constraint
                             (OPS$AK4353.FK_DEPT_ID) violated -
                             parent key not found

    Because there is a need to call Java from PL/SQL, it is reasonable to assume that we will also need a way to call PL/SQL from Java code. This is very easy to achieve by using a CallableStatement object in our Java methods.

    CallableStatement cstmt = conn.prepareCall("{your procedure}");

    Thus, it is possible to create a seamless environment of PL/SQL procedures calling Java and vice versa.

    Summary

    Even though I examined only a stored procedure in the above scenario, you can even embed those Java stored procedures in PL/SQL Packages. Don't consider only Java stored procedures; there also exists the concept of .NET based stored procedures (primarily called .NET CLR stored procedures).  .NET based stored procedures are supported by both SQL Server 2005 and Oracle 10g Version 10.2.  Remember, it is possible to create a seamless environment of PL/SQL procedures calling Java and vice versa.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · get in touch with Java stored procedure here and don't forget to give me the...
       · Nice basic article, it would be great to have an example of using an external object...
       · sure...I will plan one more...and thanks for the feedback
       · Hi, Iam doing a comparison of Java and Oracle stored procedures and i find the...
       · Hello,I am please to read this article. Indeed this article provides a step by...
       · Dear Jagadish,Thanks for such a nice article. It is very simple to understand and...
       · Hi Jagdeesh,The article is realy very helpful and knowledgable, thank you.I...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT