简体   繁体   English

在NetBeans外部访问Java DB

[英]Accessing Java DB outside NetBeans

I developed an application with a java db database .I cannot access the database records when I close the netbeans IDE with the message "Error connecting to server Localhost on port..." My connection code to the Database is : 我用Java db数据库开发了一个应用程序。当我关闭NetBeans IDE并显示消息“在端口上连接到服务器Localhost时出错...”时,无法访问数据库记录,我与数据库的连接代码为:

String host="jdbc:derby://localhost:1527/Employee;create=true";
String user="admin";
String pass="admin";
con=DriverManager.getConnection(host,user,pass);

How do I fix the problem? 我该如何解决该问题?

Netbeans automatically starts the Derby server; Netbeans自动启动Derby服务器。 you can see that in the "Services" tab (Ctrl-5). 您可以在“服务”标签(Ctrl-5)中看到它。

You'll have to start the database server by hand if you don't use Netbeans; 如果您不使用Netbeans,则必须手动启动数据库服务器。 see the doc . 请参阅文档

the simplest way of dealing with these problems is creating batch files.. first of all build your java database program.. the tricky part is to start the server. 解决这些问题的最简单方法是创建批处理文件。首先构建您的Java数据库程序。棘手的部分是启动服务器。 the jderby is a server so it needs to be started.. that's why you start the server in netbeans. jderby是一台服务器,因此需要启动..这就是为什么要在netbeans中启动服务器。 so download db derby files from " http://db.apache.org/derby/releases/release-10.8.2.2.html ". 因此,请从“ http://db.apache.org/derby/releases/release-10.8.2.2.html ”下载db derby文件。 after you download these files, copy your netbeans project in those db jderby files.. go and copy your database folders from where they will be saved.. and paste them in the db jderby files.. now open notepad and type @echo start (PATH) start (PATH) the first path take the path of the file named start network server.bat the second path take the path of the jar file of your main project. 下载完这些文件后,将您的netbeans项目复制到这些db jderby文件中。然后转到从要保存它们的数据库文件夹中复制它们。并将它们粘贴到db jderby文件中。现在打开记事本并键入@echo start( PATH)开始(PATH)第一个路径采用名为start network server的文件的路径.bat第二个路径采用主项目的jar文件的路径。 Now save your the notepad as setup.bat and run the batch file afterward.. and ur program will start the server and running your application at once... NB: you can use a different name from setup, any of your choice but the extension bat must be available 现在将记事本另存为setup.bat,然后运行批处理文件..然后ur程序将启动服务器并立即运行您的应用程序...注意:您可以使用setup以外的其他名称,但可以选择其他名称扩展棒必须可用

Presumably your Derby database is hosted in NetBeans? 大概您的Derby数据库托管在NetBeans中? You'll need to create a standalone database. 您需要创建一个独立的数据库。

You would have to probably start your database before connecting (you are using server mode). 在连接之前,您可能必须先启动数据库(使用服务器模式)。 Have a look into Vogella tutorial on Derby db connection from java application: http://www.vogella.de/articles/ApacheDerby/article.html 从Java应用程序了解有关Derby db连接的Vogella教程: http ://www.vogella.de/articles/ApacheDerby/article.html

You can use JavaDB (aka Derby) either by connecting to a JavaDB Network Server or by using it as an embedded DB when your application opens the DB files itself. 您可以通过连接到JavaDB网络服务器来使用JavaDB(也称为Derby),也可以在应用程序打开数据库文件本身时将其用作嵌入式DB

Currently, your application is connecting to a Network Server started by NetBeans, as your URL is telling to connect to port 1527 on localhost, ie your system. 当前,您的应用程序正在连接到由NetBeans启动的网络服务器,因为您的URL告诉您要连接到本地主机(即系统)上的端口1527。

What you need to do is tell your application to use JavaDB as an embedded database, ie it should manage the database itself instead of getting Netbeans to do it instead. 您需要做的就是告诉您的应用程序将JavaDB用作嵌入式数据库,即它应该管理数据库本身而不是让Netbeans来做。 You can do this just be changing the URL to something like: 您只需将URL更改为以下内容即可:

jdbc:derby:Employee;create=true

You may need to tweak that URL depending on where the database files are stored relative to your application's working directory. 您可能需要调整URL,具体取决于数据库文件相对于应用程序的工作目录的存储位置。

Only one application can have the DB open at one time. 一次只能打开一个应用程序。 So when you're doing this NetBeans won't be able to open the database, and if NetBeans has the database open your application won't be able to open it. 因此,当您执行此操作时,NetBeans将无法打开数据库,如果NetBeans已打开数据库,则您的应用程序将无法打开它。 So you may find you want to reconfigure NetBeans as a DB client. 因此,您可能会发现您想将NetBeans重新配置为数据库客户端。

I guess NetBeans has an embedded DB instance. 我猜NetBeans有一个嵌入式数据库实例。

Try to use 尝试使用

     jdbc:derby:/MyFolder/MyDatabase/Employee;create=true  

or 要么

     jdbc:derby:C:\MyFolder\MyDatabase\Employee;create=true  

if you do not need to access the DB from multiple applications. 如果您不需要从多个应用程序访问数据库。

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

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