使用 MySQL DB 完成 Java 桌面应用程序后如何创建安装程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6150146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create installer once finished with Java Desktop Application with MySQL DB?
提问by Saher Ahwal
I have finished writing a Java Desktop application with a mySQL database. I want to make the application run outside netbeans and let it be installed on other computers. I know about building the project and creating the runnable jar file, however this requires me to export the database itself to the other computer I want the application to run on. My question is two parts:
我已经用 mySQL 数据库编写了一个 Java 桌面应用程序。我想让应用程序在 netbeans 之外运行,并让它安装在其他计算机上。我知道如何构建项目并创建可运行的 jar 文件,但这需要我将数据库本身导出到我希望应用程序运行的另一台计算机上。我的问题是两部分:
1)Is there a way I can create a setup file that also installs the database and the application together?
1)有没有办法创建一个安装文件,同时安装数据库和应用程序?
2)Also my database path is hard coded, does that mean I have to change the code every time I install my application for someone, what is the better way to do that?
2)我的数据库路径也是硬编码的,这是否意味着我每次为某人安装应用程序时都必须更改代码,有什么更好的方法来做到这一点?
Thanks
谢谢
回答by Sorrow
- Yes. You can use some setup builder, like InnoSetup, for example. Personally, however, I like giving my customers a zip file, which they extract wherever they like. The executable jar should be able to handle everything by itself (I like it where there is no need to install the software, just unpack and run).
- If it is hardcoded, then yes (but, what do you mean by hardcoded? path to file? ip address?). You should use properties or configuration files for paths and other external things your software depends on. The software should read from those files. Upon startup check for presence of such file(s) - if missing, the user should be shown a window in which the config can be entered.
- 是的。例如,您可以使用一些设置构建器,例如 InnoSetup。然而,就我个人而言,我喜欢给我的客户一个 zip 文件,他们可以在他们喜欢的任何地方提取。可执行jar应该能够自己处理所有事情(我喜欢它不需要安装软件,只需解压并运行)。
- 如果是硬编码,然后是(但,什么你的意思硬编码?路径文件?IP地址?)。您应该将属性或配置文件用于路径和软件所依赖的其他外部事物。软件应该从这些文件中读取。启动时检查是否存在此类文件 - 如果丢失,则应向用户显示一个可以输入配置的窗口。
As for deploying MySQL with your code - consider using a server for that, so that your users are not forced to install MySQL, instead they connect to it over the net. If you need the database only for storing data locally, why not using SQLite or a similar, file-based db engine?
至于使用您的代码部署 MySQL - 考虑为此使用服务器,这样您的用户就不会被迫安装 MySQL,而是通过网络连接到它。如果您只需要数据库用于在本地存储数据,为什么不使用 SQLite 或类似的基于文件的数据库引擎?
The above answers are just suggestions and more-less reflect the way I am thinking. I would be happy to hear from someone with more experience. Nonetheless, I hope the answers help a little :)
以上答案只是建议,多多反映了我的想法。我很高兴收到有经验的人的来信。尽管如此,我希望答案对您有所帮助:)
回答by Aardvocate Akintayo Olusegun
I agree with Sorrow.
我同意悲伤。
If I have to use MySQL, it is normally over the net since I don't want to allow my clients pass through the hazzles of installing MySQL themselves. If however you am stuck with using MySQL locally, investigate MySQL unattended installations + NSIS Installer.
如果我必须使用 MySQL,它通常是通过网络进行的,因为我不想让我的客户通过自己安装 MySQL 的麻烦。但是,如果您坚持在本地使用 MySQL,请调查 MySQL 无人值守安装 + NSIS 安装程序。
If you can use any db you want, I just use javadb/derby. It comes bundled with most Java installations these days and if not all you need is to add a jar file to you application.
如果你可以使用任何你想要的数据库,我只使用 javadb/derby。如今,它与大多数 Java 安装捆绑在一起,如果不是全部,您只需要向应用程序添加一个 jar 文件。
As per 'hardcoding' paths, I really don't understand what you mean. You really don't have 'paths' as it were, I am assuming what you mean is connection string. You don't have to hardcode your connection string, just put some parameters in a properties file and construct your connection string from them.
根据“硬编码”路径,我真的不明白你的意思。你真的没有“路径”,我假设你的意思是连接字符串。您不必对连接字符串进行硬编码,只需将一些参数放在属性文件中并从它们构建您的连接字符串。
回答by Andrew Thompson
1) Is there a way I can create a setup file that also installs the database and the application together?
1)有没有办法创建一个安装文件,同时安装数据库和应用程序?
See my answer to Java based Standalone application.
请参阅我对基于 Java 的独立应用程序的回答。
2) Also my database path is hard coded, does that mean I have to change the code every time I install my application for someone, what is the better way to do that?
2)我的数据库路径也是硬编码的,这是否意味着我每次为某人安装应用程序时都必须更改代码,有什么更好的方法来做到这一点?
Have the DB installer pop a JFileChooser
to ask the user where they want to install the DB. Store that path using the JNLP API PersistenceService. Here is my demo. of the PersistenceService.
让 DB 安装程序弹出一个JFileChooser
询问用户他们想要安装 DB 的位置。使用 JNLP API PersistenceService存储该路径。这是我的演示。的 PersistenceService。