当前位置:   article > 正文

Java 开发篇+一个简单的数据库管理系统ZDB_java数据库管理系统

java数据库管理系统

说明:本文供数据库爱好者和初级开发人员学习使用
标签:数据库管理系统、RDBMS、Java小程序、Java、Java程序
系统:Windows 11 x86
CPU :Intel
IDE :IntelliJ IDEA Community Edition 2024
语言:Java语言
标准:JDK 17
提示:如果您发现本文哪里写的有问题或者有更好的写法请留言或私信我进行修改优化


★ 程序界面

★ 相关文章

✔ C语言 开发篇+一个简单的数据库管理系统ZDB
✔ C++ 开发篇+一个简单的数据库管理系统ZDB
 

★ 功能简介

  1. 目前该Java小程序实现了RDBMS上表的几个基础功能,如:
  2. ✔ DDL:createdropaltertruncate、flashback、rename、purge
  3. ✔ DML:insertdeleteupdateselect

★ 程序特点

  1. ✔ 程序会在首次运行时在同级创建一个数据文件和一个字典文件
  2. ✔ 程序运行期间表数据存储在内存中,类似于Redis
  3. ✔ 程序运行结束时会将表数据采用序列化和反序列化方式存取
  4. ✔ 程序采用List集合实现表中行和列的动态调整
  5. ✔ 程序采用Set 集合实现表集合(表空间)大小的动态调整
  6. ✔ 程序采用Map 集合实现数据库参数集合的动态调整
  7. ✔ 表在drop后purge前支持闪回,purge后相关空间会被清理
  8. ✔ 表记录在删除后只是标记为删除,方便后期复用并减少数据shrink的性能消化

★ 程序试用

https://download.csdn.net/download/zzt_2009/89102377

