Sybase Technical Library - Product Manuals Home
[Search Forms] [Previous Section with Hits] [Next Section with Hits] [Clear Search] Expand Search

About This Book [Table of Contents] Chapter 2 Preparing

for and Maintaining Java in the Database

Java in Adaptive Server Enterprise

[-] Chapter 1 An Introduction to Java in the Database

Chapter 1

An Introduction to Java in the Database

This chapter provides an overview of Java classes in Adaptive Server Enterprise.

Advantages of Java in the Database

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.

Capabilities of Java in the Database

Java in Adaptive Server provides these functionalities:

Java User-Defined Functions

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.

Java Classes as Datatypes

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:


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:

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.

Java in the Database: Questions and Answers

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.

What Are the Key Features?

All of these points are explained in detail in later sections. With Java in Adaptive Server, you can:

How Can I Store Java Instructions in the Database?

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.

How Is Java Executed in the Database?

To support Java in the database, Adaptive Server:

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.

How Can I Use Java and SQL Together?

A guiding principle for the design of Java in the database is that it provides a natural, open extension to existing SQL functionality.

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.

What Is the Java API?

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.

How Can I Access the Java API from 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.

Which Java Classes Are Supported in the Java API?

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, deal with writing information to a disk, and this also is not supported in the database server environment.

Can I Install My Own Java Classes?

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.

Can I Access Data Using Java?

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.

Can I Move Classes from Client to Server?

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.

How Do I Use Java Classes in SQL?

Using Java classes, whether user-defined or from the Java API, in SQL is a three-step activity:

Can I Find More Information About Java in the Database?

There are many books about Java and Java in the database. Two particularly useful books are:

What You Cannot Do with Java in the Database

Adaptive Server is a runtime environment for Java classes, not a Java development environment.

You cannot carry out these tasks in the database:

In this release of Adaptive Server, certain other restrictions apply:

Sample Java Classes

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.

About This Book [Table of Contents] Chapter 2 Preparing

for and Maintaining Java in the Database