可运行 jar 中的 SQL?
Posted
技术标签:
【中文标题】可运行 jar 中的 SQL?【英文标题】:SQL in runnable jar? 【发布时间】:2017-06-01 17:35:56 【问题描述】:我知道这可能很简单,但我想不通......
我正在尝试将我的一个程序转换为可运行的 jar,它使用 JDBC 执行一些 SQL。当我导出项目时,我选择“将所有必需的库提取到可运行的 jar 中”,我的构建路径上有 JDBC。
这是我要挂断的代码部分...
String serverURL = ****
String user = ****
String pass = ****
try
conn = DriverManager.getConnection(serverURL, user, pass);
catch (SQLException e)
//print stacktrack to a dialogbox
ResultSet rs = null;
try
rs = conn.createStatement().executeQuery("SELECT * FROM myDB");
catch (SQLException e)
//print stacktrack to a dialogbox
所以程序在 Eclipse 中运行良好,但是当我将它作为可运行的 jar 运行时,执行似乎在 executeQuery 行停止。我已经完成了一些 JDialogs 来查看问题可能出在哪里,看起来好像 conn 已设置(或者至少它不为空)。由于 conn 不为空,我认为这意味着 JDBC 工作正常。奇怪的是,据我所知,没有例外,程序只是停在 executeQuery 行。任何想法为什么会这样?
编辑:问题发生前的堆栈跟踪
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:64)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)
sun.awt.windows.WToolkit.eventLoop(Native Method)
sun.awt.windows.WToolkit.run(WToolkit.java:306)
java.lang.Thread.run(Thread.java:745)
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Object.java:502)
java.lang.ref.Reference.tryHandlePending(Reference.java:191)
java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Object.java:502)
sun.awt.AWTAutoShutdown.run(AWTAutoShutdown.java:295)
java.lang.Thread.run(Thread.java:745)
java.lang.Thread.dumpThreads(Native Method)
java.lang.Thread.getAllStackTraces(Thread.java:1607)
HandMapper.loadRecorded(HandMapper.java:161)
HandMapper.load(HandMapper.java:88)
HandMapper.main(HandMapper.java:255)
sun.awt.windows.WComponentPeer._dispose(Native Method)
sun.awt.windows.WComponentPeer.disposeImpl(WComponentPeer.java:638)
sun.awt.windows.WWindowPeer.disposeImpl(WWindowPeer.java:117)
sun.awt.windows.WObjectPeer.dispose(WObjectPeer.java:88)
java.awt.Component.removeNotify(Component.java:7042)
java.awt.Container.removeNotify(Container.java:2820)
java.awt.Window.removeNotify(Window.java:782)
java.awt.Frame.removeNotify(Frame.java:1041)
java.awt.Window$1DisposeAction.run(Window.java:1190)
java.awt.Window.doDispose(Window.java:1206)
java.awt.Window.dispose(Window.java:1147)
javax.swing.SwingUtilities$SharedOwnerFrame.dispose(SwingUtilities.java:1814)
javax.swing.SwingUtilities$SharedOwnerFrame.windowClosed(SwingUtilities.java:1792)
java.awt.AWTEventMulticaster.windowClosed(AWTEventMulticaster.java:359)
java.awt.Window.processWindowEvent(Window.java:2057)
javax.swing.JDialog.processWindowEvent(JDialog.java:683)
java.awt.Window.processEvent(Window.java:2013)
java.awt.Component.dispatchEventImpl(Component.java:4889)
java.awt.Container.dispatchEventImpl(Container.java:2294)
java.awt.Window.dispatchEventImpl(Window.java:2746)
java.awt.Component.dispatchEvent(Component.java:4711)
java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
java.awt.EventQueue.access$500(EventQueue.java:97)
java.awt.EventQueue$3.run(EventQueue.java:709)
java.awt.EventQueue$3.run(EventQueue.java:703)
java.security.AccessController.doPrivileged(Native Method)
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
java.awt.EventQueue$4.run(EventQueue.java:731)
java.awt.EventQueue$4.run(EventQueue.java:729)
java.security.AccessController.doPrivileged(Native Method)
java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
sun.java2d.Disposer.run(Disposer.java:148)
java.lang.Thread.run(Thread.java:745)
【问题讨论】:
打印出所有堆栈跟踪。总是。有重要信息。 能否添加程序的stacktrace? @yorodm 我在问题发生之前添加了完整的堆栈跟踪。不确定这是否是您想要的? 您的连接似乎出现了问题,您是否在与 IDE 相同的机器上运行jar
文件?你检查你的防火墙设置了吗?
jar 是本地的,是的,我在同一台机器上运行它们(相同的线墙设置)。我在连接上做了一个 isValid(),它返回 true。
【参考方案1】:
我推荐 Maven 来打包你的依赖和你的应用程序
一个不错的网站访问https://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/
一个例子:
<project>
[...]
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
[...]
</plugins>
[...]
</build>
</project>
【讨论】:
以上是关于可运行 jar 中的 SQL?的主要内容,如果未能解决你的问题,请参考以下文章