The validate method validates a User Id specified in the user
registration entry form. The return type of the validate method is
boolean. In the validate method, first a connection with MySQL
database is obtained using datasource configured in JBoss.
InitialContext initialContext = new
InitialContext();
javax.sql.DataSource ds =
(javax.sql.DataSource)initialContext.lookup(???java:My
SqlDS???);
java.sql.Connection conn = ds.getConnection();
A SQL query is run in the MySQL database using the User Id specified
in the user registration entry form.
Statement stmt = conn.createStatement();
String query =
???SELECT * from UserTable WHERE userId=??? +
?????™??? + userId + ?????™???;
ResultSet rs = stmt.executeQuery(query);
If the result set is not empty the User Id specified is already defined in
the database and is therefore not valid. The business logic of what is a
98 5 Ajax with Java-DWR
valid user id may contain match with a regular expression pattern. We
have used the business logic that if a user id is not already defined the user
id is valid. If the result set has data set the value of a boolean variable to
false.
if (rs.next()) {
valid = false;
return valid;
}
If the result set is empty the User Id is valid and a new user registration
entry may be created. Return true if the result set is empty. If the User Id
is valid the updateUserTable method is invoked to create a new user
entry.
Pages:
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100