★ 使用案例

  1. ############【欢迎使用 ZDB】############
  2. # 语言:Java 17 IDE:IntelliJ #
  3. # 作者:zzt_2009 版本:V 7.0.0 #
  4. # 主页:https://blog.csdn.net/zzt_2009 #
  5. # [H/h]查看帮助 [E/e]退出程序 #
  6. 字典文件不存在,初始化中……
  7. 字典落盘成功
  8. 数据文件不存在,初始化中……
  9. 数据落盘成功
  10. 首次使用,将为您展示帮助信息:
  11. # L :列出所有表的信息 > 案例:L T0 #
  12. # C :创建表并指定列名 > 案例:C T0 ID NAME #
  13. # D :删除表(支持闪回) > 案例:D T0 #
  14. # A :修改表的所有列名 > 案例:A T0 IDS NAMES #
  15. # R :重命名表的名称 > 案例:R T0 T00 #
  16. # T :清空表中的数据 > 案例:T T0 #
  17. # F :闪回被删除的表 > 案例:F T0_$0 #
  18. # P :清空被删除的表 > 案例:P #
  19. # i :往表中插入1行数据 > 案例:i T0 122 Traffic #
  20. # d :从表中删除1行数据 > 案例:d T0 3 #
  21. # u :更新表中的1行数据 > 案例:u T0 3 122 Traffic #
  22. # s :查看表中的有效数据 > 案例:s T0 #
  23. # S :查看表中的所有数据 > 案例:S T0 #
  24. # c :清理屏幕 > 案例:c #
  25. # H/h:查看帮助信息 > 案例:h #
  26. # E/e:退出程序 > 案例:e #
  27. SQL > L
  28. 表名: T0 编号: 0 状态: 2 Rows: 3 Cols: 2 [ TEL/NAME/ ]
  29. 表名: T1 编号: 1 状态: 2 Rows: 3 Cols: 2 [ country/city/ ]
  30. 对象的nextvalue:2
  31. 状态:0>未分配、1>新表、2>有数、3>删除、4>可复用
  32. SQL > C T2 ID NAME
  33. 表 T2 创建成功。
  34. 字典落盘成功
  35. 数据落盘成功
  36. 表名: T2 编号: 2 状态: 1 Rows: 0 Cols: 2 [ ID/NAME/ ]
  37. 表名: T0 编号: 0 状态: 2 Rows: 3 Cols: 2 [ TEL/NAME/ ]
  38. 表名: T1 编号: 1 状态: 2 Rows: 3 Cols: 2 [ country/city/ ]
  39. 对象的nextvalue:3
  40. 状态:0>未分配、1>新表、2>有数、3>删除、4>可复用
  41. SQL > C T3 ID NAME COMMENT
  42. 表 T3 创建成功。
  43. 字典落盘成功
  44. 数据落盘成功
  45. 表名: T2 编号: 2 状态: 1 Rows: 0 Cols: 2 [ ID/NAME/ ]
  46. 表名: T0 编号: 0 状态: 2 Rows: 3 Cols: 2 [ TEL/NAME/ ]
  47. 表名: T3 编号: 3 状态: 1 Rows: 0 Cols: 3 [ ID/NAME/COMMENT/ ]
  48. 表名: T1 编号: 1 状态: 2 Rows: 3 Cols: 2 [ country/city/ ]
  49. 对象的nextvalue:4
  50. 状态:0>未分配、1>新表、2>有数、3>删除、4>可复用
  51. SQL > D T2
  52. 表 T2 删除成功。
  53. 字典落盘成功
  54. 数据落盘成功
  55. 表名: T2_$2 编号: 2 状态: 3 Rows: 0 Cols: 2 [ ID/NAME/ ]
  56. 表名: T0 编号: 0 状态: 2 Rows: 3 Cols: 2 [ TEL/NAME/ ]
  57. 表名: T3 编号: 3 状态: 1 Rows: 0 Cols: 3 [ ID/NAME/COMMENT/ ]
  58. 表名: T1 编号: 1 状态: 2 Rows: 3 Cols: 2 [ country/city/ ]
  59. 对象的nextvalue:4
  60. 状态:0>未分配、1>新表、2>有数、3>删除、4>可复用
  61. SQL > F T2_$2
  62. 表 T2_$2 已被恢复。
  63. 字典落盘成功
  64. 数据落盘成功
  65. 表名: T2_$2 编号: 2 状态: 1 Rows: 0 Cols: 2 [ ID/NAME/ ]
  66. 表名: T0 编号: 0 状态: 2 Rows: 3 Cols: 2 [ TEL/NAME/ ]
  67. 表名: T3 编号: 3 状态: 1 Rows: 0 Cols: 3 [ ID/NAME/COMMENT/ ]
  68. 表名: T1 编号: 1 状态: 2 Rows: 3 Cols: 2 [ country/city/ ]
  69. 对象的nextvalue:4
  70. 状态:0>未分配、1>新表、2>有数、3>删除、4>可复用
  71. SQL > R T2_$2 T2
  72. 表 T2_$2 更名为 T2 成功。
  73. 字典落盘成功
  74. 数据落盘成功
  75. 表名: T2 编号: 2 状态: 1 Rows: 0 Cols: 2 [ ID/NAME/ ]
  76. 表名: T0 编号: 0 状态: 2 Rows: 3 Cols: 2 [ TEL/NAME/ ]
  77. 表名: T3 编号: 3 状态: 1 Rows: 0 Cols: 3 [ ID/NAME/COMMENT/ ]
  78. 表名: T1 编号: 1 状态: 2 Rows: 3 Cols: 2 [ country/city/ ]
  79. 对象的nextvalue:4
  80. 状态:0>未分配、1>新表、2>有数、3>删除、4>可复用
  81. SQL > s T0
  82. 行号:0 数据:[H, TEL, NAME]
  83. 行号:1 数据:[A, 110, police]
  84. 行号:2 数据:[A, 120, ambulance]
  85. 行号:3 数据:[A, 119, fire]
  86. SQL > d T0 2
  87. 相关行数据已删除。
  88. 字典落盘成功
  89. 数据落盘成功
  90. 行号:0 数据:[H, TEL, NAME]
  91. 行号:1 数据:[A, 110, police]
  92. 行号:2 数据:[A, 119, fire]
  93. SQL > S T0
  94. 行号:0 数据:[H, TEL, NAME]
  95. 行号:1 数据:[A, 110, police]
  96. 行号:2 数据:[D, 120, ambulance]
  97. 行号:3 数据:[A, 119, fire]
  98. SQL > i T0 122 car
  99. 0 / H
  100. 1 / A
  101. 2 / D
  102. 数据插入表 T0 成功。
  103. 字典落盘成功
  104. 数据落盘成功
  105. 行号:0 数据:[H, TEL, NAME]
  106. 行号:1 数据:[A, 110, police]
  107. 行号:2 数据:[A, 122, car]
  108. 行号:3 数据:[A, 119, fire]
  109. SQL > u T0 2 120 doctor
  110. 数据更新成功。
  111. 字典落盘成功
  112. 数据落盘成功
  113. 行号:0 数据:[H, TEL, NAME]
  114. 行号:1 数据:[A, 110, police]
  115. 行号:2 数据:[A, 120, doctor]
  116. 行号:3 数据:[A, 119, fire]
  117. SQL > T T0
  118. 表 T0 已被截断。
  119. 字典落盘成功
  120. 数据落盘成功
  121. 表名: T2 编号: 2 状态: 1 Rows: 0 Cols: 2 [ ID/NAME/ ]
  122. 表名: T0 编号: 0 状态: 2 Rows: 0 Cols: 2 [ TEL/NAME/ ]
  123. 表名: T3 编号: 3 状态: 1 Rows: 0 Cols: 3 [ ID/NAME/COMMENT/ ]
  124. 表名: T1 编号: 1 状态: 2 Rows: 3 Cols: 2 [ country/city/ ]
  125. 对象的nextvalue:4
  126. 状态:0>未分配、1>新表、2>有数、3>删除、4>可复用
  127. SQL > S T0
  128. 行号:0 数据:[H, TEL, NAME]
  129. SQL > e
  130. 字典落盘成功
  131. 数据落盘成功
  132. #########【感谢使用 ZDB 再见!】#########

