栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

SpringBoot简单登录注册

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot简单登录注册

1.先创建SpringBoot项目

       1.创建工程 选择Spring Initalizr

 2.勾选SpringWeb

2.导入相关jar文件

        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            mysql
            mysql-connector-java
            8.0.15
        
        
        
            com.alibaba
            druid
            1.1.21
        
    
3.数据库准备
create database demo charset=utf8;
create table student
(
    id       int auto_increment
        primary key,
    name     varchar(20) not null,
    password varchar(10) not null,
    age      int         not null,
    sex      char        not null,
    address  varchar(10) null
);
 INSERT INTO `student`(`id`, `name`, `password`, `age`, `sex`, `address`) VALUES (1, '陶喆', '', 20, '男', '陕西省西安市');
INSERT INTO `student`(`id`, `name`, `password`, `age`, `sex`, `address`) VALUES (2, '张三', '123456', 20, '男', '陕西省咸阳市');
4.进行架构搭建

 4.代码编
  • index.html



    
    Title


  注册页面
  注册
  登录界面
  登录

  • reg.html



    
    注册页面


注册页面
  • login.html



    
    登录页面


登录页面
实体类(Student)
package com.scy.boot111.domain;



public class Student {
    // 成员变量
    private Integer id;
    private String name;
    private String password;
    private Integer age;
    private String sex;
    private String address;

    // get、set
    public Integer getId() {
        return id;
    }

    public void setId(Integer 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 Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    // toString

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", password='" + password + ''' +
                ", age=" + age +
                ", sex='" + sex + ''' +
                ", address='" + address + ''' +
                '}';
    }
}
工具类(DBUtils)
package com.scy.boot111.utils;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.SQLException;


public class DBUtils {
    private static DruidDataSource ds;
    static {
        //创建表示连接池的对象
        ds = new DruidDataSource();
        //设置链接数据库的信息
        ds.setUrl("jdbc:mysql://localhost:3306/empdb?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false");
        ds.setUsername("root");
        ds.setPassword("xxxxxxxx");//写自己的密码
        //设置初始连接数量
        ds.setInitialSize(3);
        //设置最大连接数量
        ds.setMaxActive(5);
    }
    public static Connection getConn() throws SQLException {
        //从连接池中获取链接  异常抛出
        Connection conn = ds.getConnection();
        return conn;
    }
}
控制类(Controller)
package com.scy.boot111.controller;

import com.scy.boot111.domain.Student;
import com.scy.boot111.utils.DBUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


@Controller
public class StuController {
    @RequestMapping("/student/reg")
    @ResponseBody
    public String reg(Student student) {
        System.out.println("student = " + student);
        // 获取连接
        try {
            Connection conn = DBUtils.getConn();
            String sql = "insert into student values(null,?,?,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            // 替换?
            ps.setString(1, student.getName());
            ps.setString(2, student.getPassword());
            ps.setInt(3, student.getAge());
            ps.setString(4, student.getSex());
            ps.setString(5, student.getAddress());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "注册成功!!!";
    }

    @RequestMapping("/student/login")
    @ResponseBody
    public String login(Student student) {
        System.out.println("student = " + student);
        // 获取连接
        try {
            Connection conn = DBUtils.getConn();
            String sql = "select password from student where name=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            // 替换?
            ps.setString(1, student.getName());
            ResultSet resultSet = ps.executeQuery();
            if (resultSet.next()) {
                // 获取数据库中的密码
                String password = resultSet.getString(1);
                if (student.getPassword().equals(password)) {
                    return "登录成功!!!";
                }
                    return "密码错误";
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "用户不存在";
    }
}

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1039365.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号