简体   繁体   English

如何运行Netbeans IDE之外的数据库程序?

[英]How to run database program outside the Netbeans IDE?

I am really new to database and this is my first program in database using java Netbeans 7.1 --- It is summer on our country now and I am a student with the course IT Our next subject on programming is about database and since there's no class I spend my time learning database for preparation for the next school semester and I refer to this site as my guide for the first database program I currently making now.我对数据库真的很陌生,这是我在数据库中使用的第一个程序 java Netbeans 7.1 --- 现在是我们国家的夏天,我是 IT 课程的学生 我们的下一个编程主题是关于数据库的,因为没有 class我花时间学习数据库,为下学期做准备,我将此站点作为我目前正在制作的第一个数据库程序的指南。

http://www.homeandlearn.co.uk/java/databases_and_java_forms.html http://www.homeandlearn.co.uk/java/databases_and_java_forms.html

I did everything in the tutorial and I actually done doing the program.我做了教程中的所有事情,实际上我也完成了程序。

The final thing I did is I clean and build the program since I want the program to run with out opening the.netbeans again I downloaded the JRE and make my database_form.jar as a jar executable.我做的最后一件事是清理并构建程序,因为我希望程序在不打开 .netbeans 的情况下运行 我下载了 JRE 并将我的 database_form.jar 作为 jar 可执行文件。 "database_form" the name of my Netbeans Project. “database_form”是我的 Netbeans 项目的名称。 I do that by making javaw in JRE.7 as my dafault when opening any jar files.我通过在打开任何 jar 文件时将 JRE.7 中的 javaw 作为我的默认设置来做到这一点。

Anyway, this is how i run the program.无论如何,这就是我运行程序的方式。

  1. Running Program in Netbeans IDE运行程序在 Netbeans IDE

Using Netbeans, before my program works I first need to "Start Server" on JavaDB.使用 Netbeans,在我的程序运行之前,我首先需要在 JavaDB 上“启动服务器”。 because if I didn't do that an Exception occurred "Err. connecting to server localhost 1527 and so on"因为如果我不这样做,就会发生异常“错误。连接到服务器 localhost 1527 等等”

  1. Running Program using jar executable alone.单独使用 jar 可执行文件运行程序。

The Problem is there's an Exception and Err in connecting still occurred.问题是连接中仍然存在异常和错误。

What I want to achieve?我想达到什么目的?

I want the program to run without opening the Netbeans IDE and going to Java DB to click the "Start Server", I dont want to do that anymore.我希望程序在不打开 Netbeans IDE 并转到 Java DB 以单击“启动服务器”的情况下运行,我不想再这样做了。 Or my second option is to start the server using command prompt so that I just have to make a bat file so that whenever I open my program database_form.jar I just place the bat file on my desktop and run it.或者我的第二个选择是使用命令提示符启动服务器,这样我只需要制作一个 bat 文件,这样每当我打开我的程序 database_form.jar 时,我只需将 bat 文件放在我的桌面上并运行它。

Second Problem, Actually, I already try my second option by using command prompt to start the server but I forgot how I did it.第二个问题,实际上,我已经通过使用命令提示符启动服务器来尝试我的第二个选项,但我忘记了我是如何做到的。 I just found it on some website the only thing I remember is the Exception says "Failed to lunch the server because the database Employees is missing. Employees is the name of my created database.我刚刚在某个网站上找到它,我唯一记得的是异常说“由于数据库 Employees 丢失,无法为服务器提供午餐。Employees 是我创建的数据库的名称。

The OS I am using is Windows 7.我使用的操作系统是 Windows 7。

Thank you for all the reply and sorry for the long text I just want to be specific, :D感谢您的所有回复,对于我只想具体说明的长文本感到抱歉,:D

Right, from your description there seems to be a couple of things you are confusing.是的,从您的描述来看,您似乎对一些事情感到困惑。

