package java4s;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class OnServletLogin extends HttpServlet {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/STUDENTS";
static final String USER = "root";
static final String PASS = "";
protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Database..");
stmt = conn.createStatement();
String sql = "SELECT id, userId, password FROM login";
ResultSet rs = stmt.executeQuery(sql);
PrintWriter pw = res.getWriter();
res.setContentType("text/html");
String user = req.getParameter("userName");
String pass = req.getParameter("userPassword");
pw.print("<font face='verdana'>");
if (user.equals("select * from login where userId="+user+"and password"+pass) )
pw.println("Login Success...!");
else
pw.println("Login Failed...!");
pw.print("</font>");
pw.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
嗨,这是我的代码,我正在尝试从数据库实现登录页面,但我无法从数据库数据中输入登录逻辑。我创建login
了一个有 2 个字段的表user id
,password.
但是如何检索和获取以便我可以启用登录?