Access by Client

A JDBC client application allows you to access the server name to obtain a reference to a DataSource object, instead of accessing the DriverManager and providing a JDBC URL.

This is a typical JDBC client application. Once you obtain the connection, the client code is identical to any other JDBC client code. The code is generic and references Sybase only when setting the object factory property, which you can do as part of the environment setup.

The jConnect installation contains the sample program sample2/SimpleDataSource.java to illustrate the use of DataSource. This sample is provided for reference only, that is, you cannot run the sample unless you configure your environment and edit the sample appropriately. SimpleDataSource.java contains the following critical code:

import javax.naming.*;
import javax.sql.*;
import java.sql.*;
// set necessary JNDI properties for your environment (same as above)
Properties jndiProps = new Properties();
// used by JNDI to build the SybDataSource
jndiProps.put(Context.OBJECT_FACTORIES,
    "com.sybase.jdbc4.jdbc.SybObjectFactory");
// nameserver that JNDI should talk to
jndiProps.put(Context.PROVIDER_URL, "ldap: some_ldap_server:238/" + "o=MyCompany,c=Us");
// used by JNDI to establish the naming context
jndiProps.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.ldap.LdapCtxFactory");
// obtain a connection to your name server
Context ctx = new InitialContext(jndiProps);
DataSource ds = (DataSource) ctx.lookup("servername=myASE");
// obtains a connection to the server as configured earlier.
// in this case, the default username and password will be used
Connection conn = ds.getConnection();
// do standard JDBC methods
...

You need not explicitly pass the Properties to the InitialContext constructor if the properties have already been defined within the virtual machine, that is, passed when Java was either set as part of the browser properties, or by using:

java -Djava.naming.object.factory=com.sybase.jdbc4.jdbc.SybObjectFactory

See your Java VM documentation for more information about setting environment properties.