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();
}
}
}
}
本文介绍了一个简单的用户注册与登录系统的实现方法,包括密码强度验证、数据库连接与操作等核心功能。
4829

被折叠的 条评论
为什么被折叠?



