User's Guide
PART 3. Java in the Database
CHAPTER 18. Data Access Using JDBC
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 a full description of jConnect and its use with Adaptive Server Anywhere, see the jConnect documentation available in the online Books or from .
Adaptive Server Anywhere provides the following versions of the Sybase jConnect JDBC driver:
Full version If you choose to install jConnect, a jConnect subdirectory is added to your Adaptive Server Anywhere installation. This holds a directory tree with all jConnect files.
Zip file The Remote Data Access features, and the Java debugger, both use jConnect to connect to the database. A zip file of the basic jConnect classes is provided to enable jConnect use even without the full development version of the driver.
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.
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
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.*
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.
Connect to the database from Sybase Central as a user with DBA authority.
Open the Java Objects folder.
Double-click Re-Install jConnect Meta-data Support and follow the instructions in the wizard.
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.
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.
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.
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:
jdbc:sybase:Tds The Sybase jConnect JDBC driver, using the TDS application protocol.
machine-name The IP address or name of the machine on which the server is running. If you are establishing a same-machine connection, you can use localhost, which means the current machine
port number The port number on which the database server is listening. The port number assigned to Adaptive Server Anywhere is 2638, and you should use that number unless there are specific reasons not to do so.
The connection string must be less than 253 characters in length.
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.
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.
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 information on the utility database, see Using the utility database.