1 SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman, Oracle Corp. Richard Pledereder, Sybase Inc. Agenda ➨ Introduction ✔ SQLJ Part 0: Embedded SQL and Portability Profile ✔ SQLJ Part 1: Java Methods as SQL Procedures ✔ SQLJ Part 2: Java Classes as SQL Types
37
Embed
SQLJ: Java and Relational Databases SQLJ - The Technology – Part 0: SQLJ Embeded SQL • Mostly reviewed and implemented • Integrated with JDBC API • Oracle has placed Translator
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
SQLJ:Java and Relational Databases
Phil Shaw, Sybase Inc.
Brian Becker, Oracle Corp.
Johannes Klein, Tandem/Compaq
Mark Hapner, JavaSoft
Gray Clossman, Oracle Corp.
Richard Pledereder, Sybase Inc.
Agenda
➨ Introduction
✔ SQLJ Part 0: Embedded SQL and Portability Profile
✔ SQLJ Part 1: Java Methods as SQL Procedures
✔ SQLJ Part 2: Java Classes as SQL Types
2
Java and Databases
– JDBC
• Java Database Connectivity API
• Widely Implemented
– SQLJ™
• Java-Relational Database Technology
• Portability; Productivity; Java in the Database
• Leverages JDBC technology
– JavaBlend
• Object/Relational Mapping for Java
– The focus of this tutorial is on SQLJ
SQLJ - The Consortium
– Structure is informal• Participants include Oracle, IBM, Sybase, Tandem, JavaSoft,
Microsoft, Informix, XDB
• Open to other participants
– Meetings• Approximately every 3-4 weeks
• Hosted by one of the Bay Area resident vendors (Oracle,Sybase, Tandem, JavaSoft, Informix, etc.)
• Participants: Product Architects + SQL Standards people
3
SQLJ - The Technology
– Part 0: SQLJ Embeded SQL• Mostly reviewed and implemented
• Integrated with JDBC API
• Oracle has placed Translator source into public domain
– Part 1: SQLJ Stored Procedures and UDFs
• Using Java static methods as SQL stored procedures &functions
• Leverages JDBC API
– Part 2: SQLJ Data Types• Pure Java Classes as SQL ADTs
• Alternative to SQL3 Abstract Data Types
SQLJ - The Standard
– Goal of the SQLJ Consortium is to create workablestandards specifications in web time
– The Consortium is working with ANSI X3H2 on a fast-track process for adopting SQLJ as a standard
– The Consortium also works with The Open Group on aset of conformance tests
4
SQLJ - Implementation Status
– SQLJ Embedded SQL• Public-domain reference implementation available from Oracle
http://www.oracle.com/st/products/jdbc/sqlj
• Profile customizations available from Oracle, IBM, Sybase,Tandem, …
– SQLJ Procedures• Specifications mostly reviewed
• Implementations, e.g., Sybase Adaptive Server Anywhere 6.0,Oracle 8.1, IBM
– SQLJ Data Types• Specifications through first pass
• Implementations, e.g., Sybase Adaptive Server Anywhere 6.0
JDBC 2.0 - SQLJ Features
✔ Support for user-defined, object data types– Java Classes
• Persistent Java objects stored in the DBMS
– SQL3 types• BLOB, CLOB, array, reference
• Structured and distinct types
✔ New type codes– JAVA_OBJECT, STRUCT, BLOB, etc.
– Metadata for user-defined typesint[] types = {Types.JAVA_OBJECT};
int n;Statement stmt = conn.prepareStatement (“INSERT INTO emp VALUES (?)”);stmt.setInt(1,n);stmt.execute ();stmt.close();
8
Strongly typed cursors
#sql public iterator ByPos (String, int);ByPos positer;String name = null;int year = 0;
#sql positer = { SELECT name, year FROM people};while (true) { #sql { FETCH :positer INTO :name, :year}; if (positer.endFetch()) break; // process name, year}positer.close();
– Positional binding to columns
Strongly typed cursors (cont.)
#sql public iterator ByName (int year, String name);ByName namiter;String name = null;int year = 0;
#sql namiter = { SELECT name, year FROM people};while (namiter.next()) { name = namiter.name(); year = namiter.year(); // process name, year}namiter.close();
– Named binding to columns
9
Connection context
– SQLJ statements are associated with a connectioncontext
– Context type identifies exemplar schema, e.g. views,tables, privileges
#sql iterator ByNames (String name, int id, int region, BigDecimal sales);
ByNames r;
#sql r = {"SELECT name, id, region_of(state) as region, sales FROM emp
WHERE region_of(state) > :regionParm AND sales IS NOT NULL
ORDER BY sales DESC"};
if (r.next()) {
n1[0] = r.name(); id1[0] = r.id();
r1[0] = r.region(); s1[0] = r.sales();
}
else { n1[0] = "****"; return; }
if (r.next()) {
n2[0] = r.name(); id2[0] = r.id();
r2[0] = r.region(); s2[0] = r.sales();
}
else { n2[0] = "****"; return; }
}
}
OUT parameters (cont.)
– CREATE PROC for the bestTwoEmps method
• The bestTwoEmps method has eight out parameters and one inparameter:
create procedure best2(out n1 varchar(50), out id1 varchar(5), out r1 integer, out s1 decimal(6,2),out n2 varchar(50), out id2 varchar(5), out r2 integer, out s2 decimal(6,2),region integer)
reads sql dataexternal name ’Routines2.bestTwoEmps’language java parameter style java;
27
OUT parameters (cont.)– Invoking the best2 procedure
– When the Java VM encounteres an unloaded class name ine.g. the “admin_jar”, it will invoke the class loadersupplied by the SQL system, which will use the SQL pathto resolve the name.
31
Deployment descriptors
– A deployment descriptor is a text file containing thecreate and grant statements to do on install_jar, and thedrop and revoke statements to do on remove_jar.
– A deployment descriptor is contained in a jar file withthe classes it describes.
– The install_jar procedure will implicitly perform thecreate and grant statements indicated by the deploymentdescriptor.
– The remove_jar procedure will implicitly perform thedrop and revoke statements indicated by thedeployment descriptor.
Deployment descriptors (cont.)– Example deployment descriptor
• Assume that all of the above example classes Routines1,Routines2, and Routines3 are in a single jar.
• An example deployment descriptor for that jar would have thefollowing form:
SQLActions[ ] = {“BEGIN INSTALL// SQL create and grant statements// to be executed when the jar is installed.END INSTALL ”,
“BEGIN REMOVE //SQL drop and revoke statements// to be executed when the jar is removed.END REMOVE”}
32
Agenda
✔ Introduction
✔ SQLJ Part 0: Embedded SQL and Portability Profile
✔ SQLJ Part 1: Java Methods as SQL Procedures
➨ SQLJ Part 2: Java Classes as SQL Types
SQLJ Part 2:Java classes as SQL types
– Use Java classes as SQL data types for:
• Columns of SQL tables and views.
• Parameters of SQL routines.
– Especially SQL routines defined on Java methods (SQLJPart 1).
– Advantage to SQL:
• A type extension mechanism.
• Either an alternative or supplement to SQL3 ADTs.
– Advantage to Java:
• Direct support for Java objects in SQL databases.
• No need to map Java objects to SQL scalar or BLOB types.
33
Examples
– Example class: Address
public class Address implements java.io.Serializable {
public String street;
public String zip;
public static int recommended_width = 25;
// A default constructor
public Address ( ) {
street = "Unknown";
zip = "None";
}
// A constructor with parameters
public Address (String S, String Z) {
street = S;
zip = Z;
}
// A method to return a string representation of the full address
public String toString( ) {
return "Street= " + street + " ZIP= " + zip;
}
};
Examples (cont.)
– Example subclass: Address2Line
public class Address2Line extends Address implements java.io.Serializable {
public String line2;
// A default constructor
public Address2Line ( ) {
line2 = " ";
}
// A constructor with parameters
public Address2Line (String S, String L2, String Z) {
street = S;
line2 = L2;
zip = Z;
}
// A method to return a string representation of the full address
create type addr_2_lineunder addrexternal name ’Address2Line’ language java(line2_attr varchar(100) external name ’line2’,method addr_2_line ( ) returns addr_2_line external name