Java in Adaptive Server Enterprise
|Chapter 1 An Introduction to Java in the Database|
This chapter provides an overview of Java classes in Adaptive Server Enterprise.
Adaptive Server provides a runtime environment for Java, which means that Java code can be executed in the server. Building a runtime environment for Java in the database server provides powerful new ways of managing and storing both data and logic.
You can use the Java programming language as an integral part of Transact-SQL.
You can reuse Java code in the different layers of your application--client, middle-tier, or server--and use them wherever makes most sense to you.
Java in Adaptive Server provides a more powerful language than stored procedures for building logic into the database.
Java classes become rich, user-defined data types.
Methods of Java classes provide new functions accessible from SQL.
Java can be used in the database without jeopardizing the integrity, security, and robustness of the database. Using Java does not alter the behavior of existing SQL statements or other aspects of non-Java relational database behavior.
Java in Adaptive Server provides these functionalities:
Java user-defined functions (UDFs)
Java classes as datatypes in SQL
You can install Java classes in the Adaptive Server database, and then invoke the methods of those classes, both from within the SQL system and from client systems.
The methods of an object-oriented language correspond to the functions of a procedural language. You can invoke Java methods as UDFs in, for example, select lists and where clauses. You can use methods from other sources or methods you create and test.
With Java in the database, you can install pure Java classes in a SQL system, and then use those classes in a natural manner as datatypes in SQL. This capability adds a full object-oriented datatype extension mechanism to SQL, using a model that is widely understood and a language that is portable and widely available. The objects that you create and store with this facility are readily transferable to any Java-enabled environment, either in another SQL system or stand-alone Java environment.
This capability of using Java classes in the database has two different but complementary uses:
It provides a type extension mechanism for SQL, which you can use for data that is created and processed in SQL.
It provides a persistent data capability for Java, which you can use to store data in SQL that is created and processed (mainly) in Java. Java in Adaptive Server provides a distinct advantage over traditional SQL facilities: You do not need to map the Java objects into scalar SQL data types or store the Java objects as untyped binary strings.
The SQLJ consortium of SQL vendors is developing specifications for using Java with SQL. The consortium submits these specifications to ANSI for formal processing as standards.
The SQLJ specifications are divided into three parts:
Part 0 - specifications for embedding SQL statements in Java methods, similar to the traditional SQL facilities for embedded SQL in COBOL and C and other languages. The Java classes containing embedded SQL statements are precompiled to pure Java classes with JDBC calls.
Part 1 - specifications for installing Java classes in a SQL system, and for invoking Java static methods as SQL stored procedures and functions.
Part 2 - specifications for using Java classes as SQL datatypes.
You can use methods and classes using the Part 0 specifications with Java in Adaptive Server.
Java in Adaptive Server provided the basis for Parts 1 and 2. However, Java in Adaptive Server allows you to use Java names directly in SQL, whereas SQLJ Parts 1 and 2 currently require that you use the SQL create statement to define SQL aliases for Java method and class names. Java in Adaptive Server will support the SQLJ Parts 1 and 2 specifications when they are finalized.
Although this book assumes that readers are familiar with Java, there is much to learn about Java in a database. Sybase is not only extending the capabilities of the database with Java, but also extending the capabilities of Java with the database.
Both experienced and novice Java users should read this section. It uses a question-and-answer format to familiarize you with the basics of Java in Adaptive Server.
All of these points are explained in detail in later sections. With Java in Adaptive Server, you can:
Run Java in the database server using an internal Java Virtual Machine (Java VM).
Call Java functions (methods) from SQL statements.
Access data from Java using an internal JDBC driver.
Use Java classes as datatypes.
Save instances of Java objects in tables.
Generate XML-formatted documents from data stored in Adaptive Server databases and, conversely, store XML documents and data extracted from them in Adaptive Server databases.
Debug Java in the database.
Preserve the behavior of existing SQL statements and other aspects of non-Java relational database behavior.
Java is an object-oriented language, so its instructions (source code) come in the form of classes. You write and compile the Java instructions outside the database into compiled classes (byte code), which are binary files holding Java instructions.
You then install the compiled classes into the database, where they can be executed in the database server.
Adaptive Server is a runtime environment for Java classes. You need a Java development environment, such as Sybase PowerJ(TM) or Sun Microsystems Java Development Kit (JDK), to write and compile Java.
To support Java in the database, Adaptive Server:
Comes with its own Java VM, specifically developed for handling Java processing in the server.
Uses its own JDBC driver that runs in the server and accesses a database.
The Sybase Java VM runs in the database environment. It interprets compiled Java instructions and runs them in the database server.
The Sybase Java VM meets the JCM specifications from JavaSoft; it is designed to work with the 1.1.6 version of the Java API. It supports public class and instance methods; classes inheriting from other classes; the Java API; and access to protected, public, and private fields. Some Java API functions not appropriate in a server environment, such as user interface elements, are not supported. All supported Java API packages and classes come with Adaptive Server.
The Adaptive Server Java VM is available at all times to perform a Java operation whenever it is required as part of the execution of a SQL statement. The database server starts the Java VM automatically when it is needed; you do not need to take any explicit action to start or stop the Java VM.Client- and Server-Side JDBC
JDBC is the industry standard API for executing SQL in Java.
Adaptive Server provides its own server-side JDBC driver. This driver is designed to maximize performance as it executes on the server because it does not need to communicate across the network. This internal driver permits Java classes installed in a database to use JDBC classes that execute SQL statements.
When JDBC classes are used within a client application, you typically must use jConnect® for JDBC(TM), the Sybase client-side JDBC database driver, to provide the classes necessary to establish a database connection.
A guiding principle for the design of Java in the database is that it provides a natural, open extension to existing SQL functionality.
Java operations are invoked from SQL- Sybase has extended the range of SQL expressions to include fields and methods of Java objects, so that Java operations can be included in a SQL statement.
Java classes become user-defined datatypes - You store Java classes using the same SQL statements as those used for traditional SQL datatypes.
You can use classes that are part of the Java API and classes created and compiled by Java developers. The Java API classes are created and compiled by Sun Microsystems and by Sybase.
The Java Application Programmer's Interface (API) is a set of classes defined by Sun Microsystems. It provides a range of base functionality that can be used and extended by Java developers. It is the core of "what you can do" with Java.
The Java API offers considerable functionality in its own right. A large portion of the Java API is built in to any database that is enabled to use Java code--which includes the majority of non-visual classes from the Java API already familiar to developers using the Sun Microsystems JDK.
You can use the Java API in classes, in stored procedures, and in SQL statements. You can treat the Java API classes as extensions to the available built-in functions provided by SQL.
You can use the Java API in classes, in stored procedures, and in SQL statements. You can create the Java API classes as extensions to the available built-in functions provided by SQL.
For example, the SQL function PI(*) returns the value for Pi. The Java API class java.lang.Math has a parallel field named PI that returns the same value. But java.lang.Math also has a field named E that returns the base of the natural logarithm, as well as a method that computes the remainder operation on two arguments as prescribed by the IEE754 standard.
Not all Java API classes are supported in the database. Some classes, for example the java.awt package that contains user interface components for applications, is not appropriate inside a database server. Other classes, including part of java.io, deal with writing information to a disk, and this also is not supported in the database server environment.
You can install your own Java classes into the database as, for example, a user-created Employee class or Inventory class that a developer designed, wrote, and compiled with a Java compiler.
User-defined Java classes can contain both information and methods. Once installed in a database, Adaptive Server lets you use these classes in all parts and operations of the database and execute their functionality (in the form of class or instance methods) as easily as calling a stored procedure.
The JDBC interface is an industry standard designed to access database systems. The JDBC classes are designed to connect to a database, request data using SQL statements, and return results that can be processed in the client application.
You can connect from a client application to Adaptive Server Enterprise via JDBC, using jConnect or a JDBC/ODBC bridge. Adaptive Server also provides an internal JDBC driver, which permits Java classes installed in a database to use JDBC classes that execute SQL statements.
The Java in Adaptive Server design allows you to create Java classes that can be moved between levels of an enterprise application: The same Java class can be integrated into either the client application, a middle tier, or the database.
Using Java classes, whether user-defined or from the Java API, in SQL is a three-step activity:
Write or acquire a set of Java classes that you want to use as SQL datatypes.
Install those classes in the Adaptive Server database.
Use those classes in SQL code:
Call class (static) methods of those classes as UDFs.
Declare the Java classes as datatypes of SQL columns, variables, and parameters. In this book, they are called Java-SQL columns, variables, and parameters.
Reference the Java-SQL columns, their fields, and their methods.
There are many books about Java and Java in the database. Two particularly useful books are:
James Gosling, Bill Joy, and Guy Steele, The Java Language Specification, Addison-Wesley, 1996.
Graham Hamilton and Rick Cattell, JDBC: A Java SQL API, Version 1.20, JavaSoft, January 10, 1997.
Adaptive Server is a runtime environment for Java classes, not a Java development environment.
You cannot carry out these tasks in the database:
Edit class source files (*.java files).
Compile Java class source files (*.java files).
Execute Java APIs that are not supported, such as applet and visual classes.
In this release of Adaptive Server, certain other restrictions apply:
If a Java method accesses the database through JDBC, result-set values are available only to the Java method, not to the client application.
Output parameters are not supported. A method can manipulate the data it receives from a JDBC connection, but the only value it can return to its caller is a single return value declared as part of its definition.
The chapters of this book use simple Java classes to illustrate basic principles for using Java in the database. You can find copies of these classes in the chapters that describe them and in the Sybase release directory in $SYBASE/$SYBASE_ASE/sample/JavaSql (UNIX) or %SYBASE%\Ase-12_0\sample\JavaSql (Windows NT). This subdirectory also contains Javadoc facilities so that you can view specifications about sample classes and methods with your Web browser.