|
@@ -3,7 +3,8 @@ package org.snowchen.GreatSonServer.programinit;
|
|
|
import java.security.MessageDigest;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.sql.*;
|
|
|
-import java.util.Arrays;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
import java.util.Scanner;
|
|
|
import java.util.UUID;
|
|
|
|
|
@@ -21,8 +22,12 @@ public class UserRegister {
|
|
|
// 用户信息初始化
|
|
|
System.out.println("请输入用户名:");
|
|
|
this.user_name = scan.nextLine();
|
|
|
- while (this.user_name.isEmpty()) {
|
|
|
- System.out.println("用户名不能为空,请重新输入:");
|
|
|
+ while (this.user_name.isEmpty() || isUsernameExists(this.user_name)) {
|
|
|
+ if (this.user_name.isEmpty()) {
|
|
|
+ System.out.println("用户名不能为空,请重新输入:");
|
|
|
+ } else {
|
|
|
+ System.out.println("已经有同名用户,请重新输入用户名:");
|
|
|
+ }
|
|
|
this.user_name = scan.nextLine();
|
|
|
}
|
|
|
UserData[0] = this.user_name;
|
|
@@ -43,10 +48,10 @@ public class UserRegister {
|
|
|
UserData[2] = this.uuid;
|
|
|
this.user_permission_level = 0;
|
|
|
UserData[3] = String.valueOf(this.user_permission_level);
|
|
|
- this.user_register_time = String.valueOf(System.nanoTime());
|
|
|
+ // 获取年月日时分秒的时间戳
|
|
|
+ this.user_register_time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
UserData[4] = this.user_register_time;
|
|
|
System.out.println("用户名:" + this.user_name + "\n密码:" + this.user_password + "\nuuid:" + this.uuid + "\n注册时间:" + this.user_register_time);
|
|
|
- System.out.println("UserData: " + Arrays.toString(UserData));
|
|
|
insertUserData(UserData);
|
|
|
}
|
|
|
|
|
@@ -56,7 +61,7 @@ public class UserRegister {
|
|
|
}
|
|
|
|
|
|
public void insertUserData(String[] UserData) {
|
|
|
- String url = "jdbc:sqlite:test.db"; // SQLite数据库URL
|
|
|
+ String url = "jdbc:sqlite:identifier.sqlite"; // SQLite数据库URL
|
|
|
String sql = "INSERT INTO user_data (user_name, user_password, user_uuid, user_permission_level, user_register_time) VALUES (?, ?, ?, ?, ?)";
|
|
|
|
|
|
try (Connection conn = DriverManager.getConnection(url);
|
|
@@ -66,7 +71,7 @@ public class UserRegister {
|
|
|
pstmt.setString(2, UserData[1]);
|
|
|
pstmt.setString(3, UserData[2]);
|
|
|
pstmt.setInt(4, Integer.parseInt(UserData[3])); // 假设user_permission_level是整数
|
|
|
- pstmt.setString(5, UserData[4]); // 假设user_register_time是字符串形式的纳秒时间戳
|
|
|
+ pstmt.setString(5, UserData[4]); // 假设user_register_time是字符串形式的年月日时分秒时间戳
|
|
|
|
|
|
int affectedRows = pstmt.executeUpdate();
|
|
|
if (affectedRows > 0) {
|
|
@@ -81,6 +86,32 @@ public class UserRegister {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 检测用户名是否存在
|
|
|
+ *
|
|
|
+ * @param username 待检测的用户名
|
|
|
+ * @return 如果用户名存在,则返回true;否则返回false
|
|
|
+ */
|
|
|
+ public boolean isUsernameExists(String username) {
|
|
|
+ String url = "jdbc:sqlite:identifier.sqlite"; // SQLite数据库URL
|
|
|
+ String sql = "SELECT COUNT(*) FROM user_data WHERE user_name = ?";
|
|
|
+
|
|
|
+ try (Connection conn = DriverManager.getConnection(url);
|
|
|
+ PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
|
|
+
|
|
|
+ pstmt.setString(1, username);
|
|
|
+ try (ResultSet rs = pstmt.executeQuery()) {
|
|
|
+ if (rs.next()) {
|
|
|
+ int count = rs.getInt(1);
|
|
|
+ return count > 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (SQLException e) {
|
|
|
+ System.err.println("数据库错误: " + e.getMessage());
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
private static final String PWD_REGEX = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{8,}$";
|
|
|
|
|
|
/**
|