JDBC Type 4 Driver 2.0 Programmer's Reference (SQL/MX 2.x)
// enabled.
ds.setMaxStatements("7000");
Programmatically Registering the DataSource Object
In the following example, the code shows how to register, programmatically, the SQLMXDataSource object ds that was
created using the preceding code with JDNI.
java.util.Hashtable env = new java.util.Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "Factory class name here");
javax.naming.Context ctx = new javax.naming.InitialContext(env);
ctx.rebind("myDataSource", ds);
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. Once the application has the
DataSource object, the application does a getConnection() call on the data source and gets a connection.
The steps that JDBC application does to connect to and use the data source associated with the SQL/MX database are listed
below together with the application code to perform the operation.
Import the packages.
import javax.naming.*;
import java.sql.*;
import javax.sql.DataSource;
1.
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( ... ) { ... }
2.
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");
3.
Create the connection using the data source.
con = ds.getConnection();
4.
Do work with the connection. The following statements are just a simple example.
stmt = con.createStatement();
try
{
stmt.executeUpdate("drop table tdata");
}
catch (SQLException e) {}
5.
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 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:










