web登陆小案例,连接数据库,存储数据

学生党勿喷。

一共分5层:
dao 负责写sql语句
utils 连接数据库类
domain 用户类
service 负责业务逻辑
servlet 负责与服务器的交互
登陆小案例用到了
c3p0和my sql jar包
数据库的账号密码,自己提前准备好。文章中不带注册功能

第一次写,学生党。直接放代码了

dao层

package com.itheima.dao;

import com.itheima.Utils.C3P0Utils;
import com.itheima.domain.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserDao {
    private QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
    /**
     *  完成用户登陆
     * @param username  登陆的用户名
     * @param password  登陆的密码
     * @return   返回 登陆的用户   如果为null没有这样的用户 其实就是用户名或密码错误
     *             有用户就不为null
     */
    public User login(String username,String password) throws SQLException {
        String sql = "select * from user where username=? and password=?";
        Object[] params = {username,password};
        User user = qr.query(sql, new BeanHandler<>(User.class), params);

        return user;

    }
}

domain:
直接快捷键生成

package com.itheima.domain;

public class User {

    private String username;
    private String password;

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

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

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
}

service层:

package com.itheima.service;


import com.itheima.dao.UserDao;
import com.itheima.domain.User;

import java.sql.SQLException;

public class UserService {
    private UserDao userDao = new UserDao();
    /**
     *  判断 用户是否登陆成功
     * @param username
     * @param password
     * @return   F  登陆失败
     *           T  登陆成功
     *           需要找到dao查询之后 根据user对象进行判断。
     */
    public boolean login(String username,String password){
        User user=null;
        try {
            user = userDao.login(username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(user!=null){
            return true;
        }
        return false;

    }

}

servlet 层 :
package com.itheima.Servlet;

import com.itheima.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
       // 获取前端录入的用户名和密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username+"       "+password);
        // 将数据交给service
        UserService service = new UserService();
        boolean login = service.login(username, password);
        System.out.println(login);
        //System.out.println(login);
       //跳转主页 如果正确
        if(login){
            response.sendRedirect(request.getContextPath()+"/index.jsp");
        }else{
            request.setAttribute("msg","用户名或密码错误");
            //跳转地址,写自己的前端页面地址
            request.getRequestDispatcher("login.jsp").forward(request,response);
            }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

超级简陋前端:



	
		


用户名:

密码:

c3p0配置文件,写自己的数据库



	
	    
		com.mysql.jdbc.Driver
		jdbc:mysql://localhost:3306/web04
		root
		root
	
 

你可能感兴趣的:(web)