JavaSercer Pages 17.4 Developing Application-Specific Database Components The (Top web hosting companies)

JavaSercer Pages 17.4 Developing Application-Specific Database Components The classes described in this chapter can also be used for application-specific components that access a database. Chapter 15 includes one example of an application-specific bean, the EmployeeRegisterBean, that uses the SQLCommandBean to execute its SQL statements. You can also use these classes in your application-specific custom actions. One example is the custom action that’s mentioned in Chapter 9 as an alternative to the generic database actions for inserting or updating employee information: <%@ page language="java" contentType="text/html" %> <%@ taglib uri="/orataglib" prefix="ora" %> <%@ taglib uri="/mytaglib" prefix="myLib" %> <%-- Get the new or updated data from the database --%> SELECT * FROM Employee WHERE UserName = ? <%-- Redirect to the confirmation page --%> Example 17.27 shows one way to implement this custom action. Example 17.27. SaveEmployeeInfoTag Class package com.mycompany.tags; import java.sql.*; import java.text.*; import java.util.Vector; import javax.sql.*; import javax.servlet.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import com.ora.jsp.sql.*; import com.ora.jsp.sql.value.*; import com.ora.jsp.util.*; public class SaveEmployeeInfoTag extends TagSupport { private String dataSourceName; public void setDataSource(String dataSourceName) { this.dataSourceName = dataSourceName; } public int doEndTag( ) throws JspException { // Get all request parameters ServletRequest request = pageContext.getRequest( ); String userName = request.getParameter(”userName”); String password = request.getParameter(”password”); String firstName = request.getParameter(”firstName”); String lastName = request.getParameter(”lastName”); String dept = request.getParameter(”dept”); String empDate = request.getParameter(”empDate”); String emailAddr = request.getParameter(”emailAddr”); if (userName == null || password == null || firstName == null || lastName == null || dept == null || empDate == null || emailAddr == null) { throw new JspException(”Missing a mandatory parameter”); } SQLCommandBean sqlCommandBean = new SQLCommandBean( ); DataSource dataSource = (DataSource) pageContext.getAttribute(dataSourceName, PageContext.APPLICATION_SCOPE); if (dataSource == null) { throw new JspException(”The data source ” + dataSource + ” is not found in the application scope”); } Connection conn = null; try { conn = dataSource.getConnection( ); sqlCommandBean.setConnection(conn); // See if the user exists String sqlValue = “SELECT * FROM Employee WHERE UserName = ?”; page 257

Hint: This post is supported by Gama web hosting php mysql provider

Comments are closed.