JSP第七次作业

package com.example.bean;

public class User {
    private int UserID;
    private String UserName;
    private String UserEmail;
    private String Userphone;
    private String UserZhishang;
    private String Userpassword;

    public User(int userID, String userName, String userEmail, String userphone, String userZhishang, String userpassword) {
        UserID = userID;
        UserName = userName;
        UserEmail = userEmail;
        Userphone = userphone;
        UserZhishang = userZhishang;
        Userpassword = userpassword;
    }
    public User(String userName, String userEmail, String userphone, String userZhishang, String userpassword) {
        UserName = userName;
        UserEmail = userEmail;
        Userphone = userphone;
        UserZhishang = userZhishang;
        Userpassword = userpassword;
    }

    public User() {
    }

    public String getUserpassword() {
        return Userpassword;
    }

    public void setUserpassword(String userpassword) {
        Userpassword = userpassword;
    }

    public int getUserID() {
        return UserID;
    }

    public void setUserID(int userID) {
        UserID = userID;
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public String getUserEmail() {
        return UserEmail;
    }

    public void setUserEmail(String userEmail) {
        UserEmail = userEmail;
    }

    public String getUserphone() {
        return Userphone;
    }

    public void setUserphone(String userphone) {
        Userphone = userphone;
    }

    public String getUserZhishang() {
        return UserZhishang;
    }

    public void setUserZhishang(String userZhishang) {
        UserZhishang = userZhishang;
    }

    @Override
    public String toString() {
        return "User{" +
                "UserID=" + UserID +
                ", UserName='" + UserName + '\'' +
                ", UserEmail='" + UserEmail + '\'' +
                ", Userphone=" + Userphone +
                ", UserZhishang='" + UserZhishang + '\'' +
                ", Userpassword='" + Userpassword + '\'' +
                '}';
    }
}
package com.example.Dao;

import com.example.bean.User;

import java.sql.*;
import java.util.ArrayList;

public class UserDao {
    String url="jdbc:mysql://localhost:3306/axing?characterEncoding=utf8";
    String username="root";
    String password="";

    public ArrayList<User> getlist(){
        ArrayList<User> list =new ArrayList<>();
        Connection con=null;
        PreparedStatement pre=null;
        ResultSet rs=null;
        try {
            con = DriverManager.getConnection(url, username, password);
            String sql="SELECT user_id,user_name ,user_email ,user_phone,user_password,user_zhishang FROM USER";
            pre= con.prepareStatement(sql);
            rs = pre.executeQuery();
            while (rs.next()){
                User u =new User();
                u.setUserID(rs.getInt(1));
                u.setUserName(rs.getString(2));
                u.setUserEmail(rs.getString(3));
                u.setUserphone(rs.getString(4));
                u.setUserpassword(rs.getString(5));
                u.setUserZhishang(rs.getString(6));
                list.add(u);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                if (rs!=null)
                    rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                if (pre!=null)
                    pre.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                if (con!=null)
                    con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return  list;
    }
    public void addUser(User u){
        Connection con=null;
        PreparedStatement pre=null;
        try {
            con= DriverManager.getConnection(url, username, password);
            String sql="INSERT INTO USER (user_name,user_email,user_phone,user_password,user_zhishang)VALUES(?,?,?,?,?)";
            pre = con.prepareStatement(sql);
            pre.setString(1,u.getUserName());
            pre.setString(2,u.getUserEmail());
            pre.setString(3,u.getUserphone());
            pre.setString(4,u.getUserpassword());
            pre.setString(5,u.getUserZhishang());
            int m = pre.executeUpdate();
            if (m!=0){
                //跳转成功
                System.out.println("添加成功");
            }else {
                //跳转失败
                System.out.println("添加失败");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                if (con!=null)
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                if (pre!=null)
                pre.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


    public boolean QueryUser(String u,String p){
        Connection con=null;
        PreparedStatement pre=null;
        try {
            con= DriverManager.getConnection(url, username, password);
            String sql="SELECT * FROM USER WHERE user_name=? AND user_password=?";
            pre = con.prepareStatement(sql);
            pre.setString(1,u);
            pre.setString(2,p);
            pre.executeQuery();
            return true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                if (con!=null)
                    con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                if (pre!=null)
                    pre.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return  false;
    }
}

service层

package com.example.servlet;

import com.example.Dao.UserDao;
import com.example.bean.StudentInfo;
import com.example.bean.User;

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.io.IOException;
import java.sql.*;

public class Register extends HttpServlet {
    @Override
    public void init() throws ServletException {
        super.init();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        StudentInfo userBean=new StudentInfo();
        req.setAttribute("userBean",userBean);
        String register_username = req.getParameter("username").trim();
        String password1=req.getParameter("password1").trim();
        String password2=req.getParameter("password2").trim();
        String email = req.getParameter("email").trim();
        String phone = req.getParameter("phone").trim();
        String zhishang=new String(req.getParameter("select").getBytes("iso-8859-1"), "utf-8").trim();
        boolean boo=register_username.length()>0&&password1.length()>0;
        if (boo){
            User u=new User(register_username,email,phone,zhishang,password1);
            UserDao userDao=new UserDao();
            userDao.addUser(u);
            resp.getWriter().println("<h1>注册成功<h1>");
            resp.getWriter().println("<h1>5s后页面跳转至登陆界面....</h1>");
            resp.getWriter().println("<a href=\"login.jsp\">立即去登陆界面</a>");
            resp.setHeader("Refresh", "5;url=login.jsp");
        }else {
            resp.getWriter().println("<h1>注册失败<h1>");
            resp.getWriter().println("<h1>5s后页面跳转至注册界面....</h1>");
            resp.getWriter().println("<a href=\"register.jsp\">立即去注册界面</a>");
            resp.setHeader("Refresh", "5;url=register.jsp");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

 

posted on 2021-04-19 17:06  iM59  阅读(21)  评论(0编辑  收藏  举报

导航