可执行jar和数据库连接错误

时间:2013-07-06 14:00:35

标签: java database jar executable

我用java和数据库制作了一个小项目。我在另一台计算机上尝试了它,并且可执行jar工作正常,除了需要在数据库中读取或写入的方法。如果我从我的IDE(NetBeans)启动它,如果我连接到数据库它工作正常。我能怎么做?我得到的例外是:

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

1 个答案:

答案 0 :(得分:2)

您的jar正在尝试连接到当前运行的计算机上的数据库(localhost)。当您将jar移动到另一台计算机时,Derby没有在新计算机(现在是localhost)上运行,并且您收到连接错误。将配置或代码中的Derby主机名更改为运行Derby的计算机的名称。

Derby文档提供了有关在服务器模式下使用Derby的教程:http://db.apache.org/derby/papers/DerbyTut/ns_intro.html#ns_lookat_code。它描述了为数据库设置协议URL:

  

protocol =“jdbc:derby:// localhost:1527 /”;

“localhost”是指运行该软件的当前计算机。无论您在何处定义此protocl URL,请将localhost设置为运行服务器的计算机的域名。

您还需要确保在客户端/ jar计算机和数据库(Derby)服务器之间具有网络连接。如果他们都在同一个网络上,你可能会这样做。