求助大牛,关于java连接mysql驱动Class.forname(Driver)报错问题

wealom 2017-05-03 11:52:32
是这样的,我在eclipse内使用这段代码连接数据库是完全没问题,可以拿到我想要的东西,但导出jar包使用exe4j打成exe文件之后
Class.forName(CGConstants.DRIVER);直接报错,请问是什么问题,代码在最下面

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at auto.GenEntity.<init>(GenEntity.java:71)
at auto.CodeGenerator$4.actionPerformed(CodeGenerator.java:341)
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 Source)
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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
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)


// 数据库驱动
public static final String DRIVER = "com.mysql.jdbc.Driver";

// 创建连接
Connection con = null;
// 查要生成实体类的表
String sql = "select * from " + CGConstants.tableName;
PreparedStatement pStemt = null;
try {
try {
Class.forName(CGConstants.DRIVER);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
try {
con = DriverManager.getConnection(CGConstants.URL, CGConstants.NAME, CGConstants.PASS);
} catch (Exception e) {
throw new SQLException("数据库连接失败!");
}
...全文
918 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wealom 2017-05-04
  • 打赏
  • 举报
回复 1
总算找到问题了,特地过来封贴顺便分享给大家... 首先说一下这个报错是因为我用exe4j打成.exe文件之后运行的错误,直接使用jar文件运行没有错误 所以问题在于exe4j封装时jar包的问题 如果你引入了外部的jar包比如mysql驱动包, 一定!!! 一定!!! 一定!!!要在选择你的jar包和选择main函数的时候把jar包也加进去 否则有可能会报classNotFound错误
小伙真帅 2017-05-03
  • 打赏
  • 举报
回复
打包时配置好lib目录位置. 运行时确保所需要的jar包都在正确的位置. 你这个错误就是找不到jar包.
小伙真帅 2017-05-03
  • 打赏
  • 举报
回复
配置一下MAINFEST清单,可能你的配置有问题.
wealom 2017-05-03
  • 打赏
  • 举报
回复
我已经把jdbc驱动包放入jar里了,而且这个是java项目,不是web项目,没有lib文件夹

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