Switch to standard view
Sybase logo  
Sybase logo  
Products | About Sybase | Support



 
1. In order to connect to SQL Anywhere via jConnect, you must run the SQL Anywhere Open Server Gateway DBOS50.EXE

You should be using Open Server Gateway version 5.5.03 or later and Open Server drivers (DLL's) version 11.1 or later.

Refer to Technical Document 44541
"Installing and Configuring Open Server Gateway for SQL Anywhere with NT and 95"
http://my.sybase.com/detail?id=44541

Refer to Technical Document 44542
"Installing and Configuring Open Server Gateway for SQL Anywhere with Novell Netware 3.x and 4.x"
http://my.sybase.com/detail?id=44542

2. Use the SQLEDIT utility to add an entry for the gateway. The entry that you create will be added to the %SYBASE%\ini\sql.ini file. (The newer DSEDIT utility, which has a different user interface, can also be used to create the required entry in your SQL.INI file.)

 
 

3. Basic Steps for writing Java applications using jConnect

Load the driver
Open a connection to the RDBMS
Create a statement
Execute the query
Process the result set(s)
Close the connection

4. Loading the jConnect driver

The DriverManager class is used for loading the driver

Import the following packages;

import java.io.*;
import java.sql.*;
import java.util.*;

There are two ways to load the jConnect driver:

Instantiate the class. (This is the recommended if your application only uses one jdbc driver).

// Load the driver
DriverManager.registerDriver(
(Driver) Class.forName( "com.sybase.jdbc.SybDriver" ).newInstance() );

Add the jConnect driver to the System Property jdbc.drivers. (This enables multiple driver classnames).

1.Adding the driver directly in your code:
Properties sysProps = System.getProperties();
StringBuffer drivers = new StringBuffer("com.sybase.jdbc.SybDriver");
String oldDrivers = sysProps.getProperty("jdbc.drivers");
if (oldDrivers != null)
drivers.append(":" + oldDrivers);
sysProps.put("jdbc.drivers", drivers.toString());
2.Adding the driver at runtime:
java -Djdbc.drivers="com.sybase.jdbc.SybDriver" jdbc_driver

Prior to making a connection to the database, properties for each driver and database connection need to be set.
The property names and types are jdbc driver specific.
For a list of jConnect connection properties refer to the "Sybase® jConnect(TM) for JDBC(TM) Programmer's Reference "
Chapter 2. Programming Information  Setting Driver Connection Properties Table 2-2
This reference manual is available in PDF format from http://sybooks.sybase.com/jc.html
 

5. Opening a connection to the database

In order to connect to a database, you must supply a JDBC URL and set connection properties. The basic properties that need to be set are the user ID and password to access the database.

A connection object is used for access to the database

DriverManager.getConnection()

(1) requires a jdbc URL
(2) each driver that is registered is checked to find one to service the URL

Connection con = DriverManager.getConnection(URL);

The format of a jdbc URL is: jdbc:<subprotocol>:<subname>
jdbc is the protocol
<subprotocol> is the driver
<subname> is the way to identify the database

jConnect supports 5 forms of URL syntax

jdbc:sybase:Tds:host:port
jdbc:sybase:Tds:host:port/
jdbc:sybase:Tds:host:port/database
jdbc:sybase:Tds:host:port/database?property=value[?property=value]
jdbc:sybase:Tds:host:port?property=value[?property=value]

Example: jdbc:sybase:Tds:machineName:1498/SADEMO_OSG

The sybase subprotocol has been registered with JavaSoft.

6. Closing a connection

A connection can be closed by using the Connection.close() method.

con.close();

7. A simple sample connection using the demo database, sademo.db:

import java.io.*;
import java.sql.*;
import java.util.*;

class sademoSample {

// Declare and initialize connection property variables
static String url = "jdbc:sybase:Tds:machineName:1498/SADEMO_OSG";
static String uid = "dba";
static String pwd = "sql";

public static void main ( String[] args ) {

// Load the driver
DriverManager.registerDriver(
(Driver) Class.forName( "com.sybase.jdbc.SybDriver" ).newInstance() );

// Set connection properties
Properties properties = new Properties();
properties.put( "user", uid );
properties.put( "password", pwd );

// Open the connection
Connection con = DriverManager.getConnection( url, properties );

// Close the connection
con.close();

}
}
// End of sademoSample.java

8. Accessing Database Metadata

In order to support JDBC DatabaseMetaData methods, Sybase provides a set of stored procedures that jConnect can call for metadata about a database.
These stored procedures must be installed on the server for the JDBC metadata methods to work.

If the stored procedures for providing metadata are not already installed in a Sybase server, you can install them using one of two stored procedure scripts provided with jConnect:

sql_anywhere.sql installs stored procedures on a SQL Anywhere database.
sql_server.sql installs stored procedures on an Adaptive Server Enterprise database.

Refer to the jConnect for JDBC Installation Guide  for complete instructions on installing stored procedures.

In addition, to use the metadata methods, the USE_METADATA connection property must be set to "true" (its default value) when you establish a connection.

Notes:
You cannot get metadata about temporary tables in a database.

The DatabaseMetaData.getPrimaryKeys() method finds primary keys declared in a table definition (CREATE TABLE) or with alter table
(ALTER TABLE ADD CONSTRAINT). It does not find keys defined using sp_primarykey.
 
 



[#]Home  [*]Top

© Copyright 2008, Sybase Inc.