由于某种原因,我无法将项目检索/提交到本地数据库中。提交信息后,包含Servlet的下一页为空白。我确实有一个测试类来测试数据库的连接,并且它能够成功找到数据库中的项目/将项目提交到数据库中。
我不太清楚问题可能是什么,可能是到我的数据库或sql的连接。寻找任何可能的帮助-谢谢!
同样,您会看到,玻璃鱼说在搜索用户之前,“ Here1”之后出现了第一个问题。
下面是登录控制器的代码:
package Servlets;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import beans.*;
import dao.*;
public class LoginController extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Validator valid = new Validator();
String email = request.getParameter("email");
String password = request.getParameter("password");
// Date date = new Date();
long millis=System.currentTimeMillis();
java.sql.Date date=new java.sql.Date(millis);
java.sql.Time time=new java.sql.Time(millis);
DBManager manager = (DBManager) session.getAttribute("manager");
User user = null;
if (!valid.validatePassword(password)){
session.setAttribute("passErr", "Error: password format incorrect");
request.getRequestDispatcher("login.jsp").include(request, response);
}
else{
try {
System.out.println("Here1");
user = manager.findUser(email, password);
int logid = manager.logCount()+1;
System.out.println("Here2");
if (user!=null){
if(user.getAccounttype().equals("Staff")){
session.setAttribute("staff", "True");
}
System.out.println("Here3");
session.setAttribute("loggedin", "True");
session.setAttribute("logid", logid);
session.setAttribute("user", user);
manager.loginTimestamp(logid, user.getID(), date.toString(), time.toString());
request.getRequestDispatcher("welcome.jsp").include(request, response);
System.out.println("Here4");
}
else{
System.out.println("Here5");
session.setAttribute("existErr", "User does not exist");
request.getRequestDispatcher("login.jsp").include(request, response);
}
} catch (SQLException | NullPointerException ex) {
System.out.println(ex.getMessage()==null ? "User does not exist" : "welcome");
}
}
}
}
还有我用于数据库管理器的其他代码是:
public class DBManager {
private Statement st;
public DBManager(Connection conn) throws SQLException {
st = conn.createStatement();
}
//Find user by email and password in the database
public User findUser(String email, String password) throws SQLException {
String fetch = "select * from ISDUSER.USERS where EMAIL = '" + email + "' and PASSWORD='" + password + "'";
ResultSet rs = st.executeQuery(fetch);
while(rs.next()){
String useremail = rs.getString(3);
String pass = rs.getString(4);
if (useremail.equals(email) && pass.equals(password)){
int id = rs.getInt(1);
String name = rs.getString(2);
String address = rs.getString(5);
String type = rs.getString(7);
System.out.println("here + " + type);
boolean isRegistered = rs.getBoolean(6);
return new User(id, name, email, pass, address, isRegistered, type);
}
}
return null;
}
您必须找出连接数据库的方法。您必须提供数据库的IP,端口,用户名,密码。如果没有,如何连接?我认为互联网上有很多资源可以帮助您解决此问题