Developing Application-Specific Database Components

The SQLCommandBean class described in this chapter can be used for application specific components that access a database. The bean is used like this:

SQLCommandBean sqlCommandBean = new SQLCommandBean(  );
sqlCommandBean.setConnection(dataSource.getConnection(  ));
String sql = "SELECT * FROM Employee WHERE UserName = ?");
sqlCommandBean.setSqlValue(sql);
List values = new ArrayList(  );
values.add(userName);
sqlCommandBean.setValues(values);
Result result = sqlCommandBean.executeQuery(  );

Chapter 18 includes a more advanced example of an application-specific bean (the EmployeeRegisterBean) that uses the SQLCommandBean.

You can also use these classes in your application-specific custom actions. One example is the custom action that’s mentioned in Chapter 11 as an alternative to the generic database actions for inserting or updating employee information:

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%@ taglib prefix="myLib" uri="/mytaglib" %>
                 
               <myLib:saveEmployeeInfo dataSource="example" />
                 
<%-- Get the new or updated data from the database --%>
<sql:query var="newEmpDbInfo" dataSource="${example}" scope="session">
  SELECT * FROM Employee 
    WHERE UserName = ?
  <sql:param value="${param.userName}" />
</sql:query>
  
<%-- Redirect to the confirmation page --%>
<c:redirect url="confirmation.jsp" />

Example 23-8 shows one way to implement this custom action.

Get JavaServer Pages, Second Edition 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.