0
我有一個JSP登錄頁面,我在其中輸入用戶名和密碼,並將它們傳遞給我的servlet,該servlet使用psaviour數據庫中的用戶表驗證它們,然後成功或失敗時,應將其轉發到歡迎頁面或返回登錄頁面。登錄JSP和servlet連接到數據庫
然而,我試圖從我的checklogin servlet連接到我的數據庫。
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/checkLogin"})
public class checkLogin extends HttpServlet {
private final String DB_URL = "jdbc:mysql://localhost:3306/psaviour";
private final String JDBC_DRIVER="com.mysql.jdbc.Driver";
private final String USER = "root";
private final String PASS = "root";
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");//.toString();
String passkey = request.getParameter("passkey");//.toString();
try {
Class.forName(JDBC_DRIVER);
try {
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt=conn.prepareStatement("SELECT * FROM users WHERE 'username' =
? AND 'passkey' = ?");
stmt.setString(1, username);
stmt.setString(2, passkey);
rs = stmt.executeQuery();
} catch (SQLException e){
e.printStackTrace();
} catch (ClassNotFoundException ce){
ce.printStackTrace();
}
}
while (rs.next()){
//need to point to my welcome.jsp page or
}
//otherwise reload login page....
}
}