1. 程式人生 > >從登入註冊學習jdbc

從登入註冊學習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到前臺,前臺作出相應的展示效果。