一个用户创建以及用户登陆的简易实现(与mysql交互)

1.用户注册

可以将密码的注册限制增加(这里没有过多要求)

package login;

import java.sql.*;
import java.util.Scanner;

public class Login {
    static String insert="insert into login (username,password) values ('{','{')";
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("注册用户名(或者填写你的邮箱):");
        String s=sc.next();
        System.out.print("注册你的密码 :");
        String ss=sc.next();
        if(ss.length()<=6){
            System.out.println("您输入的密码长度需要至少为6个单位的数字,字母,符号,组成的字符串!");
        }else {
            insert(s,ss);
            System.out.println("用户注册成功啦,赶紧登陆看看吧!");
        }
    }
    public static Connection getConnection(){
        Connection connection=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            try {
                connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/LYJ","root","aA8932958150233");
                System.out.println("连接成功");
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return connection;
    }
    public static void insert(String user,String password){
        Connection connection=getConnection();
        Statement statement=null;
        try {
            statement=connection.createStatement();
            statement.execute(getInsertSQL(insert,user,password));
            System.out.println(getInsertSQL(insert,user,password));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    public static String getInsertSQL(String ...args){
        StringBuilder str=new StringBuilder(args[0]);
        int j=1;
        for (int i = 0; i < str.length(); i++) {
            if(str.charAt(i)=='{'){
                str.replace(i,i+1,args[j]);
                i=i+args[j].length();
                j++;
            }
        }
        return str.toString();
    }
}

2.用户登陆

package login;

import mysqlLeaning.CRUD.CRUD;
import 算法and数据结构.数据结构.多叉树.文件管理系统.命令判断.JudgeCommand;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class UserLogin {
    static final  String select_username="select username from login where username = '{'";
    static final  String select_password="select password from login where password = '{'";
    static String isOK="登陆失败";
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        do {
            System.out.print("输入您的用户名:");
            String username = sc.next();
            System.out.print("输入您的密码:");
            String password = sc.next();
            select(username, password);
        } while (!"登陆成功".equals(isOK));
		//这里以后我想根我的command——system交互,做一个小项目,当然command——system也需要升级,实现正真意义上的文件存储
        new JudgeCommand();
    }
    public static String getSelect(String ...args){
        StringBuilder str=new StringBuilder(args[0]);
        for(int i = 0 ; i < str.length() ; i++){
            if (str.charAt(i)=='{'){
                str.replace(i,i+1,args[1]);
                break;
            }
        }
        return str.toString();
    }
    public static void select(String username,String password){
        Connection connection= CRUD.getConnection();
        Statement statement=null;
        ResultSet resultSet=null;
        try {
            statement=connection.createStatement();
            resultSet=statement.executeQuery(getSelect(select_username,username));
            if (resultSet.next()){
                resultSet=statement.executeQuery(getSelect(select_password,password));
                if (resultSet.next()){
                    isOK="登陆成功";
                    System.out.println("登陆成功!!");
                }else{
                    System.out.println("您输入的密码不正确");
                }
            }else {
                System.out.println("您输入的用户名不存在");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值