Eclipse IDE无法识别mysql驱动程序

时间:2014-04-07 04:25:11

标签: java mysql eclipse

这一直非常令人沮丧,特别是因为我已经遍布论坛并遵循所有指示,据我所知。

我正在尝试创建与本地mysql数据库的连接。首先,我使用独立于平台的TAR(我在ubuntu上)从http://dev.mysql.com/downloads/connector/j下载了mysql驱动程序。然后我将生成的JAR放入我为项目调用/ lib创建的新文件夹中,然后右键单击并选择"添加到构建路径"。它现在显示为参考库。

对于代码,我正在做你将要做的典型事情:

try {
    Class.forName("com.mysql.jdbc.Driver");
    String db_url = "jdbc:mysql://localhost/project";
    connect = DriverManager.getConnection(db_url, "user", "password");
    statement = connect.createStatement();
    resultSet = statement.executeQuery("select * from database.table");
    while (resultSet.next()) {
        int id = resultSet.getInt("ID");
        String name = resultSet.getString("Name");
        System.out.println("(ID: " + id + ") (Name: " + name + ")");
    }
} catch (ClassNotFoundException | SQLException e) {
    e.printStackTrace();
}

但无论出于何种原因,我仍然得到" java.lang.ClassNotFoundException:com.mysql.jdbc.Driver找不到"

我确定这对我来说是完全愚蠢的事情,但是对于我所缺少的东西有任何指示?

这是堆栈跟踪:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver cannot be found by com.personal.recipes_1.0.0.qualifier
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.personal.recipes.test.TestUtils.execute(TestUtils.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:56)
at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:243)
at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:224)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:132)
at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:167)
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:499)
at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508)
at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:213)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.executeItem(HandledContributionItem.java:850)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.handleWidgetSelection(HandledContributionItem.java:743)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.access$7(HandledContributionItem.java:727)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem$4.handleEvent(HandledContributionItem.java:662)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1392)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3742)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3363)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1113)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:997)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:138)
at org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java:162)
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:354)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:181)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:636)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591)
at org.eclipse.equinox.launcher.Main.run(Main.java:1450)
at org.eclipse.equinox.launcher.Main.main(Main.java:1426)

2 个答案:

答案 0 :(得分:2)

所以我以前可能没有充分解释我的问题。这个问题与让我的Eclipse RCP应用程序看到驱动程序有关。在与朋友交谈后,他提醒我需要将mysql jar直接添加到.MF文件中。一旦我知道我在各种论坛上发现了大量的帖子来回答我的问题。

我添加了以下行,并且我能够毫无问题地建立与数据库的连接:

" Bundle-ClassPath:lib / mysql-connector-java-5.1.30-bin.jar,。"

最后的(。)也非常重要,让我在几分钟内陷入困境。

答案 1 :(得分:0)

我刚刚将您的代码快速复制/粘贴到新鲜项目中,在lib中安装了驱动程序...就像一个魅力。那你在代码片段之前做了些什么吗? Fresh Project