DriverManager has one significant drawback: It requires hard-coding vendor options and details about the database and server configurations. To use the code on another system, to change the database, or to change username or password, etc., we may need to edit the source code, recompile, and retest. There are several approaches that we can take to minimize this problem. One option is to put these specifics in a properties file or a configuration file that is read at startup time, perhaps using XML or a Java ResourceBundle. These minimize but don't solve the problem. In JDBC 2.0, Sun introduced an alternative with a more far-reaching approach, the DataSource class. DataSource allows our code to locate and connect to a resource, such as a database, by using a logical name. This can make our code more portable and easier to maintain. It also allows us to encapsulate some of the connection details at a higher-level using a datasource is necessary, in fact, if we want to use the connection pooling and distributed transactions that application servers can provide. The mapping of the logical name to a specific resource is intended to be managed at the higher level, outside of the application, by a naming service such as Java Naming and Directory Interface (JDNI). Using DataSource with JNDI is simple, assuming that you have a naming service set up already. (This may be provided by an application server, for example.) The naming service will allow you to associate a database URL with a logical name, using the following steps: Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("jdbc/logical_database_name"); Connection con = ds.getConnection(username, password); It is also possible to use DataSource without JNDI and set the DataSource properties in our code. This is essentially equivalent to using DriverManager. We lose the advantages of portability and maintainability, and gain verbosity. Nonetheless, because Sun may deprecate DriverManager at some time in the future, it's worth at least considering using DataSource anyway, especially if we are writing an application that will eventually use JNDI. Here are three steps we need to use DataSource without JNDI:
Importing and Using DataSource ClassThe first two steps are straightforward. At the top of the file, we import the class. import oracle.jdbc.pool.OracleDataSource; In the body of the code, we instantiate the class. OracleDataSource ods = new OracleDataSource(); Setting DataSource PropertiesDataSource has getter/setter methods to manage properties, which follow the convention used for JavaBeans. For any property xyz, the method to get its current value is called getXyz(), and the method to set it is called setXyz(). The properties we need to set in order to log into an Oracle database are.
Here is the code to set them: ods.setDriverType("thin"); ods.setServerName("noizmaker"); ods.setNetworkProtocol("tcp"); ods.setDatabaseName("osiris"); ods.setPortNumber(1521); ods.setUser("scott"); ods.setPassword("tiger"); Here is a sample program for connecting, using DataSource without JNDI: // TestDataSource.java Load JDBC and connect to database import java.sql.*; import oracle.jdbc.driver.*; import oracle.jdbc.pool.OracleDataSource; public class TestDataSource { public static void main(String [] vars) { Connection conn; try { OracleDataSource ods = new OracleDataSource(); ods.setDriverType("thin"); ods.setServerName("noizmaker"); ods.setNetworkProtocol("tcp"); ods.setDatabaseName("osiris"); ods.setPortNumber(1521); ods.setUser("david"); ods.setPassword("bigcat"); conn = ods.getConnection(); catch (SQLException e) } catch(SQLException e) { System.out.println("Caught: " + e); System.exit(1); } } } In the remainder of this chapter we'll use the DataSource method to connect using the thin driver. |