目录
需求内容如下
示例代码
数据库studb
Java代码
效果图
需求内容如下
1,创建数据库studb
2,库中添加用户表userinfo,包含如下字段
用户id ,用户名,用户密码,用户权限 (数据类型和约束自己定义)
添加最少3条用户数据
3,java项目中实现如下功能
(1)定义方法实现菜单选择:1,用户登录 2,用户注册
(2)定义方法实现用户登录
用户输入的账号密码,通过数据库表userinfo查询,如果一致,登录成功,欢迎用户XX
如果不一致,登录失败(账号密码错误),请重新登录(3次),
超过3次退出系统
(3)用户注册
输入用户名,自动检查数据库表中有无该名称存在
如果用户名存在,提示:该用户名已经被注册,请重新输入
如果不存在,继续输入密码,确认密码(两次密码一致),
如果一致,将用户名和密码添加到数据表中,提示用户注册成功!
示例代码
数据库studb
drop database if exists studb;
create database studb;
alter database studb character set utf8 collate utf8_bin;
use studb;
CREATE TABLE userinfo (
id INT PRIMARY KEY AUTO_INCREMENT, -- 用户id
username VARCHAR(50) UNIQUE NOT NULL,-- 用户名
password VARCHAR(50) NOT NULL,-- 用户密码
permission INT NOT NULL-- 用户权限
);
-- 添加数据
INSERT INTO userinfo (username, password, permission) VALUES
('111', 'l111', 1),
('222', 'l222', 2),
('333', 'l333', 3);
-- 查询数据
SELECT * FROM userinfo;
Java代码
import java.sql.*;
import java.util.*;
public class Main {
@SuppressWarnings("unused")
private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; //MYSQL6.0以下版本的得使用该驱动文件:com.mysql.jdbc.Driver
private static final String DB_URL = "jdbc:mysql://localhost:3306/studb";//studb为数据库名
private static final String USER = "root"; //换成自己的用户名
private static final String PASS = "123456"; //换成自己的密码
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int choice = -1;
while (choice != 0) {
System.out.println("请选择:");
System.out.println("1. 用户登录");
System.out.println("2. 用户注册");
System.out.println("0. 退出系统");
choice = getNextInt();
switch (choice) {
case 1:
userLogin();
break;
case 2:
userRegister();
break;
case 0:
System.out.println("谢谢使用,再见!");
break;
default:
System.out.println("选择错误,请重新选择。");
}
}
}
public static void userLogin() {
String username, password;
int count = 0;
while (count < 3) {
System.out.println("请输入用户名:");
username = scanner.next();
System.out.println("请输入密码:");
password = scanner.next();
if (validateUser(username, password)) {
System.out.println("欢迎用户" + username + "!");
return;
} else {
System.out.println("账号密码错误,请重新输入。" + "还剩" + (2 - count) + "次机会");
}
count++;
}
System.out.println("错误次数过多,系统锁定,请联系管理员解锁。");
System.exit(0);
}
public static void userRegister() {
String username, password, confirmPassword;
System.out.print("请输入用户名:");
username = getnext();
if (checkUsernameExist(username)) {
System.out.println("该用户名已经被注册,请重新选择。");
return;
}
do {
System.out.print("请输入密码:");
password = getnext();
System.out.print("请确认密码:");
confirmPassword = getnext();
if (!password.equals(confirmPassword)) {
System.out.println("两次密码不一致,请重新输入。");
} else {
break;
}
} while (true);
if (registerUser(username, password)) {
System.out.println("用户注册成功!");
} else {
System.out.println("用户注册失败,请稍后再试。");
}
}
private static String getnext() {
if (scanner.hasNext()) {
return scanner.next();
} else {
throw new NoSuchElementException("未找到行");
}
}
private static boolean validateUser(String username, String password) {
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM userinfo WHERE username = ? AND password = ?")) {
stmt.setString(1, username);
stmt.setString(2, password);
try (ResultSet rs = stmt.executeQuery()) {
return rs.next();
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
private static boolean checkUsernameExist(String username) {
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM userinfo WHERE username = ?")) {
stmt.setString(1, username);
try (ResultSet rs = stmt.executeQuery()) {
return rs.next();
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
private static boolean registerUser(String username, String password) {
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement stmt = conn.prepareStatement("INSERT INTO userinfo (username, password, permission) VALUES (?, ?, ?)")) {
stmt.setString(1, username);
stmt.setString(2, password);
stmt.setInt(3, 1);
int rows = stmt.executeUpdate();
return rows > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
private static int getNextInt() {
if (scanner.hasNextInt()) {
return scanner.nextInt();
} else {
throw new InputMismatchException("无效的输入");
}
}
}