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.

80 <strong>Getting</strong> <strong>Started</strong> <strong>with</strong> <strong>WebSphere</strong> <strong>Application</strong> <strong>Server</strong> Community Edition<br />

physical name is the one used in Section 4.2.2.2. The mapping makes application<br />

deployment flexible.<br />

<br />

jdbc/DataSource<br />

jdbc/demods<br />

<br />

Listing 4.5 - Mapping the logical name to the physical name in geronimo-web.xml<br />

d) Back to the JSP file, add some code to get a database connection and then<br />

retrieve data from the database. This is shown in Listing 4.6. There are inline<br />

comments to explain the code.<br />

Connection con = null;<br />

Statement stmt = null;<br />

// Get a database connection<br />

con = ds.getConnection();<br />

// Prepare a statement object used to execute query<br />

stmt = con.createStatement();<br />

String sql = "SELECT empID, empName FROM employees " +<br />

"WHERE XMLEXISTS('$i/employee[age > \"25\"]'" +<br />

"PASSING empDOC AS \"i\")";<br />

// Fill returned data into ResultSet object.<br />

ResultSet rs = stmt.executeQuery(sql);<br />

Listing 4.6 - Code snippet to get database connection and query data<br />

e) After you retrieve data and store it in a ResultSet object, you need to present them<br />

properly using HTML as shown in Listing 4.7.<br />

Employees who are older than 25 years old are:<br />

<br />

<br />

<br />

Employee ID<br />

Employee Name<br />

<br />

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

Saved successfully!

Ooh no, something went wrong!