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 19 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 12 as an alternative to the generic database actions for inserting or updating employee information:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="myLib" uri="mytaglib" %>
  
               <myLib:saveEmployeeInfo dataSource="${appDataSource}" />
  
<%-- Get the new or updated data from the database --%>
<sql:query var="newEmpDbInfo"="${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 24-8 shows one way to implement this custom action.

Example 24-8. SaveEmployeeInfoTag ...

Get JavaServer Pages, 3rd 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.