Skip to content

Commit dc7b6eb

Browse files
committed
feat(数据库): 添加多数据库模式支持
引入 Database 枚举类定义支持的数据库类型 修改 Configuration 类添加数据库模式配置 更新 DatabaseController 根据配置选择不同数据库连接方式 #2
1 parent a14a4eb commit dc7b6eb

File tree

3 files changed

+75
-11
lines changed

3 files changed

+75
-11
lines changed

src/main/java/xin/ctkqiang/config/Configuration.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package xin.ctkqiang.config;
22

3+
import xin.ctkqiang.dto.Database;
4+
35
public class Configuration {
6+
public String DatabaseMode;
7+
48
public static final String DB_NAME = "ExploitDB";
59
public static final String DB_URL = "jdbc:mysql://localhost:3306/" + DB_NAME
610
+ "?serverTimezone=UTC&allowPublicKeyRetrieval=true&useSSL=false";
@@ -10,4 +14,12 @@ public class Configuration {
1014
public static final String DB_DRIVER = "com.mysql.cj.jdbc.Driver";
1115

1216
public static final boolean DEBUG = false;
17+
18+
public String getDatabaseMode() {
19+
return this.DatabaseMode;
20+
}
21+
22+
public void setDatabaseMode(Database databaseMode) {
23+
this.DatabaseMode = databaseMode.getValue();
24+
}
1325
}

src/main/java/xin/ctkqiang/controller/DatabaseController.java

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.fasterxml.jackson.databind.ObjectMapper;
1919

2020
import xin.ctkqiang.config.Configuration;
21+
import xin.ctkqiang.dto.Database;
2122
import xin.ctkqiang.dto.Exploit;
2223

2324
/**
@@ -44,6 +45,8 @@ public class DatabaseController {
4445
/** 数据库驱动类名 */
4546
protected final static String DB_DRIVER = Configuration.DB_DRIVER;
4647

48+
private Configuration Config;
49+
4750
/**
4851
* 静态初始化块
4952
* 在类加载时尝试加载数据库驱动
@@ -109,18 +112,41 @@ public void CreateTableIfNotExists() {
109112
+ ");";
110113

111114
try {
112-
// 确保数据库驱动已加载
113-
Class.forName(DatabaseController.DB_DRIVER);
114-
115-
// 创建数据库连接并执行建表语句
116-
try (Connection conn = DriverManager.getConnection(
117-
DatabaseController.DB_URL,
118-
DatabaseController.DB_USER,
119-
DatabaseController.DB_PASSWORD);
120-
Statement stmt = conn.createStatement()) {
121-
stmt.execute(query);
122-
System.out.println("💾 数据表【内容信息】初始化成功!");
115+
116+
String mode = this.Config.getDatabaseMode();
117+
Database dbMode;
118+
119+
try {
120+
dbMode = Database.fromValue(mode);
121+
} catch (IllegalArgumentException e) {
122+
dbMode = null;
123123
}
124+
125+
if (dbMode == null) {
126+
System.out.println("⚠️ Unknown database mode: " + mode);
127+
return;
128+
}
129+
130+
switch (dbMode) {
131+
case SQLITE:
132+
System.out.println("📦 Connect to SQLite database");
133+
break;
134+
case MYSQL:
135+
default:
136+
// 确保数据库驱动已加载
137+
Class.forName(DatabaseController.DB_DRIVER);
138+
139+
// 创建数据库连接并执行建表语句
140+
try (Connection conn = DriverManager.getConnection(
141+
DatabaseController.DB_URL,
142+
DatabaseController.DB_USER,
143+
DatabaseController.DB_PASSWORD);
144+
Statement stmt = conn.createStatement()) {
145+
stmt.execute(query);
146+
System.out.println("💾 数据表【内容信息】初始化成功!");
147+
}
148+
}
149+
124150
} catch (Exception e) {
125151
System.err.println("❌ 数据表创建失败:" + e.getMessage());
126152
e.printStackTrace();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package xin.ctkqiang.dto;
2+
3+
public enum Database {
4+
MYSQL("MySQL"),
5+
SQLITE("SQLite");
6+
7+
private final String value;
8+
9+
Database(String value) {
10+
this.value = value;
11+
}
12+
13+
public String getValue() {
14+
return value;
15+
}
16+
17+
// 可选: 根据字符串反查 enum(比如传入 "MySQL" 返回 MYSQL 枚举值)
18+
public static Database fromValue(String value) {
19+
for (Database db : values()) {
20+
if (db.value.equalsIgnoreCase(value)) {
21+
return db;
22+
}
23+
}
24+
throw new IllegalArgumentException("Unknown database type: " + value);
25+
}
26+
}

0 commit comments

Comments
 (0)