of 23 /23
JDBC interfeisa izmantošana relāciju- objektu datu bāzes datu glabāšanas struktūrām 1. Vāji tipizētās noklusētās Java klases (weakly typed default Java class) STRUCT izmantošana objektu transformācijai. Šis variants ir efektīvāks un vienkāršāks. 2. Stipri tipizētas pašveidotās Java klases (strongly typed custom Java classe) izveidošana Oracle objektiem. Kuassi Mensah. Oracle Database programming using Java and Web Services. Elsevier Digital Press publications, 2006. R. M. Menon. Expert Oracle JDBC Programming. Apress, 2005, 744. Lpp. Programmēšan as Java Relāciju-objektu datu bāzes

datubaze.files.wordpress.com · Web view2012/09/01 · JDBC interfeisa izmantošana relāciju-objektu datu bāzes datu glabāšanas struktūrām Relāciju-objektu datu bāzes objektiProgrammēšanas

Embed Size (px)

Text of datubaze.files.wordpress.com · Web view2012/09/01 · JDBC interfeisa izmantošana...

15

JDBC interfeisa izmantoana relciju-objektu datu bzes datu glabanas struktrm

(Relciju-objektu datu bzes objekti) (Programmanas Java objekti)

1. Vji tipizts noklusts Java klases (weakly typed default Java class) STRUCT izmantoana objektu transformcijai. is variants ir efektvks un vienkrks.

2. Stipri tipiztas paveidots Java klases (strongly typed custom Java classe) izveidoana Oracle objektiem.

Kuassi Mensah. Oracle Database programming using Java and Web Services. Elsevier Digital Press publications, 2006.

R. M. Menon. Expert Oracle JDBC Programming. Apress, 2005, 744. Lpp.

Oracle RODB objekta izgana izmantojot klasi Struct

create or replace type ADRESE_TIPS as object(

PILSETA varchar2(20),

IELAvarchar2(20),

IEL_NUMURS number);

create table DARBINIEKI(

D_NUMnumber,

UZVvarchar2(20),

D_ADRESEADRESE_TIPS);

insert into DARBINIEKI values (1, 'Koks', ADRESE_TIPS('Rga', 'Avotu', 21));

insert into DARBINIEKI values (2, 'Celms', ADRESE_TIPS('Ogre', 'Rgas', 7));

insert into DARBINIEKI values(3, 'Sakne', ADRESE_TIPS('Talsi', 'Ozolu', 5));

create or replace JAVA source named "Objekts" asimport java.sql.*;import java.io.*;import oracle.jdbc.*;public class Objekts{ public static void ObjIzvade() throws SQLException { try { Connection savienojums = DriverManager.getConnection("jdbc:default:connection:"); Statement komanda = savienojums.createStatement();

// Rezulttu objekta izveidoana, izpildms SELECT komandas

// ievade un ts izpilde.

ResultSet atbilde = komanda.executeQuery(

"select D_NUM, UZV, D_ADRESE from DARBINIEKI" +

" where D_NUM = 2");

atbilde.next();

// Pirmo divu kolonu vrtbu ieguve no rezulttu objekta.

int d_num = atbilde.getInt(1);

String uzv = atbilde.getString(2);

// Treaj kolon glabjas objekts. Tas tiek izgts k struktra.

STRUCT d_adreseStruct = (STRUCT) atbilde.getObject(3);

// No struktras STRUCT objekta atribti (lauki) tiek izgti Object tipa

// masv. Notiek Oracle SQL datu tipu transformcija Java datu tipos

// varchar2, char String

// number BigDecimal

// date Date

Object d_adreseAttr[] = d_adreseStruct.getAttributes();

// Tiek izgti atribti prveidoti vlamajos Java tipos.

String pilseta = (String) adreseAttr[0];

String iela = (String) adreseAttr[1];

int iel_num = ((BigDecimal) elementAttr[2]).intValue();

// Alternatvs variants metodes getAttributes() izmantoanai ir

// getOracleAttributes() metodes lietoana. aj gadjum atribtu tipi

// atbilst Oracle JDBC datu tipiem (number, char). Turpinjum ie

// tipi tiek transformti Java tipos. is ir racionlks variants.

// Object d_adreseAttr[] = d_adreseStruct.getOracleAttributes();

// String pilseta = ((CHAR) elementAttr[0]).toString();

// String iela = ((CHAR) elementAttr[1]).toString();

// Int iel_num = ((NUMBER) elementAttr[2]).intValue();

System.out.println("Darbinieka uzvrds = " + uzv + " Pilsta = " + pilseta); komanda.close(); } catch (SQLException e) {System.err.println(e.getMessage());} } }

create or replace procedure OBJEKTA_IZVADE() as language JAVA name 'Objekts.ObjIzvade( )';

beginOBJEKTA_IZVADE;end;

Java objektu transformcija RODB objektos

Unfortunately, the standard Java Struct doesn't provide a way to create a new Struct. We can obtain one only by selecting an object type from the database. Fortunately, Oracle's STRUCT has additional functionality, including a means of creating a new STRUCT that we can insert into the database. Because we're working closely with Oracle-specific object-relational features anyway, there's little benefit to be gained by choosing the standard option and lots of benefit to be gained from Oracle's extensions, so we'll use only the Oracle STRUCT in these examples.

When we retrieve an object type from the database into a STRUCT, the JDBC driver obtains the information it needs to create the STRUCT and the attributes array from the database, behind the scenes. If we want to create a STRUCT object from scratch and insert it into the database, we need to obtain the same information about the object type from the database ourselvesthis information comes in the form of an object called a StructDescriptor, which is unique to Oracle's implementation.

To use STRUCT and the StructDescriptor classes, we need to import the Oracle JDBC classes at the top of our source file.

import oracle.sql.*;

To get a StructDescriptor, we call a static method, createDescriptor(), with the name of the object type and a valid connection to the database. Here is how we obtain the descriptor for our ELEMENT_TYPE:

String elemTypeName = "ELEMENT_TYPE";

StructDescriptor elemDesc =

StructDescriptor.createDescriptor(elemTypeName, conn);

StructDescriptor has a number of interesting methods. One of these, getMetaData(), returns metadata about the object type in the form of a ResultSetMetaData object.

ResultSetMetaData elemMetaData = elemDesc.getMetaData();

We can obtain information about the object's attributes by calling ResultMetaData methods, such as getColumnCount(), getColumnName(), getColumnType(), and getColumnTypeName().

int columns = elemMetaData.getColumnCount();

System.out.println("Number of attributes: " + columns);

System.out.println("Attributes: ");

for(int i = 1; i