16.10.2015 Views

Getting Started with WebSphere Application Server

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4 – Working <strong>with</strong> databases 79<br />

2. An editor showing the new JSP file appears as shown in Figure 4.18. Change the title<br />

text to DBDemo.<br />

Figure 4.18 – The generated JSP file<br />

3. In the JSP file, follow these steps to access the data in the database:<br />

a) Import some necessary database access java classes as shown in Listing 4.2.<br />

<br />

<br />

<br />

Listing 4.2 – Import the necessary java classes for database access<br />

b) Initialize a naming context to look up a data source as shown in Listing 4.3. In the<br />

lookup string, java:comp/env/ is the common prefix of the naming space,<br />

jdbc/DataSource is the logical name of the defined data source.<br />

Context initContext = new InitialContext();<br />

DataSource ds =<br />

(DataSource)initContext.lookup("java:comp/env/jdbc/DataSource");<br />

Listing 4.3 - Initializing a naming context and looking up a datasource<br />

c) Open the Web project descriptor file WEB-INF/web.xml, define the logical name<br />

as shown in Listing 4.4.<br />

<br />

jdbc/DataSource<br />

javax.sql.DataSource<br />

Container<br />

Shareable<br />

<br />

Listing 4.4 - The logical name defined in web.xml<br />

Then open WEB-INF/geronimo-web.xml, add some configurations to map the<br />

logical name to the physical name jdbc/demods as shown in Listing 4.5. The

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!