Collection Contents Index Using JDBC to access data Creating distributed applications pdf/chap18.pdf

User's Guide
   PART 3. Java in the Database
     CHAPTER 18. Data Access Using JDBC       

Using the Sybase jConnect JDBC driver


If you wish to use JDBC from a client application or applet, you must have Sybase jConnect to connect to Adaptive Server Anywhere databases.

Depending on the package in which you received Adaptive Server Anywhere, Sybase jConnect may or may not be included. You must have jConnect in order to use JDBC from client applications. You can use server-side JDBC without jConnect.

For Info     For a full description of jConnect and its use with Adaptive Server Anywhere, see the jConnect documentation available in the online Books or from .

Top of page  Versions of jConnect supplied with Adaptive Server Anywhere

Adaptive Server Anywhere provides the following versions of the Sybase jConnect JDBC driver:

Top of page  The jConnect driver files

The Sybase jConnect driver is installed into a set of directories under the jConnect subdirectory of your Adaptive Server Anywhere installation. If you have not installed jConnect, you can use the jdbcdrv.zip file installed into the Java subdirectory.

Classpath setting for jConnect 

For your application to use jConnect, the jConnect classes must be in your CLASSPATH environment variable at compile time and run time, so that the Java compiler and Java runtime can locate the necessary files.

For example, the following command adds the jConnect driver class path to an existing CLASSPATH environment variable where path is your Adaptive Server Anywhere installation directory.

set classpath=%classpath%;path\jConnect\classes

The following alternative command adds the jdbcdrv.zip file to your CLASSPATH.

set classpath=%classpath%;path\java\jdbcdrv.zip

Importing the jConnect classes 

The classes in jConnect are all in the com.sybase package. The client application needs to access classes in com.sybase.jdbc. For your application to use jConnect, you must import these classes at the beginning of each source file:

import com.sybase.jdbc.*

Top of page  Installing jConnect system objects into a database

If you wish to use jConnect to access system table information (database metadata), you must add the jConnect system objects to your database.

By default, the jConnect system objects are added to a database for any database created using Version 6, and to any database upgraded to Version 6.

If you do not add the jConnect objects to the database when creating or upgrading, you can add them at a later time.

You can install the jConnect system objects from Sybase Central or from Interactive SQL.

  To add the jConnect system objects to a Version 6 database from Sybase Central:
  1. Connect to the database from Sybase Central as a user with DBA authority.

  2. Open the Java Objects folder.

  3. Double-click Re-Install jConnect Meta-data Support and follow the instructions in the wizard.

  To add the jConnect system objects to a Version 6 database from Interactive SQL:
  1. Connect to the database from Interactive SQL as a user with DBA authority, and enter the following command in the Command window:

    read path\scripts\jcatalog.sql

    where path is your Adaptive Server Anywhere installation directory.

  2. Alternatively, enter the following command at a system prompt:

    dbisql -c "uid=user;pwd=pwd" path\scripts\jcatalog.sql

    where user and pwd identify a user with DBA authority, and path is your Adaptive Server Anywhere installation directory.

Top of page  Loading the driver

Before you can use jConnect in your application, you must load the driver. The simplest way to do this is with the following statement:

Class.forName("com.sybase.jdbc.SybDriver").newInstance();

Using the newInstance method works around issues in some browsers.

Top of page  Supplying a URL for the server

In order to connect to a database via jConnect, you need to supply a Universal Resource Locator (URL) for the database. An example was given in the section Connecting from a JDBC client application.

In the example, the connection was established as follows:

StringBuffer temp = new StringBuffer();
// Use the Sybase jConnect driver...
temp.append("jdbc:sybase:Tds:"); 
// to connect to the supplied machine name...
temp.append(_coninfo);
// on the default port number for ASA...
temp.append(":2638");
// and connect.
System.out.println(temp.toString());
conn = DriverManager.getConnection(temp.toString() , _props );

The URL is put together in the following way:

jdbc:sybase:Tds:machine-name:port-number

The individual components are as follows:

The connection string must be less than 253 characters in length.

Top of page  Specifying a database on a server

Each Adaptive Server Anywhere server may have one or more databases loaded at a time. The URL as described above specifies a server, but does not specify a database. The connection attempt is made to the default database on the server.

You can specify a particular database by providing an extended form of the URL in one of the following ways.

Using the ServiceName parameter 

jdbc:sybase:Tds:machine-name:port-number?ServiceName=DBN

The question mark followed by a series of assignments is a standard way of providing arguments to a URL. The case of servicename is not significant, and there must be no spaces around the = sign. The DBN parameter is the database name.

Using the RemotePWD parameter 

A more general method is to provide additional connection parameters such as the database name, or a database file, using the RemotePWD field, as follows:

jdbc:sybase:Tds:machine-name:port-number?RemotePWD="dbf=f:\sybase\asademo.db"

By using the database file parameter DBF, you can start a database on a server using jConnect. By default, the database is started with autostop=YES. If you specify a DBF or DBN of utility_db , then the utility database with automatically be started.

For Info     For information on the utility database, see Using the utility database.

Top of page  

Collection Contents Index Using JDBC to access data Creating distributed applications pdf/chap18.pdf