Thursday, July 02, 2009

ORAData Implementation--ORADataExample.java

ORAData Implementation--ORADataExample.java

This section contains code that illustrates how to define and use a custom Java type corresponding to a given SQL object type, using a ORAData implementation.

SQL Object Definition

Following is the SQL definition of an EMPLOYEE object. The object has two attributes: a VARCHAR2 attribute EMPNAME (employee name) and an INTEGER attribute EMPNO (employee number).

CREATE TYPE employee AS OBJECT (  empname VARCHAR2(50),  empno INTEGER ); 

Custom Object Class--CustomDatum Implementation

The following code defines the custom Java class Employee (defined in Employee.java) to correspond to the SQL type EMPLOYEE. Notice that the definition of Employee contains accessor methods for a string empname (employee name) and an integer empno (employee number). Also notice that the Java definition of the Employee custom Java class implements the ORAData and ORADataFactory interfaces. A custom Java class that implements ORAData has a static getFactory() method that returns a ORADataFactory object. The JDBC driver uses the ORADataFactory object's create() method to return a CustomDatum instance.

Note that instead of writing the custom Java class yourself, you can use the JPublisher utility to generate class definitions that implement the ORAData and ORADataFactory interfaces. In fact, the Employee.java code shown here was generated by JPublisher.

import java.sql.SQLException; 
import oracle.jdbc.OracleConnection; 
import oracle.jdbc.OracleTypes; 
import oracle.sql.CustomDatum; 
import oracle.sql.CustomDatumFactory; 
import oracle.sql.Datum; 
import oracle.sql.STRUCT; 
import oracle.jpub.runtime.MutableStruct;  
public class Employee implements CustomDatum, CustomDatumFactory {  
public static final String _SQL_NAME = "SCOTT.EMPLOYEE";  
public static final int _SQL_TYPECODE = OracleTypes.STRUCT;   
MutableStruct _struct;   
static int[] _sqlType =  {  12, 4  };   
static CustomDatumFactory[] _factory = new CustomDatumFactory[2];   
static final Employee _EmployeeFactory = new Employee();  
public static CustomDatumFactory getFactory()  {  
return _EmployeeFactory;  
}   
/* constructor */  
public Employee()  {  
_struct = new MutableStruct(new Object[2], _sqlType, _factory);  }   
/* CustomDatum interface */  
public Datum toDatum(OracleConnection c) throws SQLException  {  
return _struct.toDatum(c, _SQL_NAME);  
}   
/* CustomDatumFactory interface */  
public CustomDatum create(Datum d, int sqlType) throws SQLException  {  
if (d == null) return null;  
Employee o = new Employee();  
o._struct = new MutableStruct((STRUCT) d, _sqlType, _factory);  
return o;  
}   
/* accessor methods */  
public String getEmpname() throws SQLException  { 
return (String) _struct.getAttribute(0); 
}   
public void setEmpname(String empname) throws SQLException  { 
_struct.setAttribute(0, empname); 
}   
public Integer getEmpno() throws SQLException  { 
return (Integer) _struct.getAttribute(1); 
}   
public void setEmpno(Integer empno) throws SQLException  { 
_struct.setAttribute(1, empno); } 
}  

Sample Application Using CustomDatum Custom Object Class

This sample program shows how you can use the Employee class generated by JPublisher. The sample code creates a new Employee object, fills it with data, then inserts it into the database. The sample code then retrieves the Employee data from the database.

For information about using CustomDatum implementations to access and manipulate SQL object data, see "Reading and Writing Data with a ORAData Implementation".

import java.sql.*; 
import oracle.jdbc.*; 
import oracle.sql.*; 
import java.math.BigDecimal;   
public class CustomDatumExample {   
public static void main(String args []) throws Exception  {   // Connect  
DriverManager.registerDriver(new oracle.jdbc.OracleDriver ());  
OracleConnection conn = (OracleConnection)  DriverManager.getConnection("jdbc:oracle:oci8:@",  "scott", "tiger");   
// Create a Statement  
Statement stmt = conn.createStatement ();  
try  {  
stmt.execute ("drop table EMPLOYEE_TABLE");  
stmt.execute ("drop type EMPLOYEE");  
}  catch (SQLException e)  {  
// An error is raised if the table/type does not exist. Just ignore it.  
}   
// Create and populate tables  
stmt.execute ("CREATE TYPE EMPLOYEE AS OBJECT  (EmpName VARCHAR2(50),EmpNo INTEGER)");  
stmt.execute ("CREATE TABLE EMPLOYEE_TABLE (ATTR1 EMPLOYEE)");  
stmt.execute ("INSERT INTO EMPLOYEE_TABLE VALUES  (EMPLOYEE('Susan Smith', 123))");  
stmt.close();   
// Create a CustomDatum object  
Employee e = new Employee("George Jones", new BigDecimal("456"));   
// Insert the CustomDatum object  
PreparedStatement pstmt  = conn.prepareStatement ("insert into employee_table values (?)");   
pstmt.setObject(1, e, OracleTypes.STRUCT);  
pstmt.executeQuery();  
System.out.println("insert done");  
pstmt.close();   
// Select now  
Statement s = conn.createStatement();  
OracleResultSet rs = (OracleResultSet)  
s.executeQuery("select * from employee_table");   
while(rs.next())  {  
Employee ee = (Employee) rs.getCustomDatum(1, Employee.getFactory());  
System.out.println("EmpName: " + ee.empName + " EmpNo: " + ee.empNo);  }  
rs.close();  
s.close();   
if (conn != null)  {  conn.close();  }  
} 
} 

No comments: