java - Eclipse IDE 无法识别 mysql 驱动程序

标签 java mysql eclipse

这非常令人沮丧,尤其是因为据我所知,我已经浏览了整个论坛并遵循了所有指示。

我正在尝试创建到本地 mysql 数据库的连接。首先我从http://dev.mysql.com/downloads/connector/j下载了mysql驱动。使用独立于平台的 TAR(我在 ubuntu 上)。然后我将生成的 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 cannot be found”

我确定这对我来说完全是愚蠢的,但是关于我遗漏了什么的任何指示?

这是堆栈跟踪:

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)

最佳答案

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

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

“Bundle-ClassPath: lib/mysql-connector-java-5.1.30-bin.jar, .”

末尾的 (.) 也很重要,让我困扰了几分钟。

关于java - Eclipse IDE 无法识别 mysql 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22903589/

相关文章:

java - 如何在eclipse中设置java_opts

Java Swing Clickable JFree Charts,单击每个条形图或饼图即可打开新框架或新图表

java - Box java sdk - 为 BoxItem 生成 Box URL

mysql - 将重复行与附加信息连接起来 (MySQL)

php - 我的 PHP 代码不使用数组推送然后 json_encode 显示来自 MySQL 的数据

java - 将 int 转换为 arr。线程 "main"java.util.InputMismatchException 中不断出现错误异常

java - 测试 applicationContext : org. springframework.core.annotation.AnnotationUtils.getAnnotationAttributes

Java - 锁定文件以进行独占访问

php - 评级查询结构和流程经济性

Android avd 离屏