java登录注册demo 全代码。(代码阶段)

接着我们上一篇的准备阶段写文章-CSDN创作中心

根据数据库 创建好实体类

package model;

public class UserDate {
    private int id;
    private String name;
    private  String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public UserDate(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public UserDate(int id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }
}

第二步 写一个数据库连接

java登录注册demo 全代码。(代码阶段)_第1张图片

package db;


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

public class Database{
    Connection conn=null;
    public Database() throws ClassNotFoundException, SQLException {

        Class.forName("com.mysql.cj.jdbc.Driver");

        this.conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/web001?serverTiemzone=GMT%2B8","root","123456");
        //System.out.println(conn);
    }

    public void showConnect(){
        if(conn==null){
            System.out.println("数据库连接失败");
        }else{
            System.out.println("连接成功");
        }

    }
    //关闭数据
    public void close() throws SQLException {
        conn.close();
    }
    //在login表添加数据  注册
    public void insert(String name,String pwd) throws SQLException {
        PreparedStatement prep=conn.prepareStatement("insert into web001.login value (null,?,?)");
        prep.setString(1,name);
        prep.setString(2,pwd);
        prep.execute();
    }

    public ArrayList getAlluser() throws SQLException {
        ArrayList uselist=new ArrayList();
        PreparedStatement prep=conn.prepareStatement("select * from web001.login");
        prep.execute();
        ResultSet resultSet=prep.executeQuery();
        while (resultSet.next()){
            int myid=resultSet.getInt("id");
            String myname=resultSet.getString("name");
            String mypwd=resultSet.getString("password");
            uselist.add(new UserDate(myid,myname,mypwd));
        }
        return uselist;
    }
    //在表里查询数据
    public UserDate getUser(int id) throws SQLException {
    PreparedStatement prep=conn.prepareStatement("select * from web001.login where id=?");
    prep.setInt(1,id);
    prep.execute();

        ResultSet resultSet=prep.executeQuery();
        if(resultSet.next()){
            int myid=resultSet.getInt("id");
            String myname=resultSet.getString("name");
            String mypwd=resultSet.getString("password");
           return new UserDate(myid,myname,mypwd);

        }
        return null;
    }

    //登录
    public UserDate login(int id,String pwd) throws SQLException {
        PreparedStatement prep= conn.prepareStatement("select  web001.login.password from web001.login where id=?");
        prep.setInt(1,id);
        prep.execute();
        //获取到id 就能查出密码来
        ResultSet resultSet= prep.executeQuery();
        if(resultSet.next()){
            String mypwd= resultSet.getString("password");
           //进行密码的判断
            if (pwd.equals(mypwd)){
                return getUser(id);
            }else {
                return null;
            }
        }
        return null;
    }
}

写登录的servlet

package servlet;


import db.Database;
import model.UserDate;

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;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet(name = "LoginServlet",urlPatterns = "/Login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置网页乱码问题
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");


        //parseInt 解析一个字符串 返回一个整数
        // getparameter 得到post请求体中中发送的表单数据  从客服端取数据
        int id=Integer.parseInt(req.getParameter("id"));
        String pwd=req.getParameter("pwd");
        System.out.println("输入的账号为:"+id+"密码为:"+pwd);

        PrintWriter out = resp.getWriter();
         out.println("输入的账号为:"+id+"密码为:"+pwd);

        try {
            Database database=new Database();
            UserDate userDate=database.login(id,pwd);
            System.out.println(userDate);
            if(userDate==null){
                out.println("该用户不存在 或者 密码错误"+"放回登录");
            }else {
                out.println("欢迎登录"+userDate.getName());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

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

注册的serlvet

package servlet;

import db.Database;
import model.UserDate;

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;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet(name = "RegisterServlet",urlPatterns = "/Register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置网页乱码问题
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");


        //parseInt 解析一个字符串 返回一个整数
        // getparameter 得到post请求体中中发送的表单数据  从客服端取数据
        String name=req.getParameter("name");
        String pwd=req.getParameter("pwd");
        String pwd2=req.getParameter("pwd2");
        System.out.println("输入的账号为:"+name+"密码为:"+pwd);

        PrintWriter out = resp.getWriter();
       // out.println("输入的账号为:"+name+"密码为:"+pwd);

    if(pwd.equals(pwd2)){

        try {
            Database db=new Database();
            db.insert(name,pwd);
            out.println("注册成功");
            ArrayList useDates=db.getAlluser();
       out.println("总表里面共有:"+useDates.size()+"
"); for(UserDate each:useDates){ out.println(each.getId()+"--"+each.getName()+"--"+each.getPassword()+"
"); } out.println("放回登录界面"+"放回登录"); } catch (ClassNotFoundException e) { e.printStackTrace(); out.println("注册失败"); } catch (SQLException throwables) { throwables.printStackTrace(); out.println("注册失败2"); } }else{ out.println("两次的密码不一致"); out.println("放回注册界面"+"放回注册"); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } }

最后两个 前端登录 注册代码

<%--
  Created by IntelliJ IDEA.
  User: zyg_h
  Date: 2022/7/3
  Time: 15:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

  
    $Title$
  
  
  注册
<%--  br  为前端的换行 --%>
  
账号:
密码:
<%--
  Created by IntelliJ IDEA.
  User: zyg_h
  Date: 2022/7/3
  Time: 19:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    注册


用户名:
密码:
确认密码:

实现后的图

java登录注册demo 全代码。(代码阶段)_第2张图片

java登录注册demo 全代码。(代码阶段)_第3张图片

java登录注册demo 全代码。(代码阶段)_第4张图片

你可能感兴趣的:(demo项目代码,java,数据库,ide,servlet,sql)