I'm trying to log in using data from a derby database , I set up but the servlet 'loginservlet'
doesn't seem to be picking up the database. Is there a problem with my code?
Code from the loginservlet:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
/**
*
* @author billemerson
*/
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/HOUSEPLANTSIRELAND","houseplant","pass");
PreparedStatement ps = conn.prepareStatement("SELECT * FROM HOUSEPLANT.USERDATA WHERE EMAIL=? AND PASSWORD=?");
String EMAIL = req.getParameter("email");
String PASSWORD = req.getParameter("password");
ps.setString(4,EMAIL);
ps.setString(5,PASSWORD);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
res.sendRedirect("adminhome.jsp");
}
else {
res.sendRedirect("index.html");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
I think the parameter number is the problem. Your sql excerpt:
WHERE EMAIL=? AND PASSWORD=?
PreparedStatement
parameters are counted from 1. The first question mark is 1, second is 2, and so on. So the code will be:
ps.setString(1,EMAIL);
ps.setString(2,PASSWORD);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.