SQLJ: Java and Relational Databases
SQLJ: Java and Relational Databases
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
Java-Relational Database Technology
Portability; Productivity; Java in the Database
Leverages JDBC technology
JavaBlend
Object/Relational Mapping for Java
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
SQLJ Procedures
Specifications mostly reviewed
Implementations, e.g., Sybase Adaptive Server Anywhere 6.0, Oracle
8.1, IBM
SQL3 types
BLOB, CLOB, array, reference
Structured and distinct types
ResultSet rs = stmt.executeQuery(
"SELECT CUSTOMER FROM ACCOUNTS");
rs.next();
Customer cust = (Customer)rs.getObject(1);
Agenda
Overview and JDBC 2.0: New Features
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 0:
SQL Embedded in Java
Objectives
Simple, concise language for embedding SQL statements in
Java programs
Standard to allow for assembly of binary components
produced by different tools
Standard to allow for binary portability across different
database systems
Advantages
Ahead-of-time syntax and type checking
Strongly typed cursors (iterators)
Offline pre-compilation (for performance)
Deployment-time customization (for binary portability
and native pre-compilation)
SQLJ clauses
SQLJ statements start with #sql
SQLJ statements terminate with ;
SQLJ host variables start with :
SQL text is enclosed in curly braces {..}
int n;
#sql { INSERT INTO emp VALUES (:n) };
// JDBC
int n;
Statement stmt = conn.prepareStatement
(INSERT INTO emp VALUES (?));
stmt.setInt(1,n);
stmt.execute ();
stmt.close();
Connection context
SQLJ statements are associated with a connection
context
Context type identifies exemplar schema, e.g. views,
tables, privileges
#sql context Department;
Department dept = newDepartment(jdbc:odbc:acme.cs);
int n;
#sql [dept] { insert into EMP values (:n)};
SQLJ
program
SQLChecker
Java Frontend
SQLJ Translator
SQLJ
Profiles
Profile
Customizer
Utility
SQLJ
Customizations
SQL DB
SQL DB
SQL DB
SQL DB
Stored procedure
SQL DB
TP service
SQL DB
Customizations
ProfileData
EntryInfo
TypeInfo
Java Compiler
SQLJ Translator
Foo.sqlj
Foo.class
Foo.sqlj
SQLJ Translator
Foo.sqlj
describe(SQL0)
SQLChecker0
SQLJ Translator
Foo.sqlj
describe(SQL1)
SQLChecker0
SQLJ Translator
Foo.sqlj
describe(SQL2)
SQLChecker0
SQLChecker1
SQLJ Translator
Foo.sqlj
Foo.java
SQLJ Translator
Foo.jsql
Foo.java
Profile0:
Entry0
Profile0.ser
Entry0
SQLJ Translator
Foo.jsql
Foo.java
Profile0:
Entry0
Profile0:
Entry1
Profile0.ser
Entry0
Entry1
SQLJ Translator
Foo.jsql
Foo.java
Profile0:
Entry0
Profile0:
Entry1
Profile1:
Entry0
Profile0.ser
Entry0
Entry1
Profile1.ser
Entry0
Java compilation
Foo.java
Profile0:
Entry0
Profile0:
Entry1
Profile1:
Entry0
Java Compiler
[Ctx0]
{SQL0}
[Ctx0]
{SQL1}
[Ctx1]
{SQL2}
SQLJ Translator
Foo.sqlj
Foo.class
Profile0:
Entry0
Profile0:
Entry1
Profile1:
Entry0
Profile0.ser
Entry0
Entry1
Profile1.ser
Entry0
SQLJ packaging
Foo.jar
Foo.java
Profile0:
Entry0
Profile0:
Entry1
Profile1:
Entry0
Java Compiler
[Ctx0]
{SQL0}
[Ctx0]
{SQL1}
[Ctx1]
{SQL2}
SQLJ Translator
Foo.sqlj
Foo.class
Profile0:
Entry0
Profile0:
Entry1
Profile1:
Entry0
Profile0.ser
Entry0
Entry1
Profile1.ser
Entry0
Profile0.ser
Profile1.ser
Foo.jar
Foo.class
Foo.class
Customizer1
Profile0.ser
Profile0.ser
Customization
Profile1.ser
Profile1.ser
Foo.jar
Foo.jar
Foo.class
Foo.class
Foo.class
Customizer2
Customizer1
Profile0.ser
Profile0.ser
Customization1
Profile0.ser
Customization1
Customization2
Profile1.ser
Profile1.ser
Profile1.ser
Customization2
Agenda
JDBC 2.0: New Features
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 1:
Java methods as SQL procedures
Use Java static methods as SQL stored procedures
and functions.
Advantage to SQL: Direct use of pre-written Java
libraries.
Technical objectives
Convenient for Java programmers.
Not just aimed at SQL programmers.
Topics
Example Java classes
Defining Java classes to SQL
Installing jar files
Specifying SQL names
SQL Permissions
OUT parameters
Result sets
Error handling
Paths
Deployment descriptors
Examples
Example table:
create table emps (
name varchar(50),
id char(5),
state char(20),
sales decimal (6,2));
Examples (cont.)
The region and correctStates methods
public class Routines1 {
//The region method
//An Integer method that will be called as a function
public static Integer region(String s) throws SQLException {
if (s == "MN" || s == "VT" || s == "NH" ) return 1;
else if (s == "FL" || s == "GA" || s == "AL" ) return 2;
else if (s == "CA" || s == "AZ" || s == "NV") return 3;
else return 4;
}
//The correctStates method
//A void method that will be called as a stored procedure
public static void correctStates (String oldSpelling, String newSpelling) throws SQLException {
Connection conn = DriverManager.getConnection ("JDBC:DEFAULT:CONNECTION");
PreparedStatement stmt = conn.prepareStatement ("UPDATE emps SET state = ? WHERE state = ?");
stmt.setString(1, newSpelling);
stmt.setString(2, oldSpelling);
stmt.executeUpdate();
return;
}
}
Two parameters:
The URL of a jar file containing a set of Java classes
A character string that will be used to identify the Jar in SQL
Privileges
The usage privilege on the installed jar file is grantable:
grant usage on routines1_jar to Smith
OUT parameters
SQL procedures have OUT and INOUT parameters; Java doesn't.
If a Java method will be used as an SQL proc with OUT
parameters, those parameters are declared as Java arrays, to act as
"containers".
Example (next page):
bestTwoEmps returns the two top employees in a given region.
The specific region is an in parameter.
The column values of the two top employees are out parameters.
The bestTwoEmps method is coded with JSQL.
A version of bestTwoEmps coded with JDBC is shown in the draft specs, for
comparison.
Result sets
SQL procedures can return result sets that are neither parameters
nor function results.
An SQL result set is a set of rows generated by the callee for the caller.
The caller processes the result set iteratively.
Example (below):
The orderedEmps method returns a result set with the employees of a
given region ordered by sales.
Error Handling
General treatment:
Exceptions thrown and caught within an SQLJ stored
procedure are internal to Java.
Exceptions that are uncaught when you return from a Java
method become SQLSTATE error codes.
The message text of the SQLSTATE is the string specified in
the Java throw.
Paths
In Java, resolution of class names is done with the operating
system CLASSPATH.
The CLASSPATH mechanism uses the operating system directory
structure.
SQLJ defines a similar mechanism for name resolution.
Assume you have three jar files that reference classes in each
other:
The admin jar references classes in the property and project jars.
The property jar references classes in the project jar.
The project jar references classes in the property and admin jars.
Paths (cont.)
You install the jar files as usual:
sqlj.install_jar (file:~/classes/admin.jar, admin_jar);
sqlj.install_jar (file:~/classes/property.jar, property_jar);
sqlj.install_jar (file:~/classes/project.jar, project_jar);
Deployment descriptors
A deployment descriptor is a text file containing the create
and grant statements to do on install_jar, and the drop and
revoke statements to do on remove_jar.
A deployment descriptor is contained in a jar file with the
classes it describes.
The install_jar procedure will implicitly perform the create
and grant statements indicated by the deployment descriptor.
The remove_jar procedure will implicitly perform the drop
and revoke statements indicated by the deployment
descriptor.
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.
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
public String toString( ) {
return "Street= " + street + " Line2= " + line2 + " ZIP= " + zip;
}
};
CREATE TYPE
The role of create type is like that of create procedure:
Specify SQL names for the type, the fields, and the methods.
Usage Privilege
GRANTs for Address and Address2Line
grant usage on datatype addr to public;
grant usage on datatype addr2line to admin;
Insert:
insert into emps values('Bob Smith', new Address('432 Elm Street', '99782'),
new Address2Line('PO Box 99', 'attn: Bob Smith', '99678'));
Select:
select name, home_addr>>zip, home_addr>>street, mailing_addr>>zip
from emps
where home_addr>>zip <> mailing_addr>>zip
Note the use of >> to reference fields and methods of Java instances
in SQL.
This avoids ambiguities with SQL dot-qualified names.
The >> symbol is used in SQL3 for ADT references.
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.