Selaa lähdekoodia

增加注册用户防重名功能

Snow 2 viikkoa sitten
vanhempi
sitoutus
2cbaf5bdef

+ 12 - 0
.idea/artifacts/GreatSon_Server_jar.xml

@@ -0,0 +1,12 @@
+<component name="ArtifactManager">
+  <artifact name="GreatSon_Server:jar">
+    <output-path>$PROJECT_DIR$/out/artifacts/GreatSon_Server_jar</output-path>
+    <root id="root">
+      <element id="archive" name="GreatSon_Server.jar">
+        <element id="module-output" name="GreatSon_Server" />
+        <element id="module-test-output" name="GreatSon_Server" />
+      </element>
+      <element id="library" level="project" name="Maven: org.xerial:sqlite-jdbc:3.41.2.2" />
+    </root>
+  </artifact>
+</component>

+ 15 - 0
.idea/dataSources.xml

@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="DataSourceManagerImpl" format="xml" multifile-model="true">
+    <data-source source="LOCAL" name="identifier.sqlite" uuid="c5ca675d-386b-4b76-a3b4-b8cf5fc2d511">
+      <driver-ref>sqlite.xerial</driver-ref>
+      <synchronize>true</synchronize>
+      <jdbc-driver>org.sqlite.JDBC</jdbc-driver>
+      <jdbc-url>jdbc:sqlite:identifier.sqlite</jdbc-url>
+      <jdbc-additional-properties>
+        <property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
+      </jdbc-additional-properties>
+      <working-dir>$ProjectFileDir$</working-dir>
+    </data-source>
+  </component>
+</project>

+ 1 - 1
.idea/misc.xml

@@ -8,7 +8,7 @@
       </list>
     </option>
   </component>
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="zulu-21" project-jdk-type="JavaSDK">
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="zulu-21" project-jdk-type="JavaSDK">
     <output url="file://$PROJECT_DIR$/out" />
   </component>
 </project>

+ 8 - 0
.idea/sqldialects.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="SqlDialectMappings">
+    <file url="file://$PROJECT_DIR$/src/main/java/org/snowchen/GreatSonServer/databasecore/SQLiteInit.java" dialect="SQLite" />
+    <file url="file://$PROJECT_DIR$/src/main/java/org/snowchen/GreatSonServer/programinit/UserRegister.java" dialect="GenericSQL" />
+    <file url="PROJECT" dialect="SQLite" />
+  </component>
+</project>

+ 3 - 4
src/main/java/org/snowchen/GreatSonServer/Main.java

@@ -1,15 +1,14 @@
 package org.snowchen.GreatSonServer;
 
 import org.snowchen.GreatSonServer.programinit.UserRegister;
+import org.snowchen.GreatSonServer.databasecore.SQLiteInit;
 
-import java.util.Arrays;
 
 public class Main {
     public static void main(String[] args) {
 
-        System.out.printf("Hello and welcome!");
+        System.out.printf("Hello and welcome! \n  ");
+        SQLiteInit.InitDatabase();
         UserRegister userInit = new UserRegister();
-        String[] userData = userInit.getUserData(); // 外部获取UserData数组
-        System.out.println("UserData: " + Arrays.toString(userData));
     }
 }

+ 12 - 2
src/main/java/org/snowchen/GreatSonServer/databasecore/SQLiteInit.java

@@ -8,9 +8,18 @@ import java.sql.Statement;
 public class SQLiteInit {
 
     private static final String JDBC_DRIVER = "org.sqlite.JDBC";
-    private static final String DB_URL = "jdbc:sqlite:test.db";
+    private static final String DB_URL = "jdbc:sqlite:identifier.sqlite";
 
     public static void main(String[] args) {
+        // 调用初始化数据库的方法
+        InitDatabase();
+    }
+
+    /**
+     * 初始化数据库的方法
+     */
+    @SuppressWarnings("CallToPrintStackTrace")
+    public static void InitDatabase() {
         try (Connection c = DriverManager.getConnection(DB_URL)) {
             // 加载驱动程序
             Class.forName(JDBC_DRIVER);
@@ -32,7 +41,7 @@ public class SQLiteInit {
                     "user_password TEXT NOT NULL, " +
                     "user_uuid TEXT NOT NULL, " +
                     "user_permission_level INTEGER NOT NULL," + // 修改为 INTEGER
-                    "user_register_time INTEGER NOT NULL" +
+                    "user_register_time TEXT NOT NULL" + // 修改为 TEXT
                     ")";
             try (Statement statement = c.createStatement()) {
                 statement.execute(sqlUserDataTable);
@@ -51,3 +60,4 @@ public class SQLiteInit {
 
 
 
+

+ 38 - 7
src/main/java/org/snowchen/GreatSonServer/programinit/UserRegister.java

@@ -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,}$";
 
     /**