sorry, my fault, the code should have had extra braces in which I have obviously not caught. Try this, to which 've added some lines you should take out, probably everything from the start to the try { line. The URL that is accessing this page should have a
?MM_insert=true
after the page name, is this the case?
<%@page import="java.sql.ResultSet, java.sql.PreparedStatement, java.sql.Driver, java.sql.Connection, java.sql.DriverManager"%>
<html><%
String MM_LearningSite_STRING = "jdbc
dbc:mysite"; // testing
String MM_LearningSite_DRIVER = "com.sun.odbc.jdbc.OdbcJdbcDriver";
String MM_LearningSite_USERNAME = "";
String MM_LearningSite_PASSWORD = "";
%>About enter try block<%
try {
// *** Redirect if username exists
String MM_flag="MM_insert";
if (request.getParameter(MM_flag) != null) {
// if it exists, continue
// register is where the user is sent if they have chosen a duplicate username
String MM_dupKeyRedirect="register.jsp";
// rs key connection is the connection string to your database (whats the database btw?)
String MM_rsKeyConnection=MM_LearningSite_STRING;
// look for the userid (username) in the request
String MM_dupKeyUsernameValue = request.getParameter("userid");
%>About to construct sql<%
// search for that userid in the database
String MM_dupKeySQL = "SELECT USERID FROM IMAATDBA.STUDENT_DATA WHERE USERID='" + MM_dupKeyUsernameValue + "'";
/*
nothing should go wrong up until this point
Its a bit wasteful not to check that the user id
retrieved from the request is not null, because it means connecting to the database for nothing. I would add a check for userid.equals("") and userid != null
from this point on, is where the problem might be
*/
%>About to create driver<%
// instantiate the db driver
Driver MM_rsKeyDriver = (Driver)Class.forName(MM_LearningSite_DRIVER).newInstance();
%>Created driver<%
// create a connection
Connection MM_rsKeyConn = DriverManager.getConnection(MM_rsKeyConnection,MM_LearningSite_USERNAME,MM_LearningSite_PASSWORD);
// is it worth checking the connection was correctly created?
%>Created a connection to the database.<%
// prepare and execute the query
PreparedStatement MM_rsKeyStatement = MM_rsKeyConn.prepareStatement(MM_dupKeySQL);
%>Created the prepared statement<%
ResultSet MM_rsKey = MM_rsKeyStatement.executeQuery();
%>Executed the query<%
// if there are an results in the set then its not empty and there is a user with that id
boolean MM_rsKey_isEmpty = !MM_rsKey.next();
%>Just called next() on the result set. About to close result set<%
MM_rsKey.close(); // Close the recordset - we have all the info we need.
// extra line of code...
// if the connection is not closed
if(!MM_rsKeyConn.isClosed()) {
// close it
%>About to close connection<%
MM_rsKeyConn.close();
}
%>Closed connection. Nothing should go wrong from here on<%
if (!MM_rsKey_isEmpty) {
// the username was found - can not add the requested username
String MM_qsChar = "?";
if (MM_dupKeyRedirect.indexOf("?") >= 0) MM_qsChar = "&";
MM_dupKeyRedirect = MM_dupKeyRedirect + MM_qsChar + "requsername=" + MM_dupKeyUsernameValue;
response.sendRedirect(response.encodeRedirectURL(MM_dupKeyRedirect));
}
}
} catch (Exception e) {
%>An exception occurred.<%=e.getMessage()%><%
}%>
</html>