登录注册功能(JDBC+MySQL)

目录

 

一、一些准备

1、建立数据库和表

2、新建项目并导入连接数据库的包

二、开始编程

1.架构

2、代码

1)实体类

 2)连接数据库

3)数据访问

4)业务逻辑+视图

三、结果

1、注册

2、登录

四、总结


一、一些准备

1、建立数据库和表

create DATABASE student;
CREATE TABLE student(
id INT AUTO_INCREMENT,
NAME VARCHAR(255),
sex VARCHAR(255),
age INT,
PASSWORD VARCHAR(255),
PRIMARY KEY(id)
)

插入一些数据 

2、新建项目并导入连接数据库的包

参考这篇博客

https://blog.csdn.net/qq_36172505/article/details/84102468

二、开始编程

1.架构

2、代码

1)实体类

public class Student {
    private int id;
    private String name;
    private String sex;
    private int age;
    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 getSex() {
        return sex;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getPassword() {
        return password;
    }

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

    public Student() {

    }

    public Student(int id, String name, String sex, int age, String password) {
        this.id = id;
        this.age = age;
        this.name = name;
        this.sex = sex;
        this.password = password;
    }
    public Student(String name, String sex, int age, String password) {
        this.age = age;
        this.name = name;
        this.sex = sex;
        this.password = password;
    }
    @Override
    public String toString() {
        return "student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", password=" + password +
                '}';
    }

}

 2)连接数据库

import java.sql.*;

public class ConnMysql {

    public static Connection getConnection() throws ClassNotFoundException {
        Connection connection = null;
        //1.加载驱动,使用反射知识
        Class.forName("com.mysql.cj.jdbc.Driver");
        try {
            //2.使用DriverManager获取数据库连接
            //其中返回的Connection就代表了Java程序和数据库的连接
            //不同数据库的URL写法需要查驱动文档,用户名、密码由DBA分配
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf8",
                    "root", "123456");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }


    public static void close(ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(PreparedStatement statement) {

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public static void close(Connection connection) {

        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

3)数据访问

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

public class StudentDao {

    public Student login(String name, String password) {

        ResultSet resultSet = null;
        PreparedStatement statement = null;
        Connection connection = null;
        Student student = null;
        try {
            connection = ConnMysql.getConnection();
            //3.写sql
            String sql = "select * from student where name=? and password=?";
            //4.得到statement对象
            statement = connection.prepareStatement(sql);
            //5.执行sql
            statement.setString(1,name);
            statement.setString(2, password);
            resultSet = statement.executeQuery();
            //6处理结果集
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String sex = resultSet.getString(3);
                int age = resultSet.getInt("age");
                student = new Student(id,name, sex, age, password);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ConnMysql.close(resultSet);
            ConnMysql.close(connection);
            ConnMysql.close(statement);
        }
        return student;

    }


    public void addOne(Student student) throws ClassNotFoundException {
        Connection connection = ConnMysql.getConnection();
        PreparedStatement pstm = null;
        try {
            String sql = "insert into student(name,sex,age,password) values(?,?,?,?)";   //sql语言
            pstm = connection.prepareStatement(sql);

            //填充sql语句中的?
            pstm.setString(1, student.getName());
            pstm.setString(2, student.getSex());
            pstm.setInt(3, student.getAge());
            pstm.setString(4, student.getPassword());
            //使用executeUpdate函数执行sql语句
            pstm.executeUpdate();
            System.out.println("新增用户成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ConnMysql.close(pstm);
            ConnMysql.close(connection);
        }
    }
}

4)业务逻辑+视图

import java.util.Scanner;

public class Main {
    Scanner sc = new Scanner(System.in);
    StudentDao studentDao = new StudentDao();

    public static void main(String[] args) throws ClassNotFoundException {
        Main main = new Main();
        Scanner scanner = new Scanner(System.in);
        start:
        while (true) {
            System.out.println("1、登录 2、注册 3、退出");
            switch (scanner.next()) {
                case "1":
                    main.login();
                    continue start;
                case "2":
                    main.register();
                    continue start;
                default:
                    break start;
            }
        }
    }

    public void login() {
        //1.键盘录入,接收用户名和密码
        System.out.println("请输入用户名:");
        String name = sc.next();
        System.out.println("请输入密码:");
        String password = sc.next();
        //2.调用方法
        //3.判断结果,输出不同语句
        if (name == null || password == null) {
            System.out.println("用户名或密码错误!");
        } else {
            Student student = studentDao.login(name, password);
            if (student != null) {
                System.out.println("登录成功!");
            } else {
                System.out.println("用户名或密码错误!");
            }
        }
    }

    public void register() throws ClassNotFoundException {//注册
        //1.键盘录入,接收用户名和密码
        System.out.println("请输入用户名:");
        String name = sc.next();
        System.out.println("请输入性别:");
        String sex = sc.next();
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        System.out.println("请输入密码:");
        String password = sc.next();
        Student student = new Student(name, sex, age, password);
        //2.调用方法
        //3.判断结果,输出不同语句
        studentDao.addOne(student);
    }
}

三、结果

1、注册

2、登录

四、总结

       输入都没做验证,因为主要是连接数据库,所以没在意。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值