简体   繁体   English

带有嵌入式Derby数据库的Java应用程序

[英]Java application with embedded Derby database

I created a java swing application with apache derby database. 我用apache derby数据库创建了一个Java swing应用程序。 I want to know how can i do the following things. 我想知道我该怎么做以下事情。

The first time the application is double-clicked by a user, that finds a suitable location for the Derby database on the user's machine, creates the database and defines all the tables, views, etc and loads any initial data. 用户首次双击该应用程序时,将在用户计算机上找到适合Derby数据库的位置,创建该数据库并定义所有表,视图等并加载任何初始数据。 Then, on subsequent runs of the application, it will be able to re-open the database and continue using it. 然后,在应用程序的后续运行中,它将能够重新打开数据库并继续使用它。

  • You could use the user.home property from System.getProperty which will return you the user's home directory as the main path for the database. 您可以使用System.getPropertyuser.home属性,该属性将返回用户的主目录作为数据库的主路径。 On Windows, I would recommend using {user.home}\\AppData\\Remote\\{You application name} as a base path 在Windows上,我建议使用{user.home}\\AppData\\Remote\\{You application name}作为基本路径
  • Derby creates a directory of the same name as the database, you could check for the existence of this directory. Derby创建与数据库同名的目录,您可以检查该目录是否存在。 The problem with this is there is no guarantee that it contains a valid database. 问题是不能保证它包含有效的数据库。
  • You could create a normal connection to the database and check for the existence of existing tables and build them as required. 您可以创建与数据库的普通连接,并检查是否存在现有表并根据需要构建它们。 This ensures that if, for some reason, not all the tables where created, you can recover at that point. 这样可以确保,如果由于某种原因(不是所有的表都已创建),您都可以在那时恢复。

Following shall start derby database service on default port (1527) on localhost 以下应在localhost的默认端口(1527)上启动derby数据库服务

    NetworkServerControl obj= new NetworkServerControl();
    obj.start(null);

Use dburl with 'create=true', ie 将dburl与'create = true'一起使用,即

    jdbc:derby://localhost:1527/macdb;create=true 

Above shall make db dir (macdb), same name as service name on current dir. 上面将使db dir(macdb)与当前目录上的服务名相同。

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

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