Skip to content

Commit 0a0c0b7

Browse files
committed
refactor(database): 重构数据库配置和连接逻辑以支持多数据库模式
将 Configuration 类中的 DatabaseMode 改为静态变量,并调整相关方法为静态方法 重构 DatabaseController 的连接逻辑,根据配置动态选择 MySQL 或 SQLite 数据库 更新 Main 类使用静态配置方法,并完善使用说明和联系信息 同时更新 .gitignore 忽略 SQLite 数据库文件 ``` 这个提交消息: 1. 使用 refactor 类型,因为主要是代码结构调整和优化 2. 添加了 scope(database) 说明修改范围 3. 简明扼要地描述了主要修改内容 4. 在正文中详细说明了具体的重构点 5. 保持了简洁和专业性,同时涵盖了所有重要修改 参考 #2#1
1 parent 2f41080 commit 0a0c0b7

File tree

4 files changed

+82
-34
lines changed

4 files changed

+82
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,4 @@ out/
6868

6969
# 临时环境变量文件
7070
.env
71+
*.sqlite

src/main/java/xin/ctkqiang/Main.java

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ public class Main {
1313
public static final String VERSION = "1.0";
1414
public static final String TITLE = "ExploitDB 搜索工具";
1515
public static final String AUTHOR = "钟智强";
16+
public static final String EMAIL = "[email protected]";
17+
public static final String GITHUB = "https://github.com/ctkqiang";
18+
public static final String GITCODE = "https://gitcode.com/ctkqiang_sr";
1619
public static final String DESCRIPTION = "ExploitDB 搜索工具";
17-
public static final String USAGE = "mvn clean compile exec:java";
20+
public static final String USAGE = "mvn clean compile exec:java 或 java -jar {FILENAME}.jar";
1821

1922
public static final Scanner scanner = new Scanner(System.in);
2023

@@ -25,22 +28,24 @@ public class Main {
2528

2629
private static List<String> allowed = Arrays.asList("csv", "json", "sql");
2730

28-
private static Configuration Config = new Configuration();;
29-
3031
private static void PrintBanner() {
3132
System.out.println();
3233
System.out.println("╭─────────────────────────────────────────────────────────────────────────╮");
33-
System.out.println("│ (๑˃̵ᴗ˂̵)و 欢迎来到灵儿的安全世界呀~♡");
34-
System.out.println("│ ");
34+
System.out.println("│ (๑˃̵ᴗ˂̵)و 欢迎来到灵儿的安全世界呀~♡ (づ。◕‿‿◕。)づ ");
35+
System.out.println("│ ");
3536
System.out.println("│ 工具名称:" + TITLE + " v" + VERSION);
36-
System.out.println("│ 作者昵称:" + AUTHOR);
37+
System.out.println("│ 作者昵称:" + AUTHOR + " (๑•̀ㅂ•́)و✧");
3738
System.out.println("│ 简介说明:" + DESCRIPTION);
38-
System.out.println("│ ");
39-
System.out.println("│ 💌 声明一下哦,我可是乖乖的好孩子呢! ");
40-
System.out.println("│ 这个工具是用来做好事的,不是用来做坏事的~ ");
41-
System.out.println("│ 但是,如果有人坏坏地用它去做不好的事情, ");
42-
System.out.println("│ 哥哥姐姐们可要自己负责啦,我是不背锅的小可爱哟~(๑•̀ㅂ•́)و✧ ");
43-
System.out.println("│ 我不对任何恶意使用、违法操作产生的后果负责哟~请乖乖做正义的萌新吧!");
39+
System.out.println("│ ");
40+
System.out.println("│ 📧 邮箱联系: " + EMAIL);
41+
System.out.println("│ 💻 GitHub : " + GITHUB);
42+
System.out.println("│ 📂 GitCode : " + GITCODE);
43+
System.out.println("│ ");
44+
System.out.println("│ 🛠️ 使用方法: " + USAGE);
45+
System.out.println("│ ");
46+
System.out.println("│ 💌 小声明哟~我是乖乖的好孩子,这工具是帮大家做好事的~ ");
47+
System.out.println("│ 不是让坏蛋乱用的哦 (ง •̀_•́)ง ");
48+
System.out.println("│ 如果有人坏坏地用了,后果自负哒~我不背锅哦~请保持正义感~♡ ");
4449
System.out.println("╰─────────────────────────────────────────────────────────────────────────╯");
4550
System.out.println();
4651
}
@@ -50,18 +55,35 @@ public static void main(String[] args) {
5055
Main.PrintBanner();
5156

5257
/**
53-
* 设置数据库模式
54-
* 可选值请查阅 {@link xin.ctkqiang.dto.Database Database} 枚举类
58+
* 嘻嘻~这里是数据库模式设置哦
59+
* 你可以从 {@link xin.ctkqiang.dto.Database Database} 枚举里选一个最爱滴数据库
5560
* <ul>
56-
* <li>{@link xin.ctkqiang.dto.Database#MYSQL MYSQL} :标准的 MySQL 数据库</li>
57-
* <li>{@link xin.ctkqiang.dto.Database#SQLite SQLite} :轻量级嵌入式数据库</li>
61+
* <li>{@link xin.ctkqiang.dto.Database#MYSQL MYSQL} :咱们平时用的 MySQL
62+
* 数据库,默认就选它啦~</li>
63+
* <li>{@link xin.ctkqiang.dto.Database#SQLITE SQLITE} :轻巧灵活的 SQLite
64+
* 嵌入式数据库,萌萌哒!</li>
5865
* </ul>
59-
* 如果想用 SQLite,就把这里改成 {@link xin.ctkqiang.dto.Database#SQLite Database.SQLITE}
60-
* 哦~ ✨🤭⚡️🌸🌸🌸
66+
*
67+
* 小提示✨:默认模式是 MYSQL 哦,要用 SQLite 的话,记得把下面的代码改成
68+
* <code>Configuration.setDatabaseMode(Database.SQLITE);</code> 才能生效呢~
69+
*
70+
* 举个栗子给你看👇:
71+
*
72+
* 乖乖用【默认】 MySQL 嘛 ~
73+
*
74+
* <pre>
75+
* Configuration.setDatabaseMode(Database.MYSQL);
76+
* </pre>
77+
*
78+
* 想用 SQLite ~
79+
*
80+
* <pre>
81+
* Configuration.setDatabaseMode(Database.SQLITE);
82+
* </pre>
83+
*
84+
* 嘻嘻,这样就能轻轻松松切换数据库, 你棒棒啦!🎀🌸💖
6185
*/
62-
63-
// Main.Config.setDatabaseMode(Database.MYSQL);
64-
Main.Config.setDatabaseMode(Database.SQLITE);
86+
Configuration.setDatabaseMode(Database.SQLITE);
6587

6688
int pages = userUtil.askForPositiveInt("📄 想爬多少页咧~?【建议填个50以上更爽!】");
6789
boolean isExport = userUtil.askForYesNo("💾 要不要顺便导出数据嘞?(支持 .csv/.json/.sql哦)✨:");

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,26 @@
33
import xin.ctkqiang.dto.Database;
44

55
public class Configuration {
6-
public String DatabaseMode = Database.MYSQL.getValue();
6+
private static String DatabaseMode = Database.MYSQL.getValue();
77

88
public static final String DB_NAME = "ExploitDB";
99
public static final String DB_URL = "jdbc:mysql://localhost:3306/" + DB_NAME
1010
+ "?serverTimezone=UTC&allowPublicKeyRetrieval=true&useSSL=false";
1111

1212
public static final String DB_SQLITE_URL = "jdbc:sqlite:ling.sqlite";
1313

14-
public static final String DB_USER = "root"; // 改成你的数据库用户名
15-
public static final String DB_PASSWORD = ""; // 改成你的数据库密码
14+
public static final String DB_USER = "root";
15+
public static final String DB_PASSWORD = "";
1616
public static final String DB_DRIVER = "com.mysql.cj.jdbc.Driver";
1717

1818
public static final boolean DEBUG = false;
1919

20-
public String getDatabaseMode() {
21-
return this.DatabaseMode;
20+
public static String getDatabaseMode() {
21+
return DatabaseMode;
2222
}
2323

24-
public void setDatabaseMode(Database databaseMode) {
25-
this.DatabaseMode = databaseMode.getValue();
26-
27-
System.out.println(String.format("🎀 你当前选择的数据库模式是:「%s」~酱酱 ♪(๑˃ᴗ˂)ﻭ \n", this.getDatabaseMode()));
24+
public static void setDatabaseMode(Database databaseMode) {
25+
DatabaseMode = databaseMode.getValue();
26+
System.out.println(String.format("🎀 你当前选择的数据库模式是:「%s」~酱酱 ♪(๑˃ᴗ˂)ﻭ \n", getDatabaseMode()));
2827
}
2928
}

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ public class DatabaseController {
4848
/** SQLITE 数据库连接URL */
4949
protected final static String DB_SQLITE_URL = Configuration.DB_SQLITE_URL;
5050

51-
private Configuration Config = new Configuration();;
52-
5351
/**
5452
* 静态初始化块
5553
* 在类加载时尝试加载数据库驱动
@@ -69,7 +67,35 @@ public class DatabaseController {
6967
* @throws SQLException 如果连接数据库时发生错误
7068
*/
7169
protected Connection getConnection() throws SQLException {
72-
return DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
70+
String mode = Configuration.getDatabaseMode();
71+
String upperMode = mode.toUpperCase();
72+
System.out.println("💡 切换数据库模式 -> " + upperMode);
73+
74+
switch (upperMode) {
75+
case "SQLITE":
76+
try {
77+
Class.forName("org.sqlite.JDBC");
78+
System.out.println("📦 嘻嘻~SQLite 驱动已加载好惹!我已经抱好数据库小可爱啦~ฅ(^・ω・^ฅ)");
79+
} catch (ClassNotFoundException e) {
80+
System.err.println("❌ 哎呀呀!SQLite JDBC 驱动不见惹~人家找不到驱动怎么贴贴数据库嘛喵呜呜。゚(゚´ω`゚)゚。");
81+
throw new SQLException("找不到 SQLite 驱动喵~要不要检查一下依赖有没有加对呀?");
82+
}
83+
return DriverManager.getConnection(DatabaseController.DB_SQLITE_URL);
84+
85+
case "MYSQL":
86+
default:
87+
try {
88+
Class.forName(DatabaseController.DB_DRIVER);
89+
System.out.println("📦 MySQL 驱动加载完成!我打扮好了,要去连接数据库小哥哥啦~(๑•̀ㅂ•́)و✧");
90+
} catch (ClassNotFoundException e) {
91+
System.err.println("❌ 呜呜呜 MySQL JDBC 驱动不见了~是不是打包的时候忘记带灵儿一起走啦 >///< ");
92+
throw new SQLException("MySQL 驱动加载失败了喵~快检查一下 `.jar` 有没有遗漏吧!");
93+
}
94+
return DriverManager.getConnection(
95+
DatabaseController.DB_URL,
96+
DatabaseController.DB_USER,
97+
DatabaseController.DB_PASSWORD);
98+
}
7399
}
74100

75101
/**
@@ -116,7 +142,7 @@ public void CreateTableIfNotExists() {
116142

117143
try {
118144

119-
String mode = this.Config.getDatabaseMode();
145+
String mode = Configuration.getDatabaseMode();
120146
Database dbMode;
121147

122148
try {

0 commit comments

Comments
 (0)