Connecting to databases when using inline methods

You can use java.sql.Connection objects or javax.sql.DataSource objects to connect to databases.

Connections to databases through Connection objects

You can use Connection objects to connect to a JDBC-compliant database. When you create an instance of an implementation of the Data interface, you pass a Connection object to the overloaded DataFactory.getData() method.

For example, you could create a new Data object with application logic that is similar to this:

Connection con = DriverManager.getConnection(...); 
Data data = DataFactory.getData(con);

You can use all of the methods in the Data object.

The Data interface supports the following JDBC methods on Connection objects:

Connections to databases through JDBC DataSource objects

You can use JDBC DataSource objects to connect to a JDBC-compliant database. When you create an instance of an implementation of the Data interface, you pass a DataSource object to the overloaded DataFactory.getData() method.

For example, you could create a new Data object with application logic that is similar to this:

import javax.naming.*; 
import javax.sql.*; 
... 
Context ctx=new InitialContext(); 
DataSource ds=(DataSource)ctx.lookup("...");

Data data = DataFactory.getData(ds);

The DataFactory creates a java.sql.Connection object, then creates a Data object which uses the Connection to access the underlying data store. If the application then needs the implicitly-created Connection, for example to modify a Connection property, it can acquire a reference to it using the getConnection() method of the Data object.


Feedback