JDBC 2.0 Standard Extension API
Because connection strings are vendor-specific, Java specifies a DataSource interface. It takes advantage of JNDI, which functions as a Java registry. The DataSource interface allows JDBC developers to create named databases. As a developer, you register the database in JNDI along with the vendor-specific driver information. Then, your JDBC applications can be completely database agnostic and be "pure JDBC."
The PSQL JDBC driver supports the JDBC 2.0 Standard Extension API. Currently, the PSQL JDBC driver supports the following interfaces
*Note: These interfaces are packaged separately in pvjdbc2x.jar in order to keep the core JDBC API 100% pure java.
Although at this time PSQL does not provide implementation of RowSet interfaces, PSQL JDBC driver has been tested with Oracle’s implementation of RowSet interface.
DataSource
Java provides a way for application developers to write applications that are driver independent. By using the DataSource interface and JNDI, applications can access data using standard methods and eliminate driver specific elements such as connection strings. In order to use DataSource interface, a database has to be registered with a JNDI service provider. An application can then access it by name.
The following is an example of using the DataSource interface:
// this code will have to be executed by the
// administrator in order to register the
// DataSource.
// This sample uses Oaracle’s reference JNDI
// implementation
 
public void registerDataSources()
{
// this example uses the JNDI file system
// object as its registry
 
Context ctx;
jndiDir = "c:\\jndi";
try
{
Hashtable env = new Hashtable (5);
env.put (Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL, jndiDir);
ctx = new InitialContext(env);
}
catch (Exception e)
{
System.out.println(e.toString());
}
//register demodata as regular data source
com.pervasive.jdbc.v2.DataSource ds = new com.pervasive.jdbc.v2.DataSource();
String dsName = "";
 
try
{
// Set the user name, password, driver type and network protocol
ds.setUser("administrator");
ds.setPassword("admin");
ds.setPortNumber("1583");
ds.setDatabaseName("DEMODATA");
ds.setServerName("127.0.0.1");
ds.setDataSourceName("DEMODATA_DATA_SOURCE");
ds.setEncoding("cp850");
dsName = "jdbc/demodata";
 
// Bind it
try
{
ctx.bind(dsName,ds);
System.out.println("Bound data source [" + dsName + "]");
}
catch (NameAlreadyBoundException ne)
{
System.out.println("Data source [" + dsName + "] already bound");
}
catch (Throwable e)
{
System.out.println("Error in JNDI binding occurred:");
throw new Exception(e.toString());
}
}
}
}
 
//in order to use this DataSource in application the following code needs to be executed
 
public DataSource lookupDataSource(String ln) throws SQLException
{
Object ods = null;
Context ctx;
 
try
{
Hashtable env = new Hashtable (5);
env.put (Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
// this will create the jndi directory
// and return its name
// if the directory does not already exist
String jndiDir = "c:\\jndi";
env.put(Context.PROVIDER_URL, jndiDir);
ctx = new InitialContext(env);
}
catch (Exception e)
{
System.out.println(e.toString());
}
try
{
ods = ctx.lookup(ln);
if (ods != null)
System.out.println("Found data source [" + ln + "]");
else
System.out.println("Could not find data source [" + ln + "]");
}
catch (Exception e)
{
throw new SQLException(e.toString());
}
 
return (DataSource)ods;
}
 
// note that ConnectionPoolDataSource is
// handled similarly.