Java Reference
In-Depth Information
String nextPage = "MemberWelcome.jsp";
if(ProcessNABean.insertData()){
nextPage = "MemberProfile.jsp";
}else{
nextPage = "NewMemberForm.jsp";
}
%>
<jsp:forward page="<%=nextPage%>"/>
Operation of the ProcessNABean
The first part of the
ProcessNABean
is the collection of getter and setter methods required to access
the bean's parameters. These must be supplied for the bean introspection that the JSP engine requires
to work properly.
The real work is done in the
insertData()
method. The
ProcessNABean
makes extensive use of a
CallableStatement
object,
cs
. First it calls the stored procedure
GET_LOGIN_FOR_USER
to validate
the username against the Login table. If the username is already in use, the boolean flag
username_selection_ok
is set to false so that the JSP page can notify the user that he or she
needs to select a different username.
Once the user has selected a valid, unique username, the
CallableStatement
object is used to call
the stored procedure
SET_LOGIN_FOR_USER
to update the Login table with the new username and
password. The stored procedure
SET_LOGIN_FOR_USER
is defined as follows:
CREATE PROCEDURE SET_LOGIN_FOR_USER
@USERNAME VARCHAR(20),
@PASSWORD VARCHAR(20)
AS
INSERT INTO LOGIN (USERNAME, PASSWORD)
VALUES (@USERNAME, @PASSWORD);
The stored procedure
GET_LOGIN_FOR_USER
is then called again to get the auto generated MemberID
assigned to this user. A more elegant way to do this is to use the
getGeneratedKeys()
method
defined in JDBC 3.0 for the
Statement
object as shown here:
if(cs.executeUpdate()!=1)ok = false;
Result rs = cs.getGeneratedKeys();
Cross-
Reference
The use of the JDBC 3.0 extension method
Statement.getGeneratedKeys()
is discussed in
Chapter 4
.
Finally, the stored procedure INSERT_CONTACT_INFO is called to insert the member data stored in
the
ProcessNABean
.
The code for the
ProcessNABean
is shown in
Listing 13-7
.
Listing 13-7: Calling a stored procedure from a JavaBean
package JavaDatabaseBible.ch13;
import java.sql.*;
import javax.sql.*;