IBM Version 5 Executing the XMLToSQL class, Example 11-3 CustomerXSLServlet doRegister method

Models: Version 5

1 340
Download 340 pages 15.44 Kb
Page 283
Image 283

Or, you can use JAXP to change the document structure dynamically. Using JAXP following the code fragment does this. If this document’s root element is not SQLResult, add SQLResult and add the first child (in our case, it is customer), to the SQLResult element. At this time, the customer element was removed from the root and be a child of the SQLResult. Then add the SQLResult to the document, SQLResult will be a root and customer will be a first child:

if( !(doc.getFirstChild().getNodeName().equalsIgnoreCase("SQLResult")) ){ Element rootElement = doc.createElement("SQLResult"); rootElement.appendChild(doc.getFirstChild()); doc.appendChild(rootElement);

}

We introduce a wrapper of XMLToSQL to resolve this and add a capability of the datasource.

Executing the XMLToSQL class

We created doRegister method to instantiate CustomerXSL, get an XML Document, and execute XMLToSQL in one method (Example 11-3). Since XMLToSQL can throw an exception, we need to catch them. This sample shows if SQLException has been thrown, the member ship already exists.

This method returns a true if it succeeds, false if it fails.

Example 11-3 CustomerXSLServlet doRegister method

private boolean doRegister(HttpServletRequest request){ CustomerXML xml = getCustomerXML();

xml.setFirstname(new java.lang.String(request.getParameter("firstname"))); xml.setLastname(new java.lang.String(request.getParameter("lastname"))); xml.setEmail(new java.lang.String(request.getParameter("email"))); xml.setMembership(new

java.lang.String(request.getParameter("membership")));

try{

xml2sql.execute(xml.produceDOMDocumentforTools(),false); }catch(SQLException sqlexception){ System.out.println(sqlexception); xml.setMembership("Membership is already exist");

return false;

}catch(Exception exception){ System.out.println(exception);

return false;

}

return true;

}

Chapter 11. Light weight XML-based Enterprise Application 267

Page 283
Image 283
IBM Version 5 manual Executing the XMLToSQL class, Example 11-3 CustomerXSLServlet doRegister method