简体   繁体   English

使用数据库为 Java 程序创建安装程序

[英]Creating an installer for Java program with database

Recently I made an application in using Swing, AWT and JDBC that manages some database.最近我申请了使用 Swing、AWT 和 JDBC 来管理一些数据库。 I used PostgreSQL as the backend with JDBC drivers to connect to the database.我使用 PostgreSQL 作为后端,使用 JDBC 驱动程序连接到数据库。 What I want to do is create a setup/installer program so that the application can be installed and used on any pc.我想要做的是创建一个安装程序/安装程序,以便可以在任何电脑上安装和使用该应用程序。 My problem is I dont know how to integrate the database along?我的问题是我不知道如何整合数据库?

Any help would be appreciated.任何帮助,将不胜感激。

Writing an installer that also manages DB installation is a project in and of itself.编写一个同时管理数据库安装的安装程序本身就是一个项目。 The big question is do you need to install PostgreSQL from scratch or will you tell the user to install it themselves first?最大的问题是您需要从头开始安装 PostgreSQL 还是告诉用户先自己安装?

I have attempted to do something similar and what I eventually did was build an installer with NSIS that includes Postgres and a JRE in the install bundle along with my program.我试图做类似的事情,我最终做的是使用 NSIS 构建一个安装程序,其中包括 Postgres 和安装包中的 JRE 以及我的程序。 The installer copies everything to the user's install location and creates all the required config files.安装程序将所有内容复制到用户的安装位置并创建所有必需的配置文件。

The biggest issue I had was that I had a number of config options to present to the user that would have taken a lot of programming in NSIS.我遇到的最大问题是我有许多配置选项要呈现给用户,这些选项会在 NSIS 中进行大量编程。 So, I modified the ANT build scripts I already had written to build the project.因此,我修改了我已经为构建项目而编写的 ANT 构建脚本。 What NSIS does then is copy the JRE to a temp location and start a Java installer GUI I wrote instead that gathers up all the config info and launches ANT in the background. NSIS 然后将 JRE 复制到一个临时位置并启动我编写的Java安装程序 GUI,它收集所有配置信息并在后台启动 ANT。

If you don't have much to configure or what you need to configure doesn't have many options, then you can probably do everything in NSIS directly.如果你没有太多要配置的东西,或者你需要配置的东西没有很多选项,那么你可以直接在 NSIS 中做所有事情。 But, keep in mind that you need to be security conscious too.但是,请记住,您也需要具有安全意识。 How are you creating a new user to run Postgres?你如何创建一个新用户来运行 Postgres? What about directory permissions for you install?您安装的目录权限如何? Who can start and stop the DB, who can read the db files, etc?谁可以启动和停止数据库,谁可以读取数据库文件等?

Take a lesson from most commercial applications that have a database component.从大多数具有数据库组件的商业应用程序中吸取教训。 Usually they require that the database be installed/configured beforehand and provide db scripts for their supported vendors.通常他们要求预先安装/配置数据库并为其支持的供应商提供数据库脚本。 There are a lot of reasons for this.这有很多原因。 Most of these are related to the fact that organization's like to manage their own databases.其中大部分与组织喜欢管理自己的数据库有关。 This is often because they have company restrictions regarding security, maintenance, etc. Also a single database process may be shared between many different programs(if you install two instances of your application do you want to have to two installations of the database?).这通常是因为他们在安全、维护等方面有公司限制。此外,许多不同的程序可能共享一个数据库进程(如果您安装了应用程序的两个实例,您是否需要安装两次数据库?)。

Most folks(especially network admins) wouldn't want an application that ran around installing rogue DMBS's on their machine.大多数人(尤其是网络管理员)不希望在他们的机器上安装流氓 DMBS 的应用程序。

I wouldn't spend a ton of time attempting to install the database during your install process.在您的安装过程中,我不会花费大量时间来尝试安装数据库。 Why re-invent the wheel when the vendor has already created an installation process for you?当供应商已经为您创建了安装过程时,为什么要重新发明轮子? Instead install the database ahead of time and focus your installer instead on running the necessary scripts such that your application can connect to it on the first start up.而是提前安装数据库并将安装程序的重点放在运行必要的脚本上,以便您的应用程序可以在第一次启动时连接到它。 What it does means is having your installer have the necessary drivers, connection information, and credentials to communicate with the database to get it initialized.它的意思是让您的安装程序拥有必要的驱动程序、连接信息和凭据,以便与数据库进行通信以使其初始化。 After the DB is initialized then you can have additional routines that configure your application install such that it points to the database you initialized during the install.初始化数据库后,您可以使用其他例程来配置您的应用程序安装,使其指向您在安装期间初始化的数据库。 Taking this approach will not only be easier, but it will allow your application to do more interesting things like connect to remote databases or skip database initialization all together and connect to an existing database.采用这种方法不仅更容易,而且可以让您的应用程序做更多有趣的事情,例如连接到远程数据库或一起跳过数据库初始化并连接到现有数据库。

Maybe you may try a pure java database, like hsql, or h2, or derby....也许您可以尝试一个纯 java 数据库,如 hsql,或 h2,或 derby....

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

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