Collection Contents Index Introduction to Java in the database A Java seminar pdf/chap16.pdf

User's Guide
   PART 3. Java in the Database
     CHAPTER 16. Welcome to Java in the Database       

Java in the database Q & A


This section describes the key features of Java in Adaptive Server Anywhere.

Top of page  What are the key features of Java in the database?

All the following points are explained in detail in later sections.

Top of page  How do I store Java instructions in the database?

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.

Top of page  How does Java get executed in a database?

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.

Differences from a standalone VM 

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.

Top of page  Why Java?

Java provides a number of features that make it ideal for use in the database:

Top of page  How do 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.

Top of page  What is the Java API?

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 Info     For a complete list of supported Java APIs, see Built-in Java classes.

Top of page  How do I access the Java API from SQL?

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.

Top of page  Which Java classes are supported?

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 Info     For a list of supported and unsupported classes, see Built-in Java classes, and Unsupported packages and classes.

Top of page  How can I use my own Java classes in databases?

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    
This is not the same as writing stored procedures in Java. Stored procedures are written in SQL. Java classes provide a more powerful language than stored procedures, and yet can be called from client applications in the same way that stored procedures are called.

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.

Top of page  Can I access data using Java?

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.

Top of page  Can I move classes from client to server?

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.

Top of page  Can I create distributed applications?

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 Info     For an example, see Creating distributed applications.

Top of page  What can I not do with Java in the database?

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:

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.

Top of page  

Collection Contents Index Introduction to Java in the database A Java seminar pdf/chap16.pdf