First, databases are typically run as servers with multiple clients connecting to them thus allowing they contain to be shared.首先,数据库通常作为服务器运行,多个客户端连接到它们,从而允许共享它们的内容。 When you start Java DB you are starting the Java DB database server.当您启动 Java DB 时,您正在启动 Java DB 数据库服务器。

That said, lightweight databases, such as Java DB can be run in an embedded mode as explained here .也就是说,轻量级数据库,例如 Java DB 可以在嵌入式模式下运行,如此所述。 Remember that the directory you point to with the derby.system.home property will need to contain the database files, if not you'll need to create that programatically too.请记住,您使用derby.system.home属性指向的目录将需要包含数据库文件,否则您也需要以编程方式创建它。

Second, there's various ways to run a Java application outside of an IDE, but jars themselves are not executable in the same way an exe file is in Windows.其次,有多种方法可以在 IDE 之外运行 Java 应用程序,但 jars 本身不能像 Windows 中的 exe 文件那样执行。

The simplest way is to call the java executable passing the necessary classpath and the name of the class containing the main method.最简单的方法是调用 java 可执行文件,传递必要的类路径和包含 main 方法的 class 的名称。 For example if I had a class called com.example.Application that had been compiled to a directory C:\dev\example\classes the following command line would run the application:例如,如果我有一个名为com.example.Application的 class 已编译到目录C:\dev\example\classes ,则以下命令行将运行该应用程序:

java -cp C:\dev\example\classes com.example.Application

If there were dependencies on external libraries, as there will be in your case on the Derby JDBC driver, then those would also need including in the classpath resulting in something like:如果存在对外部库的依赖性,就像在 Derby JDBC 驱动程序上的情况一样,那么这些库也需要包含在类路径中,从而导致类似以下内容:

java -cp C:\dev\example\classes;C:\dev\lib\derby.jar com.example.Application

There's a full set of document on the Java launcher here . 这里有关于 Java 启动器的全套文档。

Now, back to the jar. Like I said, jars are not executable but there is something that's know as an 'executable jar'.现在,回到 jar。就像我说的,jars 不是可执行的,但有一些东西被称为“可执行的 jar”。 This is the same as any jar except there are some special additions to the manifest to specify the application entry point or main-class and the class-path as described here .这与任何 jar 相同,除了清单中有一些特殊添加以指定应用程序入口点或主类和类路径,如此所述。

Once the main-class and class-path are specified in the jar's manifest, the following command line would run the application:在 jar 的清单中指定主类和类路径后,以下命令行将运行该应用程序:

java -jar C:\dev\example.jar

You can even associate the jar extension with the java exe and double clicking the jar will cause the application to launch (though on a dev machine it's probably more useful that the jar extension be associated with WinZip or simlar in order to open that jar).您甚至可以将 jar 扩展名与 java exe 相关联,双击 jar 将导致应用程序启动(尽管在开发机器上,将 jar 扩展名与 WinZip 或类似的文件相关联以打开该 jar 可能更有用)。

The default database in Netbeans is Derby/JavaDB. Netbeans 中默认的数据库是 Derby/JavaDB。 So you need to:所以你需要:

  • add the jar of javadb/derby in our classpath (it maybe already present, as it is bundled with java when you install it in Ubuntu)在我们的类路径中添加 javadb/derby 的 jar(它可能已经存在,因为当你在 Ubuntu 中安装它时它与 java 捆绑在一起)
  • setup a path with the jdbc URI to save the database data使用 jdbc URI 设置路径以保存数据库数据

I personally recommend the usage of hsqldb or H2 for this: they support in-memory database, very useful for stand alone project with no persistence data or for tests.我个人建议为此使用hsqldbH2 :它们支持内存数据库,对于没有持久性数据的独立项目或测试非常有用。

If you use window, add ODBC Data Sources from Administrative Tools to your Java Derby driver and run jar.如果您使用 window,请将来自管理工具的 ODBC 数据源添加到您的 Java Derby 驱动程序并运行 jar。

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

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