Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
© 2009 IBM Corporation
Bilung Lee, Ph.D., [email protected] Silicon Valley Laboratory
Fully Loaded Database Driver with Hybrid Power
© 2009 IBM Corporation
2Outline• Driver Overview• XML Data Type• XML Data Type in Java Procedures• XML Schema• SQL/XML• XQuery
© 2009 IBM Corporation
3
Driver Overview
© 2009 IBM Corporation
4
COM.ibm.db2.jdbc.app.DB2DriverCOM.ibm.db2.jdbc.net.DB2Driver
Legacy driver (DB2 for LUW)
COM.ibm.db2os390.sqlj.jdbc.DB2SQLJDriverLegacy driver (DB2 for zOS)
com.ibm.db2.jcc.DB2DriverIBM Data Server Driver
Driver classDriver
IBM Data Server Driver• A single driver (also known as JCC driver) enables
Java access across DB2 product family
© 2009 IBM Corporation
5IBM Data Server Driver (continued)• Both JDBC and SQLJ access• Multiple platforms, including DB2 for Linux, Unix,
and Windows, DB2 for z/OS, DB2 for iSeries, Informix IDS, and Cloudscape
• Different connectivity paths (local and remote)• Type 2: Local via native attach on z/OS• Type 2: Local via shared memory on LUW• Type 4: Remote via DRDA protocol (pure Java)
• Direct Access from remote (no need for gateway)
© 2009 IBM Corporation
6
Type 2 Connectivityfor zOS
Common APIsfor JDBC and SQLJ
Type 4 Connectivity Type 2 Connectivityfor LUW
DRDAover
TCP/IP
Native AttachDRDA
overShared Memory
DB2 for zOS
Native Libraries
Common Architecture
DB2 for LUW
Native Libraries
© 2009 IBM Corporation
7Typical Usage
DB2ConnectGateway
JCC Type 4
Applications /Application Servers
JCC Type 2
Applications /Application Servers
JCCType 2
JavaStored Procs
InformixIDS
JCCType 2
JavaStored Procs
DB2 for LUW
DB2 for z/OS
© 2009 IBM Corporation
8Connectivity Selection• Considerations
• T4 connectivity is binary portable• T4 connectivity can run with a security manager• T4 connectivity equips with XA support for
distributed transactions• T2 connectivity performs better for local
connections• T2 connectivity is used by Java Stored
Procedures• Applications may switch between different
connectivity types
© 2009 IBM Corporation
9
6.0JDBC 4.0 and earlier
JCC 4db2jcc4.jar*
* Available starting from DB2 for LUW version 9.5
1.4JDBC 3.0 and earlier
JCC 3db2jcc.jar
Minimum level of Java
Level of JDBC support
Driver versionJAR file
Providing Two JAR Files
© 2009 IBM Corporation
10
XX**XQuery
* GA of version 9.5 is October 2007** Only XPath is supported
SQL/XML
XML schema registration
XML data type in Java procedures
XML data type
Feature
X
X
X
DB2 for zOS(March 2007 GA)
X
X
X
X
DB2 for LUW(July 2006 GA)*
XML Support on DB2 Version 9
© 2009 IBM Corporation
11
XML Data Type
© 2009 IBM Corporation
12
java.sql.Types.SQLXMLjava.sql.Types.OTHERMetadata type
Metadata type name
Java class
"XML""XML"
java.sql.SQLXMLcom.ibm.db2.jcc.DB2Xml
JCC 4 driver(db2jcc4.jar)
JCC 3 driver(db2jcc.jar)
Data Type• A built-in data type on DB2 since version 9• An XML data type can be accessed via string,
bytes, stream, DB2Xml, and SQLXML in JCC
© 2009 IBM Corporation
13
getAsciiStreamgetStringgetCharacterStream
getBytesgetBinaryStream
getObjectgetSQLXML*
* Only supported in JCC 4 driver and above
ASCII dataUCS-2 dataUTF-8 dataAbstract data
Data Retrieval• Methods on ResultSet to retrieve data from XML
columns
© 2009 IBM Corporation
14Data Retrieval (continued)• For exampleresultSet = statement.executeQuery("SELECT ORDER FROM ORDERS");
resultSet.next();byte[] xmlBytes = resultSet.getBytes(1);String xmlString = resultSet.getString(1);DB2Xml db2xml = (DB2Xml)resultSet.getObject(1);// following only applicable to the JCC 4 driver and aboveSQLXML sqlxml = resultSet.getSQLXML(1);
© 2009 IBM Corporation
15
getDB2XmlAsciiStream()getDB2AsciiStream()ASCII data
getDB2Bytes()getDB2BinaryStream()
UTF-8 data
getDB2XmlString()getDB2XmlCharacterStream()
getDB2String()getDB2CharacterStream()
UCS-2 data
getDB2XmlBytes(encoding)getDB2XmlBinaryStream(encoding)
Various encoding data
With declarationWithout declaration
Data Retrieval (continued)• Methods on DB2Xml to retrieve data
© 2009 IBM Corporation
16Data Retrieval (continued)• For exampleDB2Xml db2xml = (DB2Xml)resultSet.getObject(1);byte[] db2xmlBytes = db2xml.getDB2XmlBytes("EUC-JP");fileOutputStream = newjava.io.FileOutputStream("PurchaseOrder.EUC-JP.xml");
fileOutputStream.write(db2xmlBytes);fileOutputStream.flush();
© 2009 IBM Corporation
17
getSource(StreamSource.class)Stream source
getString()getCharacterStream()
getBinaryStream()With serialization
getSource(SAXSource.class)SAX sourcegetSource(StAXSource.class)StAX source
UCS-2 data
getSource(DOMSource.class)DOM source
UTF-8 dataWithout serialization
Data Retrieval (continued)• Methods on SQLXML to retrieve data
© 2009 IBM Corporation
18Data Retrieval (continued)• For exampleSQLXML sqlxml = resultSet.getSQLXML(1);SAXSource source = sqlxml.getSource(SAXSource.class);XMLReader reader = source.getXMLReader();ContentHandler myHandler = ...;reader.setContentHandler(myHandler);reader.parse(source.getInputSource());
© 2009 IBM Corporation
19
YesYesYesReadable
YesNoYesWritable
NoNoYesTraversable
NoNoYesUpdatable
Streaming, PullStreaming, PushTreeModelStAXSAXDOM
Data Processing• Parsing, validating, and transforming• Comparison
© 2009 IBM Corporation
20Data Processing (continued)• DOM (Document Object Model)
• Available since Java 1.4• A tree based API for random access to an XML
document• Most common XML API in use
• SAX (Simple API for XML)• Available since Java 1.4• A series of events generated as entities are
encountered within an XML document• Generally faster than DOM
© 2009 IBM Corporation
21Data Processing (continued)• StAX (Streaming API for XML)
• Available in Java 6• Pull the information out of an XML document as
needed• Also provide ability to write an XML document
• XSLT (XML Stylesheet Transform)• Available since Java 1.4• Transform XML formats from Source to Result• Transform XML among DOM/SAX/StAX/Stream
© 2009 IBM Corporation
22Data Processing (continued)• For exampleSQLXML sqlxml = resultSet.getSQLXML(1);DOMSource source = sqlxml.getSource(DOMSource.class);Result result = new StreamResult(new File("result.xml"));Source xslt = new StreamSource(new File("format.xslt"));Transformer transformer =TransformerFactory.newInstance().newTransformer(xslt);
transformer.transform(source, result);
© 2009 IBM Corporation
23
setStringsetAsciiStreamsetCharacterStreamsetObject(String)
setBytessetBinaryStreamsetSQLXML*setObject(byte[])setObject(DB2Xml)setObject(SQLXML)*
* Only supported in JCC 4 driver and above
Externally coded XML dataInternally coded XML data
Data Update• Methods on PreparedStatement to update data in
XML columns
© 2009 IBM Corporation
24Data Update (continued)• For examplestatement.execute("CREATE TABLE ORDERS (ORDER
XML)");preparedStatement = connection.prepareStatement("INSERT INTO ORDERS VALUES (?)");
fileInputStream = new FileInputStream("PurchaseOrder.xml");inputStreamReader = new InputStreamReader(fileInputStream,encoding);
preparedStatement.setCharacterStream(1, inputStreamReader,
numberOfCharactersInFile);preparedStatement.execute();
© 2009 IBM Corporation
25
setResult(StreamResult.class)Stream result
setString(String)setCharacterStream()
setBinaryStream()With serialization
setResult(SAXResult.class)SAX resultsetResult(StAXResult.class)StAX result
Externally coded
setResult(DOMResult.class)DOM result
Internally codedWithout serialization
Data Update (continued)• DB2Xml is immutable and cannot be changed• Methods on SQLXML to update data
© 2009 IBM Corporation
26Data Update (continued)• For exampleSQLXML sqlxml = connection.createSQLXML();SAXResult result = sqlxml.setResult(SAXResult.class);ContentHandler handler =result.getXMLReader().getContentHandler();
handler.startDocument();... // write elements and attributes to the resulthandler.endDocument();
© 2009 IBM Corporation
27
YESYEScreateSQLXML() of Connection
NONOsetXXX() of SQLXML
NONOgetXXX() of SQLXML
WritableReadableMethod called
SQLXML Data• An SQLXML may or may not be readable/writable
after a corresponding method is called
© 2009 IBM Corporation
28
XML Data Type inJava Procedures
© 2009 IBM Corporation
29Stored Procedures• On DB2 for zOS, stored procedure parameters
currently cannot have any XML data type• On DB2 for LUW
• For SQL procedures, XML parameters can have the XML type
• For external procedures, XML parameters can have the XML AS CLOB type
• Need to use a compatible data type in the invoking statement
© 2009 IBM Corporation
30Java Procedures• As mentioned, only on DB2 for LUW so far• JCC driver is required for necessary XML support• The corresponding type in Java is DB2Xml• Starting from version 9.5, JCC driver becomes the
default driver• In earlier versions, legacy type 2 driver is still used
by default• To make sure that JCC driver is used
db2set DB2_USE_DB2JCCT2_JROUTINE=YES
© 2009 IBM Corporation
31Stored Procedure Creation• In SQL, XML parameters are declared as XML AS
CLOB(n)statement.execute("CREATE PROCEDURE XMLPROC" +"(INOUT IOXML XML AS CLOB(2M)) " +"EXTERNAL NAME " +" 'XmlProcJar:XmlProcClass.xmlProcMethod' " +"LANGUAGE JAVA " +"PARAMETER STYLE JAVA");
© 2009 IBM Corporation
32Stored Procedure InvocationcallableStatement = c.prepareCall("CALL XMLPROC(?)");callableStatement.registerOutParameter(1, DB2Types.XML);callableStatement.execute();DB2Xml db2xml = (DB2Xml)callableStatement.getObject(1);
© 2009 IBM Corporation
33Stored Procedure Content• In Java, XML parameters are declared as DB2Xmlpublic class XmlProcClass {public static void xmlProcMethod(DB2Xml[] ioXml) {Connection connection =DriverManager.getConnection("jdbc:default:connection");
...resultSet = statement.executeQuery(...);ioXml[0] = (DB2Xml)resultSet.getObject(1);
}}
© 2009 IBM Corporation
34
XML Schema
© 2009 IBM Corporation
35Registration• Three stored procedures are defined on DB2
• SYSPROC.XSR_REGISTER• SYSPROC.XSR_ADDSCHEMADOC• SYSPROC.XSR_COMPLETE
• Convenient methods are provided in JCC• Register multiple XML schema documents for
an XML schema within one method call• One form of method takes InputStream objects
as XML schema documents• Another of method takes String objects as XML
schema documents
© 2009 IBM Corporation
36Registration (continued)• For examplejava.sql.DatabaseMetaData meta = connection.getMetaData();String sqlIdSchema[] = new String[1];if (meta.getDatabaseProductVersion().startsWith("DSN"))sqlIdSchema[0] = "SYSXSR";
else if (meta.getDatabaseProductVersion().startsWith("SQL"))sqlIdSchema[0] = "MYXSR";
String sqlIdName[] = new String[] {"ORDER"};
© 2009 IBM Corporation
37Registration (continued)String[] xmlSchemaLocations = new String[] {"order.xsd", "header.xsd", "cust.xsd", "prod.xsd“
};FileInputStream[] xmlSchemaDocuments =new FileInputStream[] {new FileInputStream("schema/order.xsd"),new FileInputStream("schema/header.xsd"),new FileInputStream("schema/cust.xsd"),new FileInputStream("schema/prod.xsd")
};
© 2009 IBM Corporation
38Registration (continued)int[] xmlSchemaDocumentsLengths = new int[] {(int)xmlSchemaDocuments[0].getChannel().size(),(int)xmlSchemaDocuments[1].getChannel().size(),(int)xmlSchemaDocuments[2].getChannel().size(),(int)xmlSchemaDocuments[3].getChannel().size()
};InputStream[] xmlSchemaDocumentsProperties = null;int[] xmlSchemaDocumentsPropertiesLengths = null;InputStream xmlSchemaProperties = null; int xmlSchemaPropertiesLength = 0; boolean isUsedForShredding = false;
© 2009 IBM Corporation
39Registration (continued)connection.registerDB2XmlSchema(sqlIdSchema,sqlIdName,xmlSchemaLocations,xmlSchemaDocuments,xmlSchemaDocumentsLengths,xmlSchemaDocumentsProperties,xmlSchemaDocumentsPropertiesLengths,xmlSchemaProperties,xmlSchemaPropertiesLength,isUsedForShredding);
© 2009 IBM Corporation
40Deregistration• A stored procedure SYSPROC.XSR_REMOVE is
defined on DB2 for zOS• A SQL statement DROP XSROBJECT is provided
instead on DB2 for LUW• A convenient method is provided in JCC
• Allow removal of an XML schema from DB2• Automatically choose between stored procedure
call or statement execution based on target DB2 platform
© 2009 IBM Corporation
41Deregistration (continued)• For examplejava.sql.DatabaseMetaData meta = connection.getMetaData();String sqlIdSchema = null;if (meta.getDatabaseProductVersion().startsWith("DSN"))sqlIdSchema = "SYSXSR";
else if (meta.getDatabaseProductVersion().startsWith("SQL"))sqlIdSchema = "MYXSR";
String sqlIdName = "ORDER";connection.deregisterDB2XmlObject(sqlIdSchema,
sqlIdName);
© 2009 IBM Corporation
42Update• A stored procedure SYSPROC.XSR_UPDATE is
defined on DB2 for LUW starting from version 9.5• A convenient method is provided in JCC• The content of an original XML schema can be
updated with the content of a new XML schema• Both the original and the new XML schemas must
already be registered• Only compatible updates are allowed, for example,
adding optional elements• Existing documents that are validated remain valid
© 2009 IBM Corporation
43Update (continued)• For exampleString originalSchema = "XSR1";String originalName = "NAME1";String newSchema = "XSR2";String newName = "NAME2";connection.updateDB2XmlSchema(originalSchema,
originalName,newSchema, newName, true);
© 2009 IBM Corporation
44Validation• Validation function
• On DB2 for zOS• Use DSN_XMLVALIDATE• The schema is SYSFUN
• On DB2 for LUW• Use XMLVALIDATE• The schema is SYSIBM• Cannot be specified as a qualified name
• For examplejava.sql.DatabaseMetaData meta = connection.getMetaData();
© 2009 IBM Corporation
45Validation (continued)if (meta.getDatabaseProductVersion().startsWith("DSN"))sql ="INSERT INTO ORDERS VALUES (XMLPARSE(DOCUMENT " +"DSN_XMLVALIDATE" +"(CAST(? AS BLOB(1G)), 'SYSXSR.ORDER')" +"))";
else if (meta.getDatabaseProductVersion().startsWith("SQL"))sql = "INSERT INTO ORDERS VALUES (" +"XMLVALIDATE" +"(? ACCORDING TO XMLSCHEMA ID MYXSR.ORDER)" +")";
© 2009 IBM Corporation
46Validation (continued)preparedStatement = connection.prepareStatement(sql);fileInputStream = new FileInputStream("PurchaseOrder.xml");preparedStatement.setBinaryStream(1, fileInputStream,(int)fileInputStream.getChannel().size());
preparedStatement.execute();
© 2009 IBM Corporation
47
SQL/XML
© 2009 IBM Corporation
48SQL/XML• Enable support for using XML in the context of an
SQL database system• XML extensions of SQL
• XML publishing functions• XML data type• Mapping rules
© 2009 IBM Corporation
49
XMLAGGAggregate function
XMLATTRIBUTESXMLCOMMENTXMLCONCATXMLDOCUMENTXMLELEMENTXMLFORESTXMLPIXMLTEXT
Scalar function
XMLNAMESPACESDeclaration functionFunctionsType
Publishing Functions
© 2009 IBM Corporation
50Publishing Functions (continued)• For exampleresultSet = statement.executeQuery("SELECT XMLELEMENT (" +" NAME \"employee\"," +" XMLNAMESPACES (DEFAULT
'http://www.ibm.com')," +" XMLATTRIBUTES (E.ID, E.FULLNAME AS
\"name\")," +" XMLFOREST ("+" E.ADDRESS AS \"address\", E.CITY AS \"city\"," +" E.STATE AS \"state\", E.ZIPCODE AS \"zip\"" +" )" +") FROM EMPLOYEE AS E");
© 2009 IBM Corporation
51Publishing Functions (continued)• Each row of the result set is an employee element<employee xmlns="http://www.ibm.com" ID="1001"
name="Jane Doe"><address>555 Bailey Ave</address><city>San Jose</city><state>California</state><zip>95141</zip></employee>
<employee xmlns="http://www.ibm.com" ID="1002" name="John Doe"><address>8200 Warden Ave</address><city>Markham</city><state>Ontario</state><zip>L6G1C7</zip></employee>
© 2009 IBM Corporation
52
xs:floatREAL
xs:longBIGINT
xs:decimalDECIMAL, NUMERIC
xs:intINTEGER
xs:doubleFLOAT, DOUBLE
xs:shortSMALLINT
xs:stringCHAR, VARCHAR
XML schema data typesSQL data types
Mapping Rules• Mapping between SQL data types and XML
schema data types
© 2009 IBM Corporation
53
XQuery
© 2009 IBM Corporation
54XQuery• The language for querying and modifying XML
data• Designed for XML data just as SQL designed for
relational data• Case-sensitive and all keywords in lower-case• Built on top of XPath• Compatible with several W3C standards
• XML• XML Schema• Namespaces• XSLT
© 2009 IBM Corporation
55Direct Invocation• XQuery is supported as first-class language• Not required to wrap XQuery inside SQL• Can exploit XQuery individually• Prefaced with the keyword XQUERY• Only available on DB2 for LUW so far
© 2009 IBM Corporation
56Hybrid Power• DB2 is equipped with both XQuery and SQL
language parsers• Both languages are compiled into the same
intermediate query representation• Can leverage “bilingual” queries• Deeper levels of nesting is also allowed• Choose the right model for the right task
© 2009 IBM Corporation
57Hybrid Power (continued)• Embed SQL in XQuery
• Only available on DB2 for LUW so far• Embed columns
db2-fn:xmlcolumn('SCHEMA.TABLE.COLUMN')• Embed query
db2-fn:sqlquery('select column from schema.table')• Embed XQuery in SQL
• Available on DB2 for both LUW and zOS• Use XMLQUERY SQL scalar function• Only XPath is supported on DB2 for zOS
© 2009 IBM Corporation
58SQL in XQueryresultSet = statement.executeQuery("XQUERY " +"declare default element namespace" +" \"http://jcc/account\";" +"<maillist>{" +"for $c in" +" db2-fn:sqlquery(\"SELECT OWNER FROM
ACCOUNT\")" +"where $c/owner[country=\"USA\"]" +"return" +" <customer>{$c//name} {$c//address}</customer>" +"}</maillist>"
);
© 2009 IBM Corporation
59XQuery in SQLresultSet = statement.executeQuery("SELECT XMLQUERY ('" +" declare default element namespace" +" \"http://jcc/account\";" +" $customer/owner[country=\"USA\"]" +"' PASSING OWNER AS \"customer\"" +") FROM ACCOUNT"
);
© 2009 IBM Corporation
60XQuery in SQL (Host Variables)preparedStatement = c.prepareStatement("SELECT XMLQUERY ('" +" declare default element namespace" +" \"http://jcc/account\";" +" $customer/owner[country=$nation] " +"' PASSING OWNER AS \"customer\"," +" CAST (? AS VARCHAR(20)) AS \"nation\"" +") FROM ACCOUNT"
);preparedStatement.setString(1, "USA");resultSet = preparedStatement.executeQuery();
© 2009 IBM Corporation
61Summary• Learn about IBM Data Server Driver for JDBC and
SQLJ• Learn the rich selection of interfaces for accessing
XML• Learn the XML schema registration• Learn the emerging SQL/XML feature• Learn the emerging XQuery feature• Learn to leverage the hybrid power
© 2009 IBM Corporation
62Questions/Comments