1、Jpublisher概述 java
Oracle JPublisher是一個用於在Java和SQL之間轉換和交換結構化對象數據的工具,它還能夠訪問其餘的數據庫資源,如PL/SQL包和Java存儲 過程。它生成Java類來表示數據庫實體,如SQL對象和操做、PL/SQL包和過程以及服務器端Java類。你能夠經過JDBC在你的Java客戶端、 servlet、JavaServer Pages (jsp)、Enterprise JavaBeans (EJB)和Web服務中使用這些生成的類。
JPublisher 能夠生成表示如下數據庫實體的類:
用戶定義的SQL對象類型。對於每一個對象類型,JPublisher爲表示該對象的類生成一個type.java文件。它提供訪問程序方法,用以獲取和設 置該對象類型的每一個屬性。還有,若是你的對象類型包含了做爲存儲過程而實現的方法,則JPublisher將生成封裝器方法在對象實例上調用這些方法。
對象引用類型(REF 類型)。 對於一個SQL對象引用類型,JPublisher生成一個typeRef.java文件來創建該Java對象類型的引用模型。它包括經過一個對象引用來訪問實際的對象值的方法。
用戶定義的SQL集合類型。對於每一個集合類型,JPublisher爲表示該集合的類生成一個type.java文件。對於嵌套的數據表,生成的類具備做爲整個數組來獲取和設置嵌套的數據表的一些方法,以及用於獲取和設置該數據表中各個元素的一些方法。
用戶定義的OPAQUE類型。每當JPublisher遇到一個沒有提供其相應類型的SQL OPAQUE類型時,它都會生成一個type.java文件來發佈一個Java封裝器類。OPAQUE的有效負荷以字節數組(byte array)來表示,能夠從生成的封裝器的子類中進行處理。
PL/SQL BOOLEAN PL/SQL BOOLEAN被映射到Java Boolean類型。這要求有SYS.SQLJUTL包,該包是由數據庫(JPublisher生成的代碼要在其中運行)中的sqljutil.sql腳 原本安裝的。這個腳本默認安裝在Oracle Database 10g中。
PL/SQL RECORD和TABLE類型。對於RECORD和TABLE類型,JPublisher生成相應的SQL對象(對於RECORD類型)或SQL VARRAY類型(對於TABLE類型),以及用於在PL/SQL和SQL類型之間進行映射的轉換函數。
PL/SQL INDEXED-BY數據表。若是你使用Oracle Database 10g 的JDBC Oracle Call Interface (OCI)驅動程序而且須要發佈已有索引的標量數據表,那麼你能夠直接在Java和這些類型(以Java數組表示)之間進行映射。 sql
2、建立PL/SQL類型與生成Java 對象 數據庫
-- 建立Person對象
CREATE OR REPLACE TYPE Person AS OBJECT
(
p_code VARCHAR2 ( 32 ),
p_name VARCHAR2 ( 16 ),
p_age NUMBER ,
p_birthday DATE
);
-- 建立對象表personTable
CREATE TABLE personTable OF person;
-- 建立表數組
CREATE OR REPLACE TYPE person_table_type IS TABLE OF Person;使用Jpublisher 生成java對象,運行createOraObj.bat 數組
createOraObj . bat
SET ORACLE_HOME = C: oracle ora92
SET JDBC_LIB = %ORACLE_HOME% jdbc lib
SET SQLJ_EXE_HOME = %ORACLE_HOME% bin
SET SQLJ_LIB = %ORACLE_HOME% sqlj lib
SET PATH = % PATH % ; %SQLJ_EXE_HOME%
SET CLASSPATH = %SQLJ_LIB% translator . jar ; %SQLJ_LIB% translator . zip ; %SQLJ_LIB% runtime12ee . jar ; %SQLJ_LIB% runtime12ee . zip ; %JDBC_LIB% classes12 . zip ; %JDBC_LIB% classes12 . jar ; %JDBC_LIB% nls_charset12 . jar ; %JDBC_LIB% nls_charset12 . zip ; %JDBC_LIB% ojdbc14 . jar ; %CLASSPATH%
jpub -user = soft1 / soft1 -sql = PERSON_TABLE_TYPE :com . baker . object . PersonTableType -url = jdbc :oracle:thin :@ 192.168 . 0.128 : 1521 :pbcuser運行以上bat後將在當前目錄下生成三個java 源文件:Person.java、PersonRef.java、PersonTableType.java 服務器
3、以PersonTableType做爲輸入參數的形式調用存儲過程 oracle
-- 建立存儲過程
CREATE OR REPLACE PROCEDURE testInPersons(persons IN person_table_type) IS
BEGIN
FORALL i IN persons.FIRST .. persons. COUNT
INSERT INTO persontable VALUES (persons(i));
COMMIT ;
END testinpersons;
// JDBC調用示例
OracleCallableStatement cstmt = (OracleCallableStatement) con
.prepareCall( " {call testInPersons(?)} " );
Person p1 = new Person();
p1.setPCode( " 個人身份證11 " );
p1.setPName( " 侯廷文11 " );
p1.setPAge( new BigDecimal( 44 ));
p1.setPBirthday( new Timestamp( new java.util.Date().getTime()));
Person p2 = new Person();
p2.setPCode( " 個人身份證22 " );
p2.setPName( " 侯廷文22 " );
p2.setPAge( new BigDecimal( 45 ));
p2.setPBirthday( new Timestamp( new java.util.Date().getTime()));
PersonTableType persons = new PersonTableType();
persons.setArray( new Person[]{p1,p2});
cstmt.setORAData( 1 , persons);
cstmt.execute();
4、以PersonTableType做爲輸出參數的形式調用存儲過程 jsp
-- 建立存儲過程
CREATE OR REPLACE PROCEDURE testTableOfObject(v_table OUT person_table_type) IS
BEGIN
v_table : = person_table_type();
FOR i IN 1 .. 5 LOOP
v_table.EXTEND;
v_table(i) : = NEW person( ' 45212319880810435 ' || i,
' 侯廷文 ' || i,
20 + i,
to_date( ' 1985-08-1 ' || i, ' YYYY-MM-DD ' ));
END LOOP;
END testtableofobject;
// JDBC調用示例
OracleCallableStatement cstmt = (OracleCallableStatement) con
.prepareCall( " {call testtableofobject(?)} " );
cstmt.registerOutParameter( 1 , OracleTypes.ARRAY, " person_table_type "
.toUpperCase());
cstmt.execute();
PersonTableType personCollection = (PersonTableType) cstmt.getORAData(
1 , PersonTableType.getORADataFactory());
Person[] persons = personCollection.getArray();
for ( int i = 0 ; i < persons.length; i ++ ) {
System.out.print(i + " : " );
System.out.print(persons[i].getPCode() + " " );
System.out.print(persons[i].getPName() + " " );
System.out.print(persons[i].getPAge() + " " );
System.out.print(persons[i].getPBirthday() + " " );
System.out.println();
}
輸出結果:
0 : 452123198808104351 侯廷文1 21 1985 - 08 - 11 00 : 00 : 00.0
1 : 452123198808104352 侯廷文2 22 1985 - 08 - 12 00 : 00 : 00.0
2 : 452123198808104353 侯廷文3 23 1985 - 08 - 13 00 : 00 : 00.0
3 : 452123198808104354 侯廷文4 24 1985 - 08 - 14 00 : 00 : 00.0
4 : 452123198808104355 侯廷文5 25 1985 - 08 - 15 00 : 00 : 00.0
5、附上生成的Java對象源文件 函數
// com.baker.object.Person.java
package com.baker.object;
import java.sql.SQLException;
import java.sql.Connection;
import oracle.jdbc.OracleTypes;
import oracle.sql.ORAData;
import oracle.sql.ORADataFactory;
import oracle.sql.Datum;
import oracle.sql.STRUCT;
import oracle.jpub.runtime.MutableStruct;
public class Person implements ORAData, ORADataFactory {
public static final String _SQL_NAME = "SOFT1.PERSON";
public static final int _SQL_TYPECODE = OracleTypes.STRUCT;
protected MutableStruct _struct;
private static int[] _sqlType = { 12, 12, 2, 91 };
private static ORADataFactory[] _factory = new ORADataFactory[4];
protected static final Person _PersonFactory = new Person(false);
public static ORADataFactory getORADataFactory() {
return _PersonFactory;
}
/* constructor */
protected Person(boolean init) {
if (init)
_struct = new MutableStruct(new Object[4], _sqlType, _factory);
}
public Person() {
this(true);
}
public Person(String pCode, String pName, java.math.BigDecimal pAge,
java.sql.Timestamp pBirthday) throws SQLException {
this(true);
setPCode(pCode);
setPName(pName);
setPAge(pAge);
setPBirthday(pBirthday);
}
/* ORAData interface */
public Datum toDatum(Connection c) throws SQLException {
return _struct.toDatum(c, _SQL_NAME);
}
/* ORADataFactory interface */
public ORAData create(Datum d, int sqlType) throws SQLException {
return create(null, d, sqlType);
}
protected ORAData create(Person o, Datum d, int sqlType)
throws SQLException {
if (d == null)
return null;
if (o == null)
o = new Person(false);
o._struct = new MutableStruct((STRUCT) d, _sqlType, _factory);
return o;
}
/* accessor methods */
public String getPCode() throws SQLException {
return (String) _struct.getAttribute(0);
}
public void setPCode(String pCode) throws SQLException {
_struct.setAttribute(0, pCode);
}
public String getPName() throws SQLException {
return (String) _struct.getAttribute(1);
}
public void setPName(String pName) throws SQLException {
_struct.setAttribute(1, pName);
}
public java.math.BigDecimal getPAge() throws SQLException {
return (java.math.BigDecimal) _struct.getAttribute(2);
}
public void setPAge(java.math.BigDecimal pAge) throws SQLException {
_struct.setAttribute(2, pAge);
}
public java.sql.Timestamp getPBirthday() throws SQLException {
return (java.sql.Timestamp) _struct.getAttribute(3);
}
public void setPBirthday(java.sql.Timestamp pBirthday) throws SQLException {
_struct.setAttribute(3, pBirthday);
}
}
// com.baker.object.PersonRef.java
package com.baker.object;
import java.sql.SQLException;
import java.sql.Connection;
import oracle.jdbc.OracleTypes;
import oracle.sql.ORAData;
import oracle.sql.ORADataFactory;
import oracle.sql.Datum;
import oracle.sql.REF;
import oracle.sql.STRUCT;
public class PersonRef implements ORAData, ORADataFactory {
public static final String _SQL_BASETYPE = "SOFT1.PERSON";
public static final int _SQL_TYPECODE = OracleTypes.REF;
REF _ref;
private static final PersonRef _PersonRefFactory = new PersonRef();
public static ORADataFactory getORADataFactory() {
return _PersonRefFactory;
}
/* constructor */
public PersonRef() {
}
/* ORAData interface */
public Datum toDatum(Connection c) throws SQLException {
return _ref;
}
/* ORADataFactory interface */
public ORAData create(Datum d, int sqlType) throws SQLException {
if (d == null)
return null;
PersonRef r = new PersonRef();
r._ref = (REF) d;
return r;
}
public static PersonRef cast(ORAData o) throws SQLException {
if (o == null)
return null;
try {
return (PersonRef) getORADataFactory().create(o.toDatum(null),
OracleTypes.REF);
} catch (Exception exn) {
throw new SQLException("Unable to convert "
+ o.getClass().getName() + " to PersonRef: "
+ exn.toString());
}
}
public Person getValue() throws SQLException {
return (Person) Person.getORADataFactory().create(_ref.getSTRUCT(),
OracleTypes.REF);
}
public void setValue(Person c) throws SQLException {
_ref.setValue((STRUCT) c.toDatum(_ref.getJavaSqlConnection()));
}
}
// com.baker.object.PersonTableType.java
package com.baker.object;
import java.sql.SQLException;
import java.sql.Connection;
import oracle.jdbc.OracleTypes;
import oracle.sql.ORAData;
import oracle.sql.ORADataFactory;
import oracle.sql.Datum;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
import oracle.jpub.runtime.MutableArray;
public class PersonTableType implements ORAData, ORADataFactory {
public static final String _SQL_NAME = "SOFT1.PERSON_TABLE_TYPE";
public static final int _SQL_TYPECODE = OracleTypes.ARRAY;
MutableArray _array;
private static final PersonTableType _PersonTableTypeFactory = new PersonTableType();
public static ORADataFactory getORADataFactory() {
return _PersonTableTypeFactory;
}
/* constructors */
public PersonTableType() {
this((Person[]) null);
}
public PersonTableType(Person[] a) {
_array = new MutableArray(2002, a, Person.getORADataFactory());
}
/* ORAData interface */
public Datum toDatum(Connection c) throws SQLException {
return _array.toDatum(c, _SQL_NAME);
}
/* ORADataFactory interface */
public ORAData create(Datum d, int sqlType) throws SQLException {
if (d == null)
return null;
PersonTableType a = new PersonTableType();
a._array = new MutableArray(2002, (ARRAY) d, Person.getORADataFactory());
return a;
}
public int length() throws SQLException {
return _array.length();
}
public int getBaseType() throws SQLException {
return _array.getBaseType();
}
public String getBaseTypeName() throws SQLException {
return _array.getBaseTypeName();
}
public ArrayDescriptor getDescriptor() throws SQLException {
return _array.getDescriptor();
}
/* array accessor methods */
public Person[] getArray() throws SQLException {
return (Person[]) _array.getObjectArray(new Person[_array.length()]);
}
public void setArray(Person[] a) throws SQLException {
_array.setObjectArray(a);
}
public Person[] getArray(long index, int count) throws SQLException {
return (Person[]) _array.getObjectArray(index, new Person[_array
.sliceLength(index, count)]);
}
public void setArray(Person[] a, long index) throws SQLException {
_array.setObjectArray(a, index);
}
public Person getElement(long index) throws SQLException {
return (Person) _array.getObjectElement(index);
}
public void setElement(Person a, long index) throws SQLException {
_array.setObjectElement(a, index);
}
}
本文轉自:http://blog.csdn.net/fk_baker/article/details/2452579 工具