User's Guide
PART 3. Java in the Database
CHAPTER 16. Welcome to Java in the Database
This section describes the key features of Java in Adaptive Server Anywhere.
All the following points are explained in detail in later sections.
You can run Java in the database server An internal Java Virtual Machine (VM) runs Java code in the database server.
You can call Java from SQL You can call Java functions (methods) from SQL statements. Java methods provide a more powerful language than SQL stored procedures for adding logic to the database.
You can access data from Java An internal JDBC driver lets you access data from Java.
You can debug Java in the database You can use the Sybase Java debugger to test and debug your Java classes in the database.
You can use Java classes as data types Every Java class installed in a database becomes available as a data type that can be used as the data type of a column in a table or a variable.
You can save Java objects in tables An instance of a Java class (a Java object) can be saved as a value in a table. Java objects can be inserted into a table, SELECT statements can be executed against the fields and methods of objects stored in a table, and Java objects can be retrieved from a table.
With this ability, Adaptive Server Anywhere becomes an object-relational database, supporting objects while not degrading existing relational functionality.
SQL is preserved The use of Java does not alter the behavior of existing SQL statements or 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. In order to execute Java in a database, you write the Java instructions outside the database, and compile them outside the database into compiled classes (byte code), which are binary files holding Java instructions.
You then install these compiled classes into a database. Once installed, these classes can be executed in the database server.
Adaptive Server Anywhere is a runtime environment for Java classes, not a Java development environment. You need a Java development environment, such as Sybase PowerJ or the Sun Microsystems Java Development Kit, to write and compile Java.
Adaptive Server Anywhere includes a Java Virtual Machine (VM), which runs in the database environment. The Sybase Java VM interprets compiled Java instructions and runs them in the database server.
The Sybase Java VM supports public class and instance methods; classes inheriting from other classes; packages; the Java API; and access to protected, public and private fields. Some Java API functions that are not appropriate in a server environment are not supported, including user interface elements.
In addition to the VM, the SQL request processor in the database server has been extended so that it can call into the VM to execute Java instructions. It can also process requests from the VM, to enable data access from Java.
There is a difference between executing Java code using a standard VM such as the Sun Java VM java.exe and executing Java code in the database. The Sun VM is run from a command line, while the Adaptive Server Anywhere 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 Sybase Java interpreter cannot be accessed externally. It is only used when the execution of a SQL statement requires a Java operation to take place. The database server starts the VM automatically when needed: you do not have to take any explicit action to start or stop the VM.
Java provides a number of features that make it ideal for use in the database:
Thorough error checking at compile time.
Built-in error handing with a well-defined error handling methodology.
Built-in garbage collection (memory recovery).
Elimination of many bug-prone programming techniques.
Strong security features
Java code is interpreted, so no operations get executed without being acceptable to the VM.
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 properties and methods of Java objects, so that Java operations can be included in a SQL statement.
Java classes become user-defined data types You store Java classes using the same SQL statements as those used for traditional SQL data types.
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 created by Sun Microsystems. It provides a range of base functionality that can be used and extended by Java developers. It is at the core of 'what you can do' with Java.
The Java API offers a tremendous amount of 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. This exposes the majority of non-visual classes from the Java API that should be familiar to developers currently using the Sun Microsystems Java Development Kit (JDK).
For a complete list of supported Java APIs, see Built-in Java classes.
In addition to using the Java API in classes, it can be used in stored procedures and SQL statements. You can treat 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 returning the same value. But java.lang.Math also has a field named E that returns the base of the natural logarithms, as well as a method that computes the remainder operation on two arguments as prescribed by the IEEE 754 standard.
Other members of the Java API offer even more specialized functionality. For example, java.util.Stack generates a last-in, first-out queue that can store a list in an ordered manner; java.util.HashTable is used to map values to keys; java.util.StringTokenizer breaks a string of characters into individual word units.
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 parts of java.io, deal with writing information to disk, and this also is not supported in the database server environment.
For a list of supported and unsupported classes, see Built-in Java classes, and Unsupported packages and classes.
You can install your own Java classes into a database. For example a user-created Employee class or Package class that a developer designed, wrote in Java, and compiled with a Java compiler.
User-created Java classes can contain both information about the subject and some computational logic. Once installed in a database, Adaptive Server Anywhere 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.
Java classes not the same as stored procedures |
When a Java class gets installed in a database, it becomes available as a new user-defined data type. A Java class can be used in any situation where built-in SQL data types can be used: as a column type in a table or a variable type.
For example, if a class called Address has been installed into a database, a column in a table called Addr can be of type Address, which means only objects based on the Address class can be saved as row values for that column.
The JDBC interface is an industry standard designed specifically to access database systems. The JDBC classes are designed to connect to a database, request data using SQL statements, and return result sets that can be processed in the client application.
Normally, JDBC classes are used within a client application, and the database system vendor supplies a JDBC driver that allows the JDBC classes to establish a connection.
You can connect from a client application to Adaptive Server Anywhere via JDBC, using jConnect or a JDBC/ODBC bridge. Adaptive Server Anywhere also provides an internal JDBC driver, which permits Java classes installed in a database to use JDBC classes that execute SQL statements.
This 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—wherever it is most appropriate.
A class that contains business logic, data or a combination of both, can be moved to any level of the enterprise system, including the server. This allows you complete flexibility to make the most appropriate use of resources.
This enables enterprise customers to develop their applications using a single programming language in a multi-tier architecture with unparalleled flexibility.
You can create an application that has some pieces operating in the database and some on the client machine. You can pass Java objects from the server to the client. This includes objects stored in database tables.
For an example, see Creating distributed applications.
Adaptive Server Anywhere is a runtime environment for Java classes, not a Java development environment.
You cannot carry out the following 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.
The Java classes used in Adaptive Server Anywhere must be written and compiled using a Java application development tool, and then installed into a database for use, testing, and debugging.