Title: SQLJ: Java and Relational Databases
1SQLJJava 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.
2Agenda
- 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
3Java 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
4SQLJ - 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
5SQLJ - 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
6SQLJ - The Standard
- Goal of the SQLJ Consortium is to create workable
standards 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
a set of conformance tests
7SQLJ - Implementation Status
- SQLJ Embedded SQL
- Public-domain reference implementation available
from Oraclehttp//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
8JDBC 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 types
- int types Types.JAVA_OBJECT
- ResultSet rs dmd.getUDTs("catalog-name",
"schema-name", "", types)
9JDBC 2.0 - SQLJ Features
- Objects-by-Value
- Java Classes as database types
- this just works
- SQL3 ADTs as database types
- Java mapping maintained per Connection
- Seamless extension of get/setObject()
- Statement stmt
-
- ResultSet rs stmt.executeQuery(
- "SELECT CUSTOMER FROM ACCOUNTS")
- rs.next()
- Customer cust (Customer)rs.getObject(1)
10Agenda
- 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
11SQLJ Part 0SQL 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
12Advantages
- 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)
13SQLJ clauses
- SQLJ statements start with sql
- SQLJ statements terminate with
- SQLJ host variables start with
- SQL text is enclosed in curly braces ..int
nsql INSERT INTO emp VALUES (n)
14SQLJ more concise than JDBC
// SQLJ int n sql INSERT INTO emp VALUES
(n) // JDBC int n Statement stmt
conn.prepareStatement (INSERT
INTO emp VALUES (?)) stmt.setInt(1,n) stmt.exec
ute () stmt.close()
15Strongly typed cursors
- Positional binding to columns
sql public iterator ByPos (String, int)ByPos
positerString name nullint year 0 sql
positer SELECT name, year FROM peoplewhile
(true) sql FETCH positer INTO name,
year if (positer.endFetch()) break
// process name, year positer.close()
16Strongly typed cursors (cont.)
sql public iterator ByName (int year, String
name) ByName namiterString name nullint
year 0 sql namiter SELECT name, year
FROM peoplewhile (namiter.next()) name
namiter.name() year namiter.year() //
process name, year namiter.close()
17Connection context
- SQLJ statements are associated with a connection
context - Context type identifies exemplar schema, e.g.
views, tables, privilegessql context
DepartmentDepartment dept newDepartment(jdbc
odbcacme.cs)int nsql dept insert into
EMP values (n)
18Extensible SQLJ framework
- Database vendors plug-in SQL syntax checkers and
semantic analyzers using SQLChecker framework - Database vendors provide customizers to install
SQLJ binaries (profiles) in target database - Default SQLJ binaries run on any JDBC driver
19SQLJ translator framework
Java Class Files
SQLChecker
SQLChecker
SQLJ program
Java Frontend
SQLJ Customizations
Profile Customizer Utility
SQLJ Profiles
SQLJ Translator
SQLJ JAR FILE
20SQLJ portability layers
SQLJ Program
Profile Entries
JDBC
SQL DB
SQL DB
SQL DB
21Custom SQL execution
SQLJ Program
Profile Entries
JDBC
Customizations
Stored procedure
TP service
SQL Module
SQL DB
SQL DB
SQL DB
22Profile customization selection
Customizations
Profile
Data source URLs
23SQLJ profile objects
Profile
ProfileData
EntryInfo
TypeInfo
ConnectedProfile
Customization
RTStatement
24SQLJ compilation phases
Foo.sqlj
Foo.java
Foo.class
25SQLJ translation phase
Foo.sqlj
26SQLJ semantic analysis
Foo.sqlj
Ctx0SQL0
27SQLJ semantic analysis
Foo.sqlj
Ctx0SQL0
Ctx0SQL1
describe(SQL1)
SQLChecker0
28SQLJ semantic analysis
Foo.sqlj
Ctx0SQL0
Ctx0SQL1
(Ctx1)SQL2
29SQLJ code generation
Foo.sqlj
Foo.java
Ctx0SQL0
Ctx0SQL1
Ctx1SQL2
30SQLJ code generation
Foo.jsql
Foo.java
Ctx0SQL0
Profile0Entry0
Ctx0SQL1
Ctx1)SQL2
Profile0.ser
Entry0
31SQLJ code generation
Foo.jsql
Foo.java
Ctx0SQL0
Profile0Entry0
Ctx0SQL1
Profile0Entry1
Ctx1SQL2
Profile0.ser
Entry0
Entry1
32SQLJ code generation
Foo.jsql
Foo.java
Ctx0SQL0
Profile0Entry0
Ctx0SQL1
Profile0Entry1
Ctx1SQL2
Profile1Entry0
Profile0.ser
Entry0
Entry1
Profile1.ser
Entry0
33Java compilation
Foo.sqlj
Foo.java
Foo.class
Ctx0SQL0
Profile0Entry0
Profile0Entry0
Java Compiler
Ctx0SQL1
Profile0Entry1
Profile0Entry1
Ctx1SQL2
Profile1Entry0
Profile1Entry0
Profile0.ser
Entry0
Entry1
Profile1.ser
Entry0
34SQLJ packaging
Foo.jar
Foo.sqlj
Foo.java
Foo.class
Ctx0SQL0
Profile0Entry0
Profile0Entry0
Java Compiler
Ctx0SQL1
Profile0Entry1
Profile0Entry1
Ctx1SQL2
Profile1Entry0
Profile1Entry0
Profile0.ser
Entry0
Entry1
Profile1.ser
Entry0
35SQLJ installation phase
36SQLJ installation phase
Foo.jar
Foo.class
Profile0.ser
Customization
Profile1.ser
37SQLJ installation phase
Foo.jar
Foo.jar
Foo.class
Foo.class
Customizer2
Profile0.ser
Profile0.ser
Customization1
Customization1
Customization2
Profile1.ser
Profile1.ser
Customization2
38Agenda
- 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
39SQLJ Part 1Java methods as SQL procedures
- Use Java static methods as SQL stored procedures
and functions. - Advantage to SQL Direct use of pre-written Java
libraries. - A procedural and scripting language for SQL.
- Portable across DBMSs.
- Deployable across tiers.
40Technical objectives
- Convenient for Java programmers.
- Not just aimed at SQL programmers.
- Portable across DBMSs.
- Same capability as regular SQL stored procedures.
- Arbitrary SQL stored procedures re-codable as
SQLJ stored procedures. - Convenience and performance comparable with SQL
routines. - Callable from CLI/ODBC, from other SQL stored
procedures, from JDBC/JSQL, and directly from
Java. - Caller needn't know the SQLJ stored procedure is
in Java.
41Technical objectives (cont.)
- Any Java static method callable as a stored
procedure - Initially support only parameter and result types
mappable to SQL. - Extensible to support arbitrary Java types, for
Java caller and callee. - Body of SQLJ stored procedure routines can use
JDBC and/or SQLJ to access SQL, or Java
computation - Initially support persistence only for duration
of a call. - Consider session and database persistence as
follow-on.
42Topics
- 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
43Examples
- Example table
- create table emps (
- name varchar(50),
- id char(5),
- state char(20),
- sales decimal (6,2))
- Example classes and methods
- Routines1.region Maps a state code to a region
number. Plain Java (no SQL). - Routines1.correctStates Performs an SQL update
to correct the state codes. - Routines2.bestEmpsReturns the top two employees
as output parameters. - Routines3.rankedEmpsReturns the employees as a
result set.
44Examples (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
("JDBCDEFAULTCONNECTION") - PreparedStatement stmt conn.prepareStatement
("UPDATE emps SET state ? WHERE state ?") - stmt.setString(1, newSpelling)
- stmt.setString(2, oldSpelling)
- stmt.executeUpdate()
- return
45Installing Java Classes in SQL
- New install_jar procedure
- sqlj.install_jar ('file/classes/Routines1.jar',
'routines1_jar' ) - 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 - Installs all classes in the jar file
- Uses Java reflection to determine their names,
methods, and signatures - Retains the Jar file, the character string
identifies it - The jar name is specified in a later remove_jar
procedure - Follow-on facilities will address replacing and
downloading jar files, etc.
46Defining SQL names for Java methods
- A form of the SQL create procedure/function
statement. - create procedure correct_states(old char(20), new
char(20)) - modifies sql data
- external name 'routines1_jarRoutines1.correctStat
es' - language java parameter style java
- create function region_of(state char(20)) returns
integer - no sql
- external name 'routines1_jarRoutines1.region'
- language java parameter style java
- The create procedure statement and the external
language X are standard. - The language alternative java is an SQLJ
extension.
47Defining SQL names for Java methods
- The procedure/function names "correct_states" and
"region_of" are normal SQL 3-part names, with
normal defaults. - You can do multiple create procedure statements
pointing to the same Java method. - The key role of create procedure is to define an
SQL synonym for the Java method. - Why use an SQL name?
- Java names have different syntax
case-sensitive, package names, Unicode, etc. - SQL metadata and permissions are keyed to SQL
names.
48Privileges
- The usage privilege on the installed jar file is
grantable - grant usage on routines1_jar to Smith
- The execute permission on the SQL names is
grantable. - grant execute on correct_states to Smith
- Methods run with "definer's rights".
49Invoking Java methods
- Use the SQL names, with normal defaults for the
first two parts - select name, region_of(state) as region
- from emps
- where region_of(state) 3
- call correct_states ('CAL', 'CA')
50OUT 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.
51OUT Parameters (cont.)
- public class Routines2
- public static void bestTwoEmps (String n1,
String id1, int r1, BigDecimal s1, - String n2, String id2, int r2,
BigDecimal s2, - Integer regionParm) throws SQLException
- 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) gt
regionParm AND sales IS NOT NULL - ORDER BY sales DESC"
- if (r.next())
- n10 r.name() id10 r.id()
- r10 r.region() s10 r.sales()
-
- else n10 "" return
- if (r.next())
- n20 r.name() id20 r.id()
- r20 r.region() s20 r.sales()
-
- else n20 "" return
52OUT parameters (cont.)
- CREATE PROC for the bestTwoEmps method
- The bestTwoEmps method has eight out parameters
and one in parameter - 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 data
- external name 'Routines2.bestTwoEmps'
- language java parameter style java
53OUT parameters (cont.)
- Invoking the best2 procedure
- java.sql.CallableStatement stmt
conn.prepareCall ("call best2(?,?,?,?,?,?,?,?,?)
") - stmt.registerOutParameter(1, java.sql.Types.String
) - stmt.registerOutParameter(2, java.sql.Types.String
) - stmt.registerOutParameter(3, java.sql.Types.Int)
- stmt.registerOutParameter(4, java.sql.Types.BigDec
imal) - stmt.registerOutParameter(5, java.sql.Types.String
) - stmt.registerOutParameter(6, java.sql.Types.String
) - stmt.registerOutParameter(7, java.sql.Types.Int)
- stmt.registerOutParameter(8, java.sql.Types.BigDec
imal) - stmt.setInt(9, 3)
- stmt.executeUpdate()
- String n1 stmt.getString(1)
- String id1 stmt.getString(2)
- Integer r1 stmt.getInt(3)
- BigDecimal s1 stmt.getBigDecimal(4)
- String n2 stmt.getString(5)
- String id2 stmt.getString(6)
54Result 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. - SQLJ models this as follows
- An SQL3 clause on create procedure specifies that
the proc has result sets. - Such an SQL proc can be defined on a Java method
with a result set return value. - Example (below)
- The orderedEmps method returns a result set with
the employees of a given region ordered by sales.
55Result sets (cont.)
- Example The orderedEmps method
- public class Routines3
- public static orderedEmps(int regionParm,
ResultSet rs ) - throws SQLException
- Connection conn DriverManager.getConnection
- ("JDBCDEFAULTCONNECTION")
- java.sql.PreparedStatement stmt
conn.prepareStatement - ("SELECT name, region_of(state) as region, sales
- FROM emp WHERE region_of(state) gt ?
- AND sales IS NOT NULL
- ORDER BY sales DESC")
- stmt.setInteger(1, regionParm)
- rs0 stmt.executeQuery()
- return
-
56Result sets (cont.)
- CREATE PROC for the orderedEmps method
- The orderedEmps method returns one result set
- create procedure ranked_emps (region integer)
- dynamic result sets 1
- reads sql data
- external name 'Routines3.orderedEmps'
- language java parameter style java
- The dynamic result sets clause is standard
ISO/ANSI SQL3. - Initially the dynamic result sets clause will
only allow "1".
57Result sets (cont.)
- Invoking the rankedEmps procedure
- java.sql.CallableStatement stmt
conn.prepareCall( - "call ranked_Emps(?)")
- stmt.setInt(1, 3)
- ResultSet rs stmt.executeQuery()
- while (rs.next())
- String name rs.getString(1)
- Integer region rs.getInt(2)
- BigDecimal sales rs.getBigDecimal(3)
- System.out.print(" Name " name)
- System.out.print(" Region " region)
- System.out.print(" Sales " sales)
- System.out.print("\n")
-
58Error 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.
59Paths
- 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.
60Paths (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) - Then you specify paths for the jar files
- sqlj.alter_java_path (admin_jar, (property/,
property_jar) (project/, project_jar)) - sqlj.alter_java_path (property_jar,
(project/, project_jar )) - sqlj.alter_java_path (project_jar, (,
property_jar) (, admin_jar) ) - When the Java VM encounteres an unloaded class
name in e.g. the admin_jar, it will invoke the
class loader supplied by the SQL system, which
will use the SQL path to resolve the name.
61Deployment 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.
62Deployment 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 the following 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
63Agenda
- 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
64SQLJ 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.
65Examples
- 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
66Examples (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 -
-
67CREATE TYPE
- The role of create type is like that of create
procedure - Specify SQL names for the type, the fields, and
the methods. - Additional clauses for ordering specs, etc.
- The above example uses the default ordering
68CREATE TYPE (cont.)
- CREATE for Address
- create type addr
- external name 'Address' language java
- (zip_attr char(10) external name 'zip',
- street_attr varchar(50) external name 'street',
- static rec_width_attr integer external name
'recommended_width', - method addr ( ) returns addr external name
'Address', - method addr (s_parm varchar(50), z_parm char(10))
returns addr - external name 'Address',
- method to_string ( ) returns varchar(255)
external name toString, - method remove_leading_blanks ( ) external name
removeLeadingBlanks - static method contiguous (A1 addr, A2 addr)
returns char(3) - external name 'contiguous'
- )
69CREATE TYPE (cont.)
- CREATE for Address2
- create type addr_2_line
- under addr
- external name 'Address2Line' language java
- (line2_attr varchar(100) external name 'line2',
- method addr_2_line ( ) returns addr_2_line
external name 'Address2Line', - method addr_2_line (s_parm varchar(50), s2_parm
char(100), z_parm char(10)) - returns addr_2_line external name
'Address2Line', - method to_string ( ) returns varchar(255)
external name 'toString', - method remove_leading_blanks ( ) external name
removeLeadingBlanks - method strip ( ) external name 'removeLeadingBlank
s' - )
70Usage Privilege
- GRANTs for Address and Address2Line
-
- grant usage on datatype addr to public
- grant usage on datatype addr2line to admin
71Java Classes as SQL datatypes
- Column data types
- create table emps (
- name varchar(30),
- home_addr addr),
- mailing_addr addr_2_line)
- 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_addrgtgtzip, home_addrgtgtstreet,
mailing_addrgtgtzip - from emps
- where home_addrgtgtzip ltgt mailing_addrgtgtzip
- Methods and comparison
- select name, home_addrgtgtdisplay(),
mailing_addrgtgtdisplay() - from emps
- where home_addr ltgt mailing_addr
72Java Classes as SQL datatypes (cont.)
- Update
- update emps
- set home_addrgtgtzip '99783'
- where name 'Bob Smith'
- update emps
- set home_address mailing_address --Normal
Java substitutability - where home_address is null
- Note the use of gtgt to reference fields and
methods of Java instances in SQL. - This avoids ambiguities with SQL dot-qualified
names. - The gtgt symbol is used in SQL3 for ADT
references.
73SQLJJava 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.