简体   繁体   English

每次启动应用程序时如何制作一个新的数据库

[英]How to make a fresh database every time the application is started

I'm making a swing application in which the user inputs a file (.html) and the application parses the data in the file and interprets into a database. 我正在制作一个swing应用程序,其中用户输入一个文件(.html),该应用程序解析文件中的数据并解释为数据库。 Then there are a bunch of queries that are run on the database. 然后,在数据库上运行了一堆查询。 This question tells what kind of queries I need to run over the database. 这个问题告诉我需要在数据库上运行哪种查询。

Following is how the 以下是

my questions are following - 我的问题如下-

  1. How can I embed an empty database into my project, which is emptied every time the user quits the application? 如何将一个空数据库嵌入到我的项目中,该数据库在用户每次退出应用程序时都会清空? Basically I don't want the user to have to install a database separately or setup connections and stuff. 基本上,我不希望用户必须单独安装数据库或设置连接和设置。

  2. Will it be a better idea to NOT parse the html file into a database and rather convert it into a different data structure? 不将html文件解析为数据库,而是将其转换为其他数据结构,是一个更好的主意吗?

  3. Since the results of queries over the database are supposed to be numbers only (I don't actually need to names of people or the Ticket#), I feel there is not much effort that needs to go into the database part. 由于对数据库的查询结果应该只是数字(我实际上不需要输入人名或Ticket#),因此我认为无需太多精力就可以进入数据库部分。 So basically I want to create a separate module (so to speak), that -- Takes the file from the application -- Parses the html into a database (or a different data structure) - pulls the numbers after running the appropriate queries -- gives those numbers to the application. 因此,基本上我想创建一个单独的模块(可以这么说),-从应用程序中获取文件-将html解析到数据库(或其他数据结构)中-在运行适当的查询后提取数字-将这些数字提供给应用程序。 Is this feasible? 这可行吗?

Question 1 问题1

You'll probably have something like the following class in your app. 您的应用程序中可能会有类似以下类的内容。 Just overwrite the startup () method and perform your clean logic here. 只需覆盖startup ()方法并在此处执行干净的逻辑即可。

public class YourSwingApp extends SingleFrameApplication {
/**
 * At startup clean the DB.
 */
@Override
protected void startup() {
    show(new YourSwingApp(this));
    // issue a set of 'truncate table' or 'delete from table' query here
    // you may want to create a Singleton to centralize the DB operation
}
}

Question 2 问题2

If you discard the data everytime the user quits yout application you probably don't need a persistence layer at all. 如果您在用户每次退出应用程序时都丢弃数据,则可能根本不需要持久层。 Just keep the data model in memory and you're good. 只要将数据模型保存在内存中就可以了。 If you want something lighter than a DB, but persistent, you may want to use Object Serialization . 如果您想要的东西比数据库轻,但是持久,则可能需要使用Object Serialization

Question 3 问题3

The term "module" makes me think to OSGI which it may be an overkill here. 术语“模块”使我想到OSGI,这在这里可能是一个过大的杀伤力。 Just separate the project in a lib and create an interface to implement. 只需将项目分隔为一个lib,然后创建一个要实现的接口即可。 In this way your GUI can use the interface to get the results. 这样,您的GUI可以使用该界面来获取结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM