Connecting to a Data Source

Applications must first be connected to a database before performing any database operation. As explained earlier, connections are made ready by the container, at startup, through the creation of a connection pool.

Connections are created using the DataSource object. Applications need to locate a DataSource object, before creating a Connection. Applications locate a DataSource through the JNDI service. The following is an example of these steps:

 // Connect to the default JNDI service and establish a context Context ctx = new InitialContext(); // Lookup the DataSource for the configured database javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup ("java:comp/env/jdbc/styejbDB"); // Make a connection to the ...

Get Sams Teach Yourself EJB in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.