一些背景;我非常习惯 PHP,我总是使用类似“connect.ini”的东西,在其中设置所有变量,让 PHP 脚本连接到数据库(即数据库 URL、密码、名称等);现在,我正在上有关 Java iee 的相同课程,老师希望我们使用可运行的 jar 和 connect.bat 文件,并使用主函数中的“String[] args”链接它们。也就是说,您编写代码并让方法接收变量以连接到数据库,这些变量在从命令参数传递的 main 方法中设置,main 调用需要变量的方法,然后程序连接到数据库。需要 bat 文件,因为您将设置加上命令加上参数(以环境变量的形式);希望这是清楚的。
但我想知道这是否是一个好的解决方案,以及是否是一个可以接受的解决方案。我的问题:
- 这个系统可以在 Linux 上运行吗?我不相信是这样。也许是一个shell脚本,所以我们正在失去代码的可移植性?
- 为什么不使用简单的 ini 文件来放入资源中?
- 您如何评价要求使用此方法分配变量以连接到数据库的老师?
- 如果我认为在使用外部有用参数连接到数据库时他让我陷入了错误的方式,我是否错了?
Some background; I'm quite used to PHP, and I always used something like 'connect.ini' where set all my variables to let the PHP script to the database (i.e. the DB URL, the password, the name, and so on); now, I'm taking same classes about Java iee, and the teacher wants us to use runnable jars and connect.bat files, linking them using the 'String[] args' in the main function. That is, you write your code and let the method to receive the variables to connect to the database, those variables are set in the main method where they were passed from the command arguments, the main call the method where the variables are needed and the program connects to the DB. the bat file is needed cause you put on it your settings plus the command plus the arguments (in form of environment variables); hope it was clear.
But I'm wondering if this is a good solution, and also if it's an acceptable solution. My questions:
- Does this system work on Linux? I don't believe so. Perhaps a shell script, so we are losing the portability of the code?
- Why not to use a simply ini file to be put in the resources?
- How would you rate a teacher who are asking to use this method to assign the variables to connect to the database?
- Am I wrong if I think he's putting me in the wrong way when connecting to a database using external useful parameters?
发布评论
评论(2)
有多种方法可以向主方法提供参数。应用程序的 EG。通过启动。 Java Web Start,您可以在 JNLP 启动文件。成为 JWS 应用程序。意味着该应用程序:
..所有这些可能远远超出了你现在同学的能力。
您相信 .bat 文件不是最佳文件是正确的,因为它是 Windows 特定的。 OTOH,这是启动应用程序的一种有效方式。当使用 Windows 时,也许老师希望在本次练习之后向您介绍更多 x-plat 方法。
我的建议是暂时保持耐心,但如果老师介绍了更多使用 .bat 文件启动 Java 应用程序的练习,请询问他们有关启动 Java 代码的其他“更多跨平台”方法。
简单介绍一下 .ini 文件。 Java 提供了对
属性
文件,其格式与 .ini 类似,但我不确定Properties
类是否可以解析 INI 文件。There are a number of ways to supply parameters to the main method. E.G. for an app. launched via. Java Web Start, you would define those arguments in the JNLP launch file. To be a JWS app. means that the app.:
..all of which is probably significantly beyond the skills of your classmates right now.
You are right in believing that a .bat file is not optimal, since it is Windows specific. OTOH, it is one valid way to launch an app. when using Windows, and perhaps the teacher is looking to introduce you to more x-plat methods after this exercise.
My advice would be to be patient for the moment, but if the teacher introduces more exercises using .bat files to launch Java apps., ask them about other 'more cross-platform' ways to launch Java code.
And just a quick aside on .ini files. Java provides inbuilt support for
Properties
files, which have a similar format to .ini, but I'm not sure if theProperties
class could parse an INI file..sh
文件。.properties
文件,因此,命令行参数。正如 Andrew Thompson 提到的,使用
.properties
文件。.sh
file for Linux/Unix shells..properties
files for now, hence, command line parameters.Use a
.properties
file, as Andrew Thompson has mentioned.