简体   繁体   English

从可执行jar文件访问derby数据库

[英]access derby database from an executable jar file

I did a java project with netbeans using derby. 我用derby用netbeans做了一个Java项目。 The project runs fine in the IDE, but when i build it and run from the .jar file without starting server from the netbeans, it displays: 该项目在IDE中运行良好,但是当我构建它并从.jar文件运行而没有从netbeans启动服务器时,它将显示:

"java.net.ConnectException : Error connecting to server localhost on port 1527 
with message Connection refused: connect."

How can i connect to the database on my client`s machine without netbeans on it? 如何在没有netbean的情况下连接到客户端计算机上的数据库?

You need to know the IP of your DB server, as localhost or 127.0.0.1 refers to the DB installed on local machine, and if you run the app on client's machine it is pretty sure, that this DB is not there locally. 您需要知道数据库服务器的IP,因为localhost127.0.0.1指的是本地计算机上安装的数据库,并且如果您在客户端计算机上运行该应用程序,则可以确定该数据库不在本地。 You need to specify the exact IP address, if multiple clients should connect to the same DB. 如果多个客户端应连接到同一数据库,则需要指定确切的IP地址。

If you just need a DB per app, then you can use Embedded Derby, that starts and stops with your application, so you don't really need external server (very good solution in case when this is the only app that use this DB): 如果每个应用程序仅需要一个数据库,则可以使用随应用程序启动和停止的Embedded Derby,因此您实际上并不需要外部服务器(如果这是唯一使用此数据库的唯一应用程序,这是一个很好的解决方案) :

public String driver = "org.apache.derby.jdbc.EmbeddedDriver";
...
Class.forName(driver).newInstance();

Get an Embedded Connection 获取嵌入式连接

public String protocol = "jdbc:derby:";
...
conn = DriverManager.getConnection(protocol + "derbyDB;create=true", props);

That embedded connection URL, fully constructed, looks like this: 完全构建的嵌入式连接URL如下所示:

jdbc:derby:derbyDB;create=true

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

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