User
public class User {
public User(){
}
public User(int id,String username,String password){
this.id = id;
this.username = username;
this.password = password;
}
public User(String username,String password){
this.username = username;
this.password = password;
}
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
UserDao
public class UserDao {
public List<User> findAll(){
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
List<User> userList = new ArrayList<>();
try{
connection= DBUtil.getConnection();
//写sql语句
String sql ="select * from tb_user";
//获得statement对象
statement=connection.prepareStatement(sql);
//执行sql得到结果集
resultSet = statement.executeQuery();
//处理结果集
while (resultSet.next()){
User user = new User();
user.setId(resultSet.getInt(1));
user.setUsername(resultSet.getString(2));
user.setPassword(resultSet.getString(3));
userList.add(user);
}
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException throwables){
throwables.printStackTrace();
}finally {
try{
DBUtil.closeAll(resultSet,statement,connection);
}catch (SQLException throwsable){
throwsable.printStackTrace();
}
}
return userList;
}
public void deleteByname(String username){
Connection connection = null;
PreparedStatement statement = null;
try{
connection= DBUtil.getConnection();
//写sql语句
String sql ="delete from tb_user where username=?";
//获得statement对象
statement=connection.prepareStatement(sql);
statement.setString(1,username);
//执行sql得到结果集
statement.executeUpdate();
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException throwsable){
throwsable.printStackTrace();
}finally {
//关闭资源
try{
DBUtil.closeAll(null,statement,connection);
}catch (SQLException throwsable){
throwsable.printStackTrace();
}
}
}
public void insert(String username,String password){
Connection connection = null;
PreparedStatement statement = null;
try{
connection= DBUtil.getConnection();
//写sql语句
String sql ="insert into tb_user(username,password) values(?,?)";
//获得statement对象
statement=connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2,password);
//处理结果集
statement.executeUpdate();
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException throwsable){
throwsable.printStackTrace();
}finally {
//关闭资源
try{
DBUtil.closeAll(null,statement,connection);
}catch (SQLException throwsable){
throwsable.printStackTrace();
}
}
}
public void update(String password,String username){
Connection connection = null;
PreparedStatement statement = null;
try{
connection= DBUtil.getConnection();
//写sql语句
String sql ="update tb_user set password=? where username=?";
//获得statement对象
statement=connection.prepareStatement(sql);
statement.setString(1,password);
statement.setString(2,username);
//处理结果集
statement.executeUpdate();
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException throwsable){
throwsable.printStackTrace();
}finally {
//关闭资源
try{
DBUtil.closeAll(null,statement,connection);
}catch (SQLException throwsable){
throwsable.printStackTrace();
}
}
}
}
DBUtil
public class DBUtil {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
// 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//创建链接
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wzsxy","root","123456");
/*System.out.println("创建成功");*/
return connection;
}
public static void closeAll(ResultSet resultSet, Statement statement,Connection connection) throws SQLException {
if(resultSet!=null){
resultSet.close();
}
if(statement!=null){
statement.close();
}
if(connection!=null){
connection.close();
}
}
}
LoginServlet
public class LoginServlet extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username=req.getParameter("username");
String password=req.getParameter("password");
List<User> userList=new ArrayList<>();
UserDao userDao = new UserDao();
userList=userDao.findAll();
int r = 0;
for(User user:userList){
if(user.getUsername().equals(username)&&user.getPassword().equals(password)){
resp.getWriter().write("Success");
r=1;
}
}
if(r==0){
resp.getWriter().write("Error");
}
}
}
web.xml
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.sj.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
JSP
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="/login" method="post">
username:<input name="username" type="text">
password:<input name="password" type="password">
<input type="submit" value="登录">
</form>
</body>
</html>