Home arrow Oracle arrow Page 2 - Extending PL/SQL with Java Libraries, concluded

Building Internal Server Java Objects - Oracle

If you are looking for a way to extend stored programs with Java, look no further. This article, the second of two parts, is excerpted from chapter five of Expert Oracle PL/SQL, written by Ron Hardman and Michael McLaughlin (McGraw-Hill/Osborne, 2005; ISBN: 0072261943).

TABLE OF CONTENTS:
  1. Extending PL/SQL with Java Libraries, concluded
  2. Building Internal Server Java Objects
  3. More on Building Internal Server Java Objects
  4. Troubleshooting Java Class Library Build, Load, Drop, and Use
  5. Mapping Oracle Types
By: McGraw-Hill/Osborne
Rating: starstarstarstarstar / 12
January 19, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The Java programming language is Object-Oriented (OO). In the previous examples, Java stored objects were used as static functions. The potential to use Java to accomplish significant OO computing models lies in the Oracle object features introduced in Oracle 9i Release 2. Beginning with that release, you can construct instances of object types and use them as objects. After you develop an understanding of implementing stored Java objects in this section, you can see how PL/SQL objects work in Chapter 6.

Why Would I Use This?

We use internal server Java objects for the same reasons you use PL/SQL objects. Using Java as instantiable and threaded objects is where the value of using stored Java objects adds value once you understand how to use the SQLData interface. Java internal server objects have the ability to indirectly instantiate objects. The internal server Java objects are awkward to use for Java developers because they use external or indirect interfaces to effect communication.

This section illustrates how the SQLData interface is used and explains the concepts supporting it. We believe Java developers will find this feature useful but tricky to use at first. This section should help you jump-start your use of the feature.

Server-side stored Java programs support full runtime object behaviors starting with Oracle 9i, as noted earlier. This means you can now design, develop, and implement natural Java applications beneath PL/SQL object type wrappers. These Java classes can have instance methods, which mean nonstatic methods. You may also use static methods for libraries.

The balance of the differences covered earlier in the chapter still applies. You build Java object libraries by writing the Java class file and SQL object type definition. Object type bodies are not defined when the object type implementation is written in a stored Java object.

The substantial difference between external Java and server internal Java objects is the way you construct an instance of the class. You do not directly instantiate the class file and cannot use overriding constructors in the Java class file. The SQLData interface is the key to instantiating stored Java objects. It enables instantiating the Java class by passing back and forth the parameter values. This enables a class to return a reference to a copy or instance of the class.

TIP

Thereís no way to instantiate directly a default constructor when using a stored Java object class. You also cannot use overriding constructors. The SQLData interface allows you to pass values to an instantiated class based on known class scope instance variables. Instance variables are not static variables. These limits are imposed by the implementation of the SQLData interface.

Implementing the SQLData interface is done by providing a variable definition and three concrete methods in your Java class file. The following are the components:

  • A String data type named sql_type.
  • A getSQLTypeName() method that returns a String data type.
  • A readSQL() method that takes two formal parameters and returns a void. One formal parameter is a SQLInput that contains a stream. The other is a string that contains a data type name.
  • A writeSQL() method that takes one formal parameter, which is a SQLOutput that contains a stream.

Details on implementing runtime Java classes will be illustrated in the following examples. The HelloWorld4 Java class file is designed to work as an instantiable Java stored object type body. The source code for the class is:

