简体   繁体   中英

access derby database from an executable jar file

I did a java project with netbeans using derby. 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:

"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?

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. You need to specify the exact IP address, if multiple clients should connect to the same DB.

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):

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:

jdbc:derby:derbyDB;create=true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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