73
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.

SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Embed Size (px)

Citation preview

Page 1: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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.

Page 2: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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

Page 3: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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

Page 4: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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

Page 5: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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

Page 6: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ - 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

Page 7: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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

Page 8: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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 types int[] types = {Types.JAVA_OBJECT};

ResultSet rs = dmd.getUDTs("catalog-name", "schema-name", "%", types);

Page 9: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

JDBC 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);

Page 10: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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

Page 11: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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

Page 12: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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)

Page 13: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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) };

Page 14: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ 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.execute ();stmt.close();

Page 15: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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

Page 16: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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

Page 17: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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)};

Page 18: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Extensible 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

Page 19: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ translator framework

SQLJprogram

SQLChecker

Java Frontend

SQLJ Translator

Java Class Files

SQLJ Profiles

SQLJ JAR FILE

Profile Customizer

Utility

SQLJ Customizations

Page 20: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ portability layers

SQL DB SQL DBSQL DB

SQLJ ProgramProfile Entries

JDBC

Page 21: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Custom SQL execution

SQL DB SQL DBSQL DBStored procedure TP serviceSQL Module

SQLJ Program

Profile Entries

JDBC

Customizations

Page 22: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Profile customization selection

CustomizationsProfileData source URLs

Page 23: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ profile objects Profile ProfileData EntryInfo TypeInfo

ConnectedProfileCustomization RTStatement

Page 24: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ compilation phases

SQ

LJ T

ranslator

Foo.sqlj Foo.java Foo.classJava Com

piler

Page 25: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ translation phase

SQ

LJ T

ranslator

Foo.sqlj

Page 26: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ semantic analysis

SQ

LJ T

ranslator

Foo.sqlj[Ctx0]{SQL0}

describe(SQL0)

SQLChecker0

Page 27: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ semantic analysis

SQ

LJ T

ranslator

Foo.sqlj[Ctx0]{SQL0}[Ctx0]{SQL1}

describe(SQL1)

SQLChecker0

Page 28: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ semantic analysis

SQ

LJ T

ranslator

Foo.sqlj[Ctx0]{SQL0}[Ctx0]{SQL1}

SQLChecker0

(Ctx1){SQL2}

describe(SQL2)

SQLChecker1

Page 29: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ code generation

SQ

LJ T

ranslator

Foo.sqlj[Ctx0]{SQL0}[Ctx0]{SQL1}[Ctx1]{SQL2}

Foo.java

Page 30: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ code generation

SQ

LJ T

ranslator

Foo.jsql[Ctx0]{SQL0}[Ctx0]{SQL1}[Ctx1)]SQL2}

Foo.java

Entry0

Profile0.ser

Profile0:Entry0

Page 31: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ code generation

SQ

LJ T

ranslator

Foo.jsql[Ctx0]{SQL0}[Ctx0]{SQL1}[Ctx1]{SQL2}

Foo.java

Entry0

Profile0.ser

Profile0:Entry0

Profile0:Entry1

Entry1

Page 32: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ code generation

SQ

LJ T

ranslator

Foo.jsql[Ctx0]{SQL0}[Ctx0]{SQL1}[Ctx1]{SQL2}

Foo.java

Entry0

Profile0.ser

Profile0:Entry0

Profile0:Entry1

Entry1

Entry0

Profile1.ser

Profile1:Entry0

Page 33: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Java compilation

SQ

LJ T

ranslator

Foo.sqlj[Ctx0]{SQL0}[Ctx0]{SQL1}[Ctx1]{SQL2}

Foo.java

Entry0

Profile0.ser

Profile0:Entry0

Profile0:Entry1

Entry1

Entry0

Profile1.ser

Profile1:Entry0

Java Com

piler

Foo.classProfile0:Entry0

Profile0:Entry1

Profile1:Entry0

Page 34: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ packaging

SQ

LJ T

ranslator

Foo.sqlj[Ctx0]{SQL0}[Ctx0]{SQL1}[Ctx1]{SQL2}

Foo.java

Entry0

Profile0.ser

Profile0:Entry0