-- Available online as part of HelloWorld4.java file.
// Oracle class imports.
import java.sql.*;
import java.io.*;
import oracle.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.oracore.*;
// Class definition.
public class HelloWorld4 implements SQLData {
 
// Define and initialize a private class name variable.
  private String className = new String("HelloWorld4.class");
 
// Define a formal parameter signature variable.
  private String instanceName;
  // Define a private schema qualified name value.
  private String qualifiedName;
 
// Define a class instance variable to support SQLData Interface.
  private String sql_type;
 
// --------------------------------------/
 
// Define default constructor.
  public HelloWorld4()
  {
   
// Define local String variables.
    String user = new String();
   
// Use a try-catch block because of SQL statement.
    try
    {
     
// Call a method of the inner class.
      user = getUserName();
   
}
    catch (Exception e) {}
   
// Set the class instance variable.
    qualifiedName = user + "." + className;
 
} // End of default constructor.
  // --------------------------------------/
 
// Define a method to return a qualified name.
  public String getQualifiedName() throws SQLException
  { 
   
// Define and initialize a return variable.
    return this.qualifiedName + "." + instanceName;
 
} // End of getQualifiedName() method.
 
// --------------------------------------/
 
// Define a method to return the database object name.
  public String getSQLTypeName() throws SQLException
  {
    // Returns the UDT map value or database object name.
    return sql_type;
 
} // End of getSQLTypeName() method.
  // --------------------------------------/
 
// Define getUserName() method to query the instance.
  public String getUserName() throws SQLException
  {
   
// Define and initialize a local return variable.
    String userName = new String();
   
// Define and initialize a query statement.
    String getDatabaseSQL = "SELECT user FROM dual";
   
// Define a connection for Oracle.
    Connection conn = new OracleDriver().defaultConnection();
   
// Define and initialize a prepared statement.
    PreparedStatement ps = conn.prepareStatement(getDatabaseSQL);
   
// Execute a query.
    ResultSet rs = ps.executeQuery();
   
// Use a while-loop even though only one row is returned.
   
while (rs.next())
   
{
    // Assign the cursor return.
      
userName = rs.getString(1);
    }
   
// Return the user name.
   
return userName;
 
} // End of getUserName() method.
 
// --------------------------------------/
 
// Implements readSQL() method from the SQLData interface.
  public void readSQL(SQLInput stream, String typeName) throws SQLException
  {
    // Define sql_type to read input and signal overloading signatures.
   
sql_type = typeName;
   
// Pass values into the class.
    instanceName = stream.readString();
  } // End of readSQL() method.
  // -------------------------------------/   // Implements readSQL() method from the SQLData interface.
  public void writeSQL(SQLOutput stream) throws SQLException
  {
   
// You pass a value back by using a stream function.
   
// stream.writeString('variable_name');
 
} // End of readSQL() method.
} // End of HelloWorld4 class.

The Java class file does the following:

  • It defines five key import statements. Make sure you use these as your starting point for stored Java object classes.
  • It defines four String data type class instance variables. The className variable is defined and initialized but the instanceName and qualifiedName variables are only defined. The sql_type variable is also only defined, but thatís how it should be done when using the SQLData interface. Also note that all four are private access level variables.
  • It defines a single class with a no argument constructor. This is typically called the default constructor. Youíll see why itís important to note when you examine how to instantiate this object in PL/SQL. Within the default constructor, the qualifiedName variable is initialized. This is the only place where the variable can be assigned a value. Itís implemented this way to demonstrate that the default constructor is executed.
  • It defines five methods. Two are unique to the test class and three are required to implement the SQLData interface. They do the following:

    • The getQualifiedName() method returns the this.qualifiedName variable, which is an instance variable for the class. If you were to attempt to reference a class-level variable in a Java stored class supporting a PL/SQL function and procedure wrappers, it would fail. The loadjava would raise an exception to prevent putting it into the database instance.
    • The getSQLTypeName() method implements a method from the SQLData interface and assigns a user defined type to a local variable. This enables the class to be managed within the scope of the Oracle JVM.
    • The getUserName() method queries the database for the current user of the stored Java class.
    • The readSQL() method manages the incoming stream to the Java stored object class. The example uses one direct mapping to illustrate the differences between two instantiated classes during testing.
    • The writeSQL() method manages the outgoing stream from the Java stored object class. The example requires no direct mapping of the outgoing stream but the syntax is there for your convenience. This is done because all variables are defined with private access. Private access variables are encapsulated by design and are available only through published methods.



 
 
>>> More Oracle Articles          >>> More By McGraw-Hill/Osborne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

ORACLE ARTICLES

- Oracle Java Security Woes Continue
- Oracle's New IaaS Cloud Option: There's a Ca...
- Oracle Acquires Eloqua to Boost Cloud Presen...
- Choosing Innovation: Oracle Survey Insights
- Oracle Fixes Privilege Escalation Bug
- Oracle`s Communications Service Availability...
- Oracle Releases Exalytics, Taleo Plans
- Oracle Releases Communications Network Integ...
- Oracle Releases Communications Data Model 11...
- Oracle Releases PeopleSoft PeopleTools 8.52
- Oracle Integrates Cloudera Apache Distro, My...
- Oracle Releases MySQL 5.5.18
- Oracle Announces NoSQL Database Availability
- Sorting Database Columns With the SELECT Sta...
- Retrieving Table Data with the LIKE Operator

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: