使用derby数据库登录Web应用程序(netbeans)

时间:2017-12-29 16:17:05

标签: java html servlets netbeans derby

我正在尝试使用 derby数据库中的数据登录,我设置但servlet 'loginservlet'似乎没有拿起数据库。我的代码有问题吗?

  

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());           
    }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为参数编号是个问题。你的sql摘录:

  

哪里有电子邮件=?和密码=?

PreparedStatement参数从1开始计算。第一个问号是1,第二个是2,依此类推。所以代码将是:

    ps.setString(1,EMAIL);
    ps.setString(2,PASSWORD);