Opening a Connection to a Database


There are several ways to connect to Oracle databases such as Connecting to Oracle Database. One of the methods is to open a connection to the database with the static getConnection method of the JDBC DriverManager class.

The JDBC 2.0 extension API introduced the concept of data sources, which are standard, general-use objects for specifying databases or other resources to use.

Data sources can optionally be bound to Java Naming and Directory Interface (JNDI) entities so that you can access databases by logical names, for convenience and portability. The data source facility provides a complete replacement for the previous JDBC DriverManager facility. For example, the following code uses the OracleDataSource to connect to our Oracle database:
  OracleDataSource  ods = new OracleDataSource( );
  String database = "65.52.222.73:1521/cdb1";
  ods.setURL     ( "jdbc:oracle:thin:@" + database );
  ods.setUser    ( "userid" );
  ods.setPassword( "password" );
  Connection  conn = ods.getConnection( );
A connection pool is a cache of database connections maintained in memory so that the connections can be reused when the database receives future requests for data. Whenever you retrieve a connection through an OracleDataSource.getConnection, the JDBC drivers check to see if a connection is available in the cache. If a match is found, a logical connection is returned wrapping the physical connection. If no physical connection match is found, a new physical connection is created, wrapped in a logical connection, and returned.




      Apparently, someone in London gets stabbed every 52 seconds.    
      Poor bastard.