As As
As As

Reputation: 2107

Executable jar and database connection error

I made a little project with java and a database. I tryed it on another computer and the executable jar works perfectly except for the methods that need to read or write in a database. If i start it from my IDE (NetBeans) it works fine if i connect to the database. How can i do? The exception that i get is this:

java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Errore
di connessione al server localhost sulla porta 1527 con messaggio Connection ref
used: connect.
        at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unkn
own Source)
        at org.apache.derby.client.am.SqlException.getSQLException(Unknown Sourc
e)
        at org.apache.derby.jdbc.ClientDriver.connect(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at newsletter.Utilities.salva(Utilities.java:80)
        at newsletter.NsLs.salva(NsLs.java:40)
        at newsletter.NSFrame$3.actionPerformed(NSFrame.java:80)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$200(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: org.apache.derby.client.am.DisconnectException: java.net.ConnectExcep
tion : Errore di connessione al server localhost sulla porta 1527 con messaggio
Connection refused: connect.
        at org.apache.derby.client.net.NetAgent.<init>(Unknown Source)
        at org.apache.derby.client.net.NetConnection.newAgent_(Unknown Source)
        at org.apache.derby.client.am.Connection.<init>(Unknown Source)
        at org.apache.derby.client.net.NetConnection.<init>(Unknown Source)
        at org.apache.derby.client.net.NetConnection40.<init>(Unknown Source)
        at org.apache.derby.client.net.ClientJDBCObjectFactoryImpl40.newNetConne
ction(Unknown Source)
        ... 42 more
Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.DualStackPlainSocketImpl.connect0(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at java.net.Socket.<init>(Unknown Source)
        at java.net.Socket.<init>(Unknown Source)
        at javax.net.DefaultSocketFactory.createSocket(Unknown Source)
        at org.apache.derby.client.net.OpenSocketAction.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        ... 48 more

Upvotes: 0

Views: 1566

Answers (1)

lreeder
lreeder

Reputation: 12226

Your jar is trying to to connect to the database on the computer that it is currently running on (localhost). When you move the jar to another computer, Derby is not running on the new computer (now localhost) and you get the connection error. Change the Derby hostname in your configuration or code to the name of a computer that is running Derby.

The Derby docs have a tutorial on using Derby in server mode here: http://db.apache.org/derby/papers/DerbyTut/ns_intro.html#ns_lookat_code. It describes setting a protocol URL for the database:

protocol = "jdbc:derby://localhost:1527/";

"localhost" refers the current computer that the software is running on. Wherever you are defining this protocl URL, set localhost to the domain name of the computer the server is running on.

You'll also need to make sure that you have network connectivity between your client/jar computer and your database (Derby) server. You probably do if they are both on the same network.

Upvotes: 2

Related Questions