Java
  Home arrow Java arrow Page 2 - 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 - A Simple Java Class


    (Page 2 of 5 )

    One should remember that Java stored procedures are still Java classes, but stored as Oracle schema objects. They will be made accessible to Oracle SQL and PL/SQL through call specifications. Call specifications, as we will see, are simply PL/SQL declarations that 'wrap' methods of Java stored in the database.  Call specifications, in other words, work as mediators (or interfaces).

    Another important issue to consider is that Java methods must be “public” and they must be “static” if they are to be used as stored procedures inside an Oracle database.

    JDeveloper is the product from Oracle, which seems to be a famous IDE for Java developers who need Oracle based Java stored procedures to develop and deploy very easily.  You can write, compile, and even unit test your Java code before moving it into the Oracle database.  But in this article, you work with a simple notepad by saving the following code as “Employee.java”.

    The following listing displays a simple Java class called “Employee”. For now, it contains a single method to “insert” an employee record into the database.

    import java.sql.*;
    import oracle.jdbc.*;

    public class Employee {

       //Add an employee to the database.
       public static void addEmp(int empno, String ename,
          float sal, int deptno) {

          System.out.println("Creating new employee...");

          try {
             Connection conn =
                DriverManager.getConnection("jdbc:default:connection:");

             String sql =
                "INSERT INTO emp " +
                "(empno,ename,sal,deptno) " +
                "VALUES(?,?,?,?)";
             PreparedStatement pstmt = conn.prepareStatement(sql);
             pstmt.setInt(1,empno);
             pstmt.setString(2,ename);
             pstmt.setFloat(3,sal);
             pstmt.setInt(4,deptno);
             pstmt.executeUpdate();
             pstmt.close();
             }
          catch(SQLException e) {
             System.err.println("ERROR! Adding Employee: "
               + e.getMessage());
             }
       }
    }

    There is nothing much new in the above program.  In this method, the database connection URL is "jdbc:default:connection:". When writing Java that will execute inside the Oracle database, you can take advantage of a special server-side JDBC driver. This driver uses the user's default connection (which is also called a context connection) and provides the fastest access to the database.

    More Java Articles
    More By Jagadish Chaterjee


       · 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 1 Hosted by Hostway
    Stay green...Green IT