JDBC Type 4 Driver Programmer's Reference for SQL/MX Release 3.2.1
Retrieving a DataSource Instance by using JNDI and to the Data Source
Typically, the JDBC application looks up the data source JNDI name from a context object. After
the application has the DataSource object, the application makes a getConnection() call
on the data source and gets a connection.
To connect to and use the data source associated with the SQL/MX database, the JDBC application
performs the following steps are listed together with the application code to perform the operation:
1. Import the packages.
import javax.naming.*;
import java.sql.*;
import javax.sql.DataSource;
2. Create the initial context.
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
try
{
Context ctx = new InitialContext(env);
} catch( ... ) { ... }
3. Look up the JNDI name associated with the data source myDataSource, where
myDataSource is the logical name that will be associated with the real world data source
– MXCS server.
DataSource ds = (DataSource)ctx.lookup("myDataSource");
4. Using the data source, create the connection.
con = ds.getConnection();
5. Do work with the connection. The following statements are an example:
stmt = con.createStatement();
try
{
stmt.executeUpdate("drop table tdata");
}
catch (SQLException e) {}
Specifying the Properties File that Configures the Data Source
To use the properties file method to configure a DataSource object, the properties file must exist
on disk and contain the property_name=property_value pairs that configure the data source.
See “Creating and Using a Properties File” (page 37) for more information about creating this file.
When the JDBC application makes the connection, the application should pass the properties file
as a command-line parameter:
java -Dt4sqlmx.properties=file_location ...
All the above said properties and configuration are applicable to the SQLMX XADataSource objects
participating in distributed transactions.
Connecting using the DriverManager Class
“Loading and Registering the Driver” (page 24)
“Establishing the Connection” (page 24)
“Guidelines for connecting with the Driver Manager” (page 24)
The java.sql.DriverManager class is widely used to get a connection, but is less portable
than the DataSource class. The DriverManager class works with the Driver interface to manage
Connecting to SQL/MX 23