从登录注册学习jdbc

项目目录

该目录下

该目录下控制器使用LoginControllerPost,Object类使用User2,暂时不考虑sql注入、异常处理等,纯粹的入门

数据库格式类型

在这里插入图片描述
在这里插入图片描述

前台代码


var loginParams = {

"userName": this.ruleForm2.account,

"password": this.ruleForm2.checkPass,

'sysId':'hello'

};

this.$http.post('/login2',loginParams,).then((res) => {

  if (res.data.returnCode == '200') {

    _this.$message({

      message: '登陆成功',

      type: 'success'

    });

  }

}

前台页面

在这里插入图片描述

返回的数据

在这里插入图片描述

后台代码


/**

*LoginControllerPost    控制器

*/

package com.example.demo22.controller;

import com.example.demo22.Object.User2;

import com.example.demo22.jdbcUtil.JdbcUtil;

import com.example.demo22.jsonUtil.JsonUtil;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RestController;

import java.sql.ResultSet;

import java.util.ArrayList;

@RestController

public class LoginControllerPost {

    @PostMapping("/login2")

    public Object login(@RequestBody User2 user2){

        String username = user2.getUserName();

        String password = user2.getPassword();

        String sysId = user2.getSysId();

        String sql = "select * from user where userName='"+username+"'&&password='"+password+"'&&sysId='"+sysId+"'";

        JsonUtil json = new JsonUtil();

        ArrayList list = new ArrayList();

        try {

            ResultSet res = JdbcUtil.jdbc1(sql);

            if(res.next()){

                User2 user = new User2();

                user.setSysId(sysId);

                user.setId("1111");

                user.setUserName(username);

                user.setPassword(password);

                list.add(user);

                json.setRows(list);

                json.setTotal(1);

                json.setReturnCode("200");

                json.setReturnMsg("登录成功");

            }else{

                json.setReturnCode("500");

                json.setReturnMsg("账号或密码错误");

                json.setRows(new ArrayList());

            }

        } catch  (Exception e){

            e.printStackTrace();

        }finally{

            System.out.println(user2.getUserName());

            return json;

        }

    }

}


/**

*JdbcUtil类

*/

package com.example.demo22.jdbcUtil;

import javax.annotation.Resource;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

@Resource

public class JdbcUtil {

    private static final String URL = "jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8";

    public static final String USER = "root";

    public static final String PASSWORD = "";

    public static ResultSet jdbc1(String sql) throws Exception {

        //1.加载驱动程序

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

        //2. 数据库连接

        Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);

        //3.操作数据库

        Statement stmt = conn.createStatement();

        ResultSet rs = stmt.executeQuery(sql);

        return rs;

    }

}


/**

* User2类

*/

package com.example.demo22.Object;

public class User2 {

    String id;

    String userName;

    String password;

    String sysId;

    public String getId() {

        return id;

    }

    public void setId(String 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;

    }

    public String getSysId() {

        return sysId;

    }

    public void setSysId(String sysId) {

        this.sysId = sysId;

    }

}


package com.example.demo22.jsonUtil;

import java.util.ArrayList;

public class JsonUtil {

    private String returnCode;

    private String returnMsg;

    private ArrayList rows;

    private int total;

    public void setReturnMsg(String returnMsg) {

        this.returnMsg = returnMsg;

    }

    public void setRows(ArrayList rows) {

        this.rows = rows;

    }

    public void setTotal(int total) {

        this.total = total;

    }

    public String getReturnMsg() {

        return returnMsg;

    }

    public ArrayList getRows() {

        return rows;

    }

    public int getTotal() {

        return total;

    }

    public String getReturnCode() {

        return returnCode;

    }

    public void setReturnCode(String returnCode) {

        this.returnCode = returnCode;

    }

}

总结

要想实现登录注册,首先前台要提交用户输入的账号密码到后台,后台接收参数去数据库查找,如果查找到了,那么就生成token,并返回相应的状态码和token到前台,前台作出相应的展示效果。

你可能感兴趣的:(从登录注册学习jdbc)