求助大牛,关于java连接mysql驱动Class.forname(Driver)报错问题
是这样的,我在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("数据库连接失败!");
}
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("数据库连接失败!");
}
...全文
相关推荐
浅析使用JDBC操作MySQL需要添加Class.forName("com.mysql.jdbc.Driver")
因此,`Class.forName("com.mysql.jdbc.Driver")`不仅加载了MySQL驱动类,还确保了它的初始化,使得静态初始化块得以执行。
java中Class.forName方法的作用详解
在上述的JDBC示例中,我们看到`Class.forName("com.mysql.jdbc.Driver")`这行代码,其作用是加载并初始化`com.mysql.jdbc.Driver`这个数据库驱动类
JDBC连接MySQL驱动
**注册驱动**:在Java代码中,你需要通过`Class.forName()`方法注册MySQL驱动,如下所示: ```java Class.forName("com.mysql.cj.jdbc.Driver
mysql5.6连接驱动jaf,可处理报错java.sql.SQLException: No suitable driver
例如: ```java Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection( "jdbc
jdbc中class.forname的作用
加载驱动:`Class.forName("com.mysql.jdbc.Driver")` 这行代码的作用是确保`com.mysql.jdbc.Driver` 类被加载并且执行了类的静态初始化块。


Java SE
