Prechádzať zdrojové kódy

增加了用户注册信息存入数据库的实现

Snow 2 týždňov pred
rodič
commit
d172e94c03

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

@@ -9,7 +9,7 @@ public class SQLiteInit {
 
     private static final String JDBC_DRIVER = "org.sqlite.JDBC";
     private static final String DB_URL = "jdbc:sqlite:test.db";
-    //初始化数据库
+
     public static void main(String[] args) {
         try (Connection c = DriverManager.getConnection(DB_URL)) {
             // 加载驱动程序
@@ -31,7 +31,8 @@ public class SQLiteInit {
                     "user_name TEXT NOT NULL, " +
                     "user_password TEXT NOT NULL, " +
                     "user_uuid TEXT NOT NULL, " +
-                    "user_permission_level INTEGER NOT NULL" + // 修改为 INTEGER
+                    "user_permission_level INTEGER NOT NULL," + // 修改为 INTEGER
+                    "user_register_time INTEGER NOT NULL" +
                     ")";
             try (Statement statement = c.createStatement()) {
                 statement.execute(sqlUserDataTable);

+ 28 - 0
src/main/java/org/snowchen/GreatSonServer/programinit/UserInit.java

@@ -1,5 +1,6 @@
 package org.snowchen.GreatSonServer.programinit;
 
+import java.sql.*;
 import java.util.Arrays;
 import java.util.Scanner;
 import java.util.UUID;
@@ -38,6 +39,7 @@ public class UserInit {
         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);
     }
 
     // 提供一个公共方法来获取UserData数组
@@ -45,6 +47,32 @@ public class UserInit {
         return UserData;
     }
 
+    public void insertUserData(String[] UserData) {
+        String url = "jdbc:sqlite:test.db"; // 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);
+             PreparedStatement pstmt = conn.prepareStatement(sql)) {
+
+            pstmt.setString(1, UserData[0]);
+            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是字符串形式的纳秒时间戳
+
+            int affectedRows = pstmt.executeUpdate();
+            if (affectedRows > 0) {
+                System.out.println("用户数据插入成功");
+            } else {
+                System.out.println("用户数据插入失败");
+            }
+        } catch (SQLException e) {
+            System.err.println("数据库错误: " + e.getMessage());
+        } catch (Exception e) {
+            System.err.println("错误: " + e.getMessage());
+        }
+    }
+
     private static final String PWD_REGEX = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).{8,}$";
 
     /**