★ 程序源码

  1. package zdb_default;
  2. import java.io.*;
  3. import java.util.*;
  4. // 字典类(程序较为简单为了减少文件切换使用内部类方式)
  5. class Class_Dict implements Serializable { // 实现 Serializable 接口,这是进行对象序列化和反序列化所必需的。
  6. private static final long serialVersionUID = 1L; // 为了在版本升级时保持版本的兼容性,在序列化类中添加 serialVersionUID
  7. public int value; //值
  8. public String comment; //描述
  9. // 添加构造函数和getter/setter方法(如果需要)
  10. public Class_Dict(int value, String comment) {
  11. this.value = value;
  12. this.comment = comment;
  13. }
  14. // 如果需要,还可以添加toString方法
  15. @Override
  16. public String toString() {
  17. return "Class_Dict{" +
  18. "value=" + value +
  19. ", comment='" + comment + '\'' +
  20. '}';
  21. }
  22. }
  23. // 表类(程序较为简单为了减少文件切换使用内部类方式)
  24. class Class_Table implements Serializable { // 实现 Serializable 接口,这是进行对象序列化和反序列化所必需的。
  25. private static final long serialVersionUID = 1L; // 为了在版本升级时保持版本的兼容性,在序列化类中添加 serialVersionUID
  26. public int id; // 序号
  27. public int state; // 状态:0>未分配、1>新表、2>有数、3>删除、4>可复用
  28. public String name; // 名称
  29. public int oid; // 对象编号
  30. public int rows; // 行数
  31. public int cols; // 列数
  32. public List<List<String>> record = new ArrayList<>(); // 数据&状态:"">未分配、H>字段名、A>可用、D>删除
  33. }
  34. public class Class_Main {
  35. // 定义常量
  36. private static final String DICT_FILE = "ZDB.dic";
  37. private static final String DATA_FILE = "ZDB.dat";
  38. // 创建一个Set集合来存储Class_Dict对象(参数)
  39. private static Map<String, Class_Dict> dicts = new HashMap<>();
  40. // 创建一个Set集合来存储Class_Table对象(表)
  41. private static Set<Class_Table> tables = new HashSet<>();
  42. public static void main(String[] args) {
  43. // 显示头信息
  44. printHead();
  45. // 从文件中读取字典信息到集合中
  46. readDicts();
  47. // 从文件中读取表信息到集合中
  48. tables = readTables();
  49. // 读取输入
  50. while (true) {
  51. String table_name;
  52. System.out.print("SQL > ");
  53. Scanner scanner = new Scanner(System.in);
  54. String command = scanner.nextLine();
  55. String[] tokens = command.split(" ");
  56. String operation = tokens[0];
  57. switch (operation) {
  58. case "C": // 创建新表,用法:C T0 id name
  59. createTable(tokens);
  60. break;
  61. case "D": // 删除表,用法:D T0
  62. dropTable(tokens);
  63. break;
  64. case "A": // 修改表列名,用法:A T0 new_id new_name
  65. alterTable(tokens);
  66. break;
  67. case "R": // 重命名表,用法:R T0 T00
  68. renameTable(tokens);
  69. break;
  70. case "T": // 清空表数据,用法:T T0
  71. truncateTable(tokens);
  72. break;
  73. case "F": // 闪回删除的表,用法:F T0
  74. flashbackTable(tokens);
  75. break;
  76. case "P": // 清空所有已删除的表,用法:P
  77. purgeTables();
  78. break;
  79. case "L": // 列出库中所有表名和其基础信息,用法:L
  80. listTables();
  81. break;
  82. case "i": // 往表插入数据,用法:i T0 1 a 2 b 3 c
  83. insertTable(tokens);
  84. break;
  85. case "d": // 删除表中第N行数据,用法:d T0 N
  86. deleteTable(tokens);
  87. break;
  88. case "u": // 更新表中第N行数据,用法:u T0 N 1 a
  89. updateTable(tokens);
  90. break;
  91. case "s": // 查询表中所有可见数据,用法:s T0
  92. selectTable(tokens);
  93. break;
  94. case "S": // 查询表中所有数据,用法:S T0
  95. selectAllTable(tokens);
  96. break;
  97. case "c": // 清理屏幕并重新显示printHead()
  98. printHead();
  99. break;
  100. case "h": //
  101. case "H": //
  102. printHelp();
  103. break;
  104. case "e": //
  105. case "E": //
  106. writeDicts();
  107. writeTables();
  108. printEnd();
  109. System.exit(0);
  110. default:
  111. System.out.println("命令不正确请重新输入,或按[H/h]查看帮助");
  112. break;
  113. }
  114. } //循环结束
  115. } //main_end
  116. public static void createTable(String[] tokens) {
  117. if (tokens.length < 3) {
  118. System.out.println("输入有误,案例:C T0 ID NAME");
  119. return;
  120. }
  121. String tableName = tokens[1];
  122. for (Class_Table table : tables) {
  123. if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
  124. System.out.println("表 " + tableName + " 已存在。");
  125. return;
  126. }
  127. }
  128. Class_Table newTable = new Class_Table();
  129. newTable.state = 1;
  130. newTable.name = tableName;
  131. newTable.oid = dicts.get("noid").value;
  132. newTable.rows = 0;
  133. newTable.cols = (tokens.length - 2);
  134. dicts.put("noid", new Class_Dict((dicts.get("noid").value + 1), "对象编号的nextvalue"));
  135. List<String> header = new ArrayList<>();
  136. header.add("H");
  137. header.addAll(Arrays.asList(tokens).subList(2, tokens.length));
  138. newTable.record.add(header);
  139. tables.add(newTable);
  140. System.out.println("表 " + tableName + " 创建成功。");
  141. writeDicts();
  142. writeTables();
  143. listTables();
  144. }
  145. public static void dropTable(String[] tokens) {
  146. if (tokens.length != 2) {
  147. System.out.println("输入有误,案例:D T0");
  148. return;
  149. }
  150. String tableName = tokens[1];
  151. boolean found = false;
  152. for (Class_Table table : tables) {
  153. if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
  154. table.state = 3;
  155. table.name = table.name + "_$" + table.oid;
  156. found = true;
  157. break;
  158. }
  159. }
  160. if (found) {
  161. System.out.println("表 " + tableName + " 删除成功。");
  162. writeDicts();
  163. writeTables();
  164. listTables();
  165. } else {
  166. System.out.println("表 " + tableName + " 不存在或已被删除。");
  167. }
  168. }
  169. public static void renameTable(String[] tokens) {
  170. if (tokens.length != 3) {
  171. System.out.println("命令错误,请检查参数数量。");
  172. return;
  173. }
  174. String oldName = tokens[1];
  175. String newName = tokens[2];
  176. for (Class_Table table : tables) {
  177. if ((table.name.equals(oldName)) && ((table.state == 1) || (table.state == 2))) {
  178. table.name = newName;
  179. System.out.println("表 " + oldName + " 更名为 " + newName + " 成功。");
  180. writeDicts();
  181. writeTables();
  182. listTables();
  183. return;
  184. }
  185. }
  186. System.out.println("表 " + oldName + " 不存在。");
  187. }
  188. public static void alterTable(String[] tokens) {
  189. if (tokens.length < 3) {
  190. System.out.println("输入有误,案例:A T0 new_col1 new_col2");
  191. return;
  192. }
  193. String tableName = tokens[1];
  194. for (Class_Table table : tables) {
  195. if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
  196. if ((tokens.length - 2) == table.cols) {
  197. List<String> header = new ArrayList<>();
  198. header.add("H");
  199. header.addAll(Arrays.asList(tokens).subList(2, tokens.length));
  200. table.record.set(0, header);
  201. writeDicts();
  202. writeTables();
  203. listTables();
  204. } else {
  205. System.out.println("新列数量须和旧列数量一致!");
  206. }
  207. return;
  208. }
  209. }
  210. System.out.println("表 " + tableName + " 不存在。");
  211. }
  212. public static void truncateTable(String[] tokens) {
  213. if (tokens.length != 2) {
  214. System.out.println("输入有误,案例:T T0");
  215. return;
  216. }
  217. String tableName = tokens[1];
  218. boolean tableFound = false;
  219. for (Class_Table table : tables) {
  220. if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
  221. tableFound = true;
  222. List<String> header = table.record.get(0);
  223. table.record.clear();
  224. table.rows = 0;
  225. table.record.add(header);
  226. System.out.println("表 " + tableName + " 已被截断。");
  227. writeDicts();
  228. writeTables();
  229. listTables();
  230. break;
  231. }
  232. }
  233. if (!tableFound) {
  234. System.out.println("表 " + tableName + " 不存在。");
  235. }
  236. }
  237. public static void flashbackTable(String[] tokens) {
  238. if (tokens.length != 2) {
  239. System.out.println("输入有误,案例:F T0");
  240. return;
  241. }
  242. String tableName = tokens[1];
  243. boolean tableFound = false;
  244. for (Class_Table table : tables) {
  245. if (table.name.equals(tableName) && table.state == 3) {
  246. tableFound = true;
  247. if (table.rows == 0) {
  248. table.state = 1;
  249. } else {
  250. table.state = 2;
  251. }
  252. System.out.println("表 " + tableName + " 已被恢复。");
  253. writeDicts();
  254. writeTables();
  255. listTables();
  256. break;
  257. }
  258. }
  259. if (!tableFound) {
  260. System.out.println("表 " + tableName + " 不存在或未被删除。");
  261. }
  262. }
  263. public static void purgeTables() {
  264. tables.removeIf(table -> table.state == 3);
  265. System.out.println("所有标记为删除的表已清除。");
  266. }
  267. public static void listTables() {
  268. tables.forEach(table -> {
  269. System.out.print("表名: " + table.name + "\t");
  270. System.out.print("编号: " + table.oid + "\t");
  271. System.out.print("状态: " + table.state + "\t");
  272. System.out.print("Rows: " + table.rows + "\t");
  273. System.out.print("Cols: " + table.cols + " ");
  274. System.out.print("[ ");
  275. for (int i = 1; i < table.record.get(0).size(); i++) {
  276. System.out.print(table.record.get(0).get(i) + "/");
  277. }
  278. System.out.println(" ]");
  279. });
  280. System.out.println("对象的nextvalue:" + dicts.get("noid").value);
  281. System.out.println("状态:0>未分配、1>新表、2>有数、3>删除、4>可复用");
  282. }
  283. public static void insertTable(String[] tokens) {
  284. if (tokens.length < 3) {
  285. System.out.println("输入有误,案例:i T0 911 fire");
  286. return;
  287. }
  288. String tableName = tokens[1];
  289. for (Class_Table table : tables) {
  290. if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
  291. if ((tokens.length - 2) == table.cols) {
  292. for (int i = 0; i < table.record.size(); i++) {
  293. System.out.println(i + " / " + table.record.get(i).get(0));
  294. if (Objects.equals(table.record.get(i).get(0), "D")) {
  295. List<String> rowData = new ArrayList<>();
  296. rowData.add(0, "A");
  297. rowData.addAll(Arrays.asList(tokens).subList(2, tokens.length));
  298. table.record.set(i, rowData);
  299. table.rows++;
  300. System.out.println("数据插入表 " + tableName + " 成功。");
  301. writeDicts();
  302. writeTables();
  303. selectTable(("X " + tableName).split(" "));
  304. return;
  305. }
  306. }
  307. List<String> rowData = new ArrayList<>();
  308. rowData.add(0, "A");
  309. rowData.addAll(Arrays.asList(tokens).subList(2, tokens.length));
  310. table.record.add(rowData);
  311. table.rows++;
  312. System.out.println("数据插入表 " + tableName + " 成功。");
  313. writeDicts();
  314. writeTables();
  315. selectTable(("X " + tableName).split(" "));
  316. } else {
  317. System.out.println("列数量不一致!");
  318. }
  319. return;
  320. }
  321. }
  322. System.out.println("表 " + tableName + " 不存在。");
  323. }
  324. public static void deleteTable(String[] tokens) {
  325. if (tokens.length != 3) {
  326. System.out.println("输入有误,案例:d T0 1");
  327. return;
  328. }
  329. String tableName = tokens[1];
  330. String rowNumber = tokens[2];
  331. int irowNumber = Integer.parseInt(rowNumber);
  332. boolean tableFound = false;
  333. for (Class_Table table : tables) {
  334. if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
  335. if ((irowNumber != 0) && (irowNumber <= table.rows)) {
  336. tableFound = true;
  337. table.record.get(irowNumber).set(0, "D");
  338. table.rows--;
  339. System.out.println("相关行数据已删除。");
  340. writeDicts();
  341. writeTables();
  342. selectTable(("X " + tableName).split(" "));
  343. return;
  344. } else {
  345. System.out.println("不可操作的行号。");
  346. return;
  347. }
  348. }
  349. }
  350. if (!tableFound) {
  351. System.out.println("表 " + tableName + " 不存在。");
  352. }
  353. }
  354. public static void updateTable(String[] tokens) {
  355. if (tokens.length < 4) {
  356. System.out.println("输入有误,案例:u T0 1 110 police");
  357. return;
  358. }
  359. String tableName = tokens[1];
  360. String rowNumber = tokens[2];
  361. int irowNumber = Integer.parseInt(rowNumber);
  362. boolean tableFound = false;
  363. for (Class_Table table : tables) {
  364. if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
  365. tableFound = true;
  366. if (table.record.get(irowNumber).get(0).equals("A")) {
  367. if ((tokens.length - 3) == table.cols) {
  368. List<String> rowData = new ArrayList<>();
  369. rowData.add(0, "A");
  370. rowData.addAll(Arrays.asList(tokens).subList(3, tokens.length));
  371. table.record.set(irowNumber, rowData);
  372. System.out.println("数据更新成功。");
  373. writeDicts();
  374. writeTables();
  375. selectTable(("X " + tableName).split(" "));
  376. } else {
  377. System.out.println("列数量不一致!");
  378. }
  379. return;
  380. }
  381. }
  382. }
  383. if (!tableFound) {
  384. System.out.println("表 " + tableName + " 不存在。");
  385. }
  386. }
  387. public static void selectTable(String[] tokens) {
  388. if (tokens.length != 2) {
  389. System.out.println("输入有误,案例:s T0");
  390. return;
  391. }
  392. String tableName = tokens[1];
  393. boolean tableFound = false;
  394. for (Class_Table table : tables) {
  395. if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
  396. tableFound = true;
  397. int i = 0;
  398. for (List<String> row : table.record) {
  399. if ((row.get(0).equals("H")) || (row.get(0).equals("A"))) {
  400. System.out.println("行号:" + i + "\t数据:" + row);
  401. i++;
  402. }
  403. }
  404. break;
  405. }
  406. }
  407. if (!tableFound) {
  408. System.out.println("表 " + tableName + " 不存在。");
  409. }
  410. }
  411. public static void selectAllTable(String[] tokens) {
  412. if (tokens.length != 2) {
  413. System.out.println("输入有误,案例:s T0");
  414. return;
  415. }
  416. String tableName = tokens[1];
  417. boolean tableFound = false;
  418. for (Class_Table table : tables) {
  419. if ((table.name.equals(tableName)) && ((table.state == 1) || (table.state == 2))) {
  420. tableFound = true;
  421. int i = 0;
  422. for (List<String> row : table.record) {
  423. System.out.println("行号:" + i + "\t数据:" + row);
  424. i++;
  425. }
  426. break;
  427. }
  428. }
  429. if (!tableFound) {
  430. System.out.println("表 " + tableName + " 不存在。");
  431. }
  432. }
  433. public static void printHead() {
  434. try {
  435. new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
  436. } catch (IOException | InterruptedException e) {
  437. e.printStackTrace();
  438. }
  439. System.out.println("############【欢迎使用 ZDB】############");
  440. System.out.println("# 语言:Java 17 IDE:IntelliJ #");
  441. System.out.println("# 作者:zzt_2009 版本:V 7.0.0 #");
  442. System.out.println("# 主页:https://blog.csdn.net/zzt_2009 #");
  443. System.out.println("# [H/h]查看帮助 [E/e]退出程序 #");
  444. }
  445. public static void printEnd() {
  446. System.out.println("#########【感谢使用 ZDB 再见!】#########");
  447. }
  448. public static void printHelp() {
  449. System.out.println("# L :列出所有表的信息 > 案例:L T0 #");
  450. System.out.println("# C :创建表并指定列名 > 案例:C T0 ID NAME #");
  451. System.out.println("# D :删除表(支持闪回) > 案例:D T0 #");
  452. System.out.println("# A :修改表的所有列名 > 案例:A T0 IDS NAMES #");
  453. System.out.println("# R :重命名表的名称 > 案例:R T0 T00 #");
  454. System.out.println("# T :清空表中的数据 > 案例:T T0 #");
  455. System.out.println("# F :闪回被删除的表 > 案例:F T0_$0 #");
  456. System.out.println("# P :清空被删除的表 > 案例:P #");
  457. System.out.println("# i :往表中插入1行数据 > 案例:i T0 122 Traffic #");
  458. System.out.println("# d :从表中删除1行数据 > 案例:d T0 3 #");
  459. System.out.println("# u :更新表中的1行数据 > 案例:u T0 3 122 Traffic #");
  460. System.out.println("# s :查看表中的有效数据 > 案例:s T0 #");
  461. System.out.println("# S :查看表中的所有数据 > 案例:S T0 #");
  462. System.out.println("# c :清理屏幕 > 案例:c #");
  463. System.out.println("# H/h:查看帮助信息 > 案例:h #");
  464. System.out.println("# E/e:退出程序 > 案例:e #");
  465. }
  466. public static void initDicts() {
  467. dicts.put("noid", new Class_Dict(2, "对象编号的nextvalue"));
  468. }
  469. public static void writeDicts() {
  470. try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(DICT_FILE))) {
  471. oos.writeObject(dicts);
  472. System.out.println("字典落盘成功");
  473. } catch (IOException e) {
  474. e.printStackTrace();
  475. }
  476. }
  477. public static void readDicts() {
  478. try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(DICT_FILE))) {
  479. dicts = (Map<String, Class_Dict>) ois.readObject();
  480. } catch (IOException | ClassNotFoundException e) {
  481. System.out.println("字典文件不存在,初始化中……");
  482. initDicts();
  483. writeDicts();
  484. }
  485. }
  486. public static void initTables() {
  487. Class_Table T0 = new Class_Table();
  488. T0.id = 0;
  489. T0.state = 2;
  490. T0.name = "T0";
  491. T0.oid = 0;
  492. T0.rows = 3;
  493. T0.cols = 2;
  494. T0.record.add(new ArrayList<>());
  495. T0.record.get(0).add("H");
  496. T0.record.get(0).add("TEL");
  497. T0.record.get(0).add("NAME");
  498. T0.record.add(new ArrayList<>());
  499. T0.record.get(1).add("A");
  500. T0.record.get(1).add("110");
  501. T0.record.get(1).add("police");
  502. T0.record.add(new ArrayList<>());
  503. T0.record.get(2).add("A");
  504. T0.record.get(2).add("120");
  505. T0.record.get(2).add("ambulance");
  506. T0.record.add(new ArrayList<>());
  507. T0.record.get(3).add("A");
  508. T0.record.get(3).add("119");
  509. T0.record.get(3).add("fire");
  510. tables.add(T0);
  511. Class_Table T1 = new Class_Table();
  512. T1.id = 1;
  513. T1.state = 2;
  514. T1.name = "T1";
  515. T1.oid = 1;
  516. T1.rows = 3;
  517. T1.cols = 2;
  518. T1.record.add(new ArrayList<>());
  519. T1.record.get(0).add("H");
  520. T1.record.get(0).add("country");
  521. T1.record.get(0).add("city");
  522. T1.record.add(new ArrayList<>());
  523. T1.record.get(1).add("A");
  524. T1.record.get(1).add("China");
  525. T1.record.get(1).add("Beijing");
  526. T1.record.add(new ArrayList<>());
  527. T1.record.get(2).add("A");
  528. T1.record.get(2).add("America");
  529. T1.record.get(2).add("Washington");
  530. T1.record.add(new ArrayList<>());
  531. T1.record.get(3).add("A");
  532. T1.record.get(3).add("Russia");
  533. T1.record.get(3).add("Moscow");
  534. tables.add(T1);
  535. }
  536. public static void writeTables() {
  537. try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(DATA_FILE))) {
  538. oos.writeObject(tables);
  539. System.out.println("数据落盘成功");
  540. } catch (IOException e) {
  541. e.printStackTrace();
  542. }
  543. }
  544. @SuppressWarnings("unchecked")
  545. public static HashSet<Class_Table> readTables() {
  546. Set<Class_Table> loadedTables = new HashSet<>();
  547. try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(DATA_FILE))) {
  548. loadedTables = (Set<Class_Table>) ois.readObject();
  549. } catch (IOException | ClassNotFoundException e) {
  550. System.out.println("数据文件不存在,初始化中……");
  551. initTables();
  552. writeTables();
  553. loadedTables = readTables();
  554. System.out.println("首次使用,将为您展示帮助信息:");
  555. printHelp();
  556. }
  557. return (HashSet<Class_Table>) loadedTables;
  558. }
  559. } //Class_Main_end
  560. //end

※ 如果您觉得文章写的还不错, 别忘了在文末给作者点个赞哦 ~

20200426194203245.gif

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/662515
推荐阅读
相关标签
  

闽ICP备14008679号