SQL Exception: Before start of result set
843838Feb 15 2006 — edited Feb 15 2006Hello all,
im trying to make a login page using a servlet to connect to mysql database, retrieve email and password, compare these with the text entered in the jsp page and if equal redirect to logged in page else forgot password page, but i keep getting the SQL Exception: Before start of result set error when i enter the email and password. below is my servlet code
package onlinephoto;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class Loginmain extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
/**Initialize global variables*/
public void init() throws ServletException {
}
/**Process the HTTP Get request*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String Email = request.getParameter("txtEmail");
String password=request.getParameter("txtPassword");
String query = "SELECT Customer_Email FROM customer where Customer_Email='"+Email+"' AND Customer_Password='"+password+"'";
String query2 = "SELECT Customer_Password FROM customer where Customer_Password='"+Email+"' AND Customer_Password='"+password+"'";
Connection con = null;
String strURL = "../website/Home.jsp"; // address of JSP page for successful login
String failURL = "../website/ForGotPassword.jsp"; // address of page for unsuccessful login
String resultemail = ("");
String resultPassword = ("");
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost/orgphoto_photosystem
user=orgphoto_admin&password=admin");
Statement stmt = con.createStatement ();
ResultSet rs = stmt.executeQuery (query);
resultemail = rs.getString(1); //Email
rs.close();
ResultSet rs2 = stmt.executeQuery (query2);
resultPassword = rs2.getString(1); // Password
rs.close();
if((resultemail.equals(Email)) && (resultPassword.equals(password))){
// UserID and password submitted by user match up with what's in database.
response.sendRedirect(response.encodeRedirectURL(s trURL));
response.sendRedirect(strURL);
}
else {
response.sendRedirect(response.encodeRedirectURL(f ailURL));
response.sendRedirect(failURL);
}
}
catch (SQLException ex) {
//PrintWriter out = response.getWriter();
response.setContentType("text/html");
while (ex != null) {
out.println ("SQL Exception: " + ex.getMessage ());
ex = ex.getNextException ();
} // end while
} // end catch SQLException
catch (java.lang.Exception ex) {
//PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println ("Exception: " + ex.getMessage ());
}
} // end returnHTML
/**Clean up resources*/
/**Process the HTTP Post request*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
/**Clean up resources*/
public void destroy() {
}
}
any help would be appreciated, thanks mark