Profile0:Entry1

Entry1

Entry0

Profile1.ser

Profile1:Entry0

Java Com

piler

Foo.classProfile0:Entry0

Profile0:Entry1

Profile1:Entry0

Foo.jar

Page 35: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ installation phase

Profile0.ser

Profile1.ser

Foo.class

Foo.jar

Page 36: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ installation phase

Cu

stomizer1

Profile0.ser

Profile1.ser

Foo.class

Foo.jar

Profile0.ser

Profile1.ser

Foo.class

Foo.jar

Customization

Page 37: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

SQLJ installation phase

Cu

stomizer1

Cu

stomizer2

Profile0.ser

Profile1.ser

Foo.class

Foo.jar

Profile0.ser

Profile1.ser

Foo.class

Foo.jar

Profile0.ser

Profile1.ser

Foo.class

Foo.jar

Customization1 Customization1

Customization2

Customization2

Page 38: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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

Page 39: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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.

A procedural and scripting language for SQL.– Portable across DBMSs.

– Deployable across tiers.

Page 40: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Technical 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.

Page 41: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Technical 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.

Page 42: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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

Page 43: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Examples– 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.bestEmps—Returns the top two employees as output parameters.

• Routines3.rankedEmps—Returns the employees as a result set.

Page 44: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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;

}

}

Page 45: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Installing 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.

Page 46: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Defining 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_jar:Routines1.correctStates'

language java parameter style java;

create function region_of(state char(20)) returns integer

no sql

external name 'routines1_jar:Routines1.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.

Page 47: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Defining 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.

Page 48: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Privileges

– 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".

Page 49: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Invoking 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');

Page 50: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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.

Page 51: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

OUT 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) > :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; }

}

}

Page 52: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

OUT 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;

Page 53: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

OUT 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.BigDecimal);

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.BigDecimal);

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);

Integer r2 = stmt.getInt(7);

BigDecimal s2 = stmt.getBigDecimal(8);

Page 54: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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.

– 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.

Page 55: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Result sets (cont.)– Example: The orderedEmps method

public class Routines3 {

public static orderedEmps(int regionParm, ResultSet[ ] rs )

throws SQLException {

Connection conn = DriverManager.getConnection

("JDBC:DEFAULT:CONNECTION");

java.sql.PreparedStatement stmt = conn.prepareStatement

("SELECT name, region_of(state) as region, sales

FROM emp WHERE region_of(state) > ?

AND sales IS NOT NULL

ORDER BY sales DESC");

stmt.setInteger(1, regionParm);

rs[0] = stmt.executeQuery();

return;

}

}

Page 56: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Result 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".

Page 57: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Result 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");

}

Page 58: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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.

Page 59: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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.

Page 60: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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’);

– 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.

Page 61: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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.

Page 62: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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 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”

}

Page 63: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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

Page 64: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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.

Page 65: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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;

}

};

Page 66: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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;

}

};

Page 67: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

CREATE 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

Page 68: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

CREATE 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'

)

Page 69: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

CREATE TYPE (cont.)– CREATE for Address2

create type addr_2_line under addrexternal 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 'removeLeadingBlanks')

Page 70: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Usage Privilege

– GRANTs for Address and Address2Line

grant usage on datatype addr to public;

grant usage on datatype addr2line to admin;

Page 71: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Java 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_addr>>zip, home_addr>>street, mailing_addr>>zip

from emps

where home_addr>>zip <> mailing_addr>>zip

• Methods and comparison:

select name, home_addr>>display(), mailing_addr>>display()

from emps

where home_addr <> mailing_addr

Page 72: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

Java Classes as SQL datatypes (cont.)

• Update:

update emps

set home_addr>>zip = '99783'

where name = 'Bob Smith'

update emps

set home_address = mailing_address --Normal Java substitutability

where home_address is null;

• 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.

Page 73: SQLJ: Java and Relational Databases Phil Shaw, Sybase Inc. Brian Becker, Oracle Corp. Johannes Klein, Tandem/Compaq Mark Hapner, JavaSoft Gray Clossman,

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.