Cannot connect MySQL in Eclipse RCP IDE

一曲冷凌霜 提交于 2019-12-23 03:56:08

问题


All,

I am getting the com.mysql.jdbc.Driver exception when trying to load the Driver class using Class.forName("com.mysql.jdbc.Driver"). I have written a custom Eclipse plugin and wanted to access the database in my custom Eclipse plugin.

I have tried following 2 options, both obviously failed:
1 > Adding the mysql connection JAR file to the Eclipse plugin build path
2 > Adding the connector JAR file as library and adding to the plugin's runtime classpath

Can anyone please help me out on this?

Here is the log:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
   at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:506)
   at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:422)
   at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:410)
   at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
   at java.lang.ClassLoader.loadClass(Unknown Source)
   at java.lang.Class.forName0(Native Method)
   at java.lang.Class.forName(Unknown Source)
   at draft1.set1.utility.DatabaseUpdatesUtility.commitPerspectiveToDB(DatabaseUpdatesUtility.java:48)
   at draft1.set1.display.DomainEditor.doSaveAs(DomainEditor.java:57)
   at org.eclipse.ui.internal.SaveAsAction.run(SaveAsAction.java:64)
   at org.eclipse.jface.action.Action.runWithEvent(Action.java:498)
   at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
   at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
   at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
   at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
   at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
   at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066)
   at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
   at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
   at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
   at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
   at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
   at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
   at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
   at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
   at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
   at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
   at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
   at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
   at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
   at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:619)
   at org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)
   at org.eclipse.equinox.launcher.Main.run(Main.java:1407)
   at org.eclipse.equinox.launcher.Main.main(Main.java:1383)
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mysql
   at java.sql.DriverManager.getConnection(Unknown Source)
   at java.sql.DriverManager.getConnection(Unknown Source)
   at draft1.set1.utility.DatabaseUpdatesUtility.commitPerspectiveToDB(DatabaseUpdatesUtility.java:53)
   at draft1.set1.display.DomainEditor.doSaveAs(DomainEditor.java:57)
   at org.eclipse.ui.internal.SaveAsAction.run(SaveAsAction.java:64)
   at org.eclipse.jface.action.Action.runWithEvent(Action.java:498)
   at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
   at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
   at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
   at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
   at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
   at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066)
   at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
   at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
   at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
   at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
   at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
   at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
   at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
   at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
   at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
   at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
   at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
   at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
   at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
   at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:619)
   at org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)
   at org.eclipse.equinox.launcher.Main.run(Main.java:1407)
   at org.eclipse.equinox.launcher.Main.main(Main.java:1383)

回答1:


I recommend to create your own OSGi-Bundle for the MySQL Driver, see http://wiki.eclipse.org/Create_and_Export_MySQL_JDBC_driver_bundle for a how-to. Afterwards you have to set a dependency in your plugin.xml of your bundle to the created mysql-bundle.

In addition: Please use in an OSGi environment not the DriverManager but an implementation of DataSource.



来源:https://stackoverflow.com/questions/4868546/cannot-connect-mysql-in-eclipse-rcp-ide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!