部署程序集和CLASSNOTFOUND Java / MySQL问题

问题描述 投票:0回答:1

我一直在这里看了几个小时,尝试了一些不同的解决方案,但我还没有进一步。我的环境变量中包含了我的.jar文件。我确保Eclipse在我的构建路径中有mysql-connector ... jar。

我找不到部署程序集设置,这让我相信有些东西我不知道存在我做错了。或者也许他们从光子中取出了这个特征?我觉得我已经击中了adamantium墙。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class main {


public static void main(String[] args) {
    Connection conn = null;
try {
//Server connection details
Class.forName("com.mysql.jdbc.Driver");
String host = "jdbc:mysql://localhost/3306/db";
String userName = "admin";
String password = "admin";

 //Get the connection going.

conn = DriverManager.getConnection(host, userName, password);
}
catch (SQLException err) {
System.out.println(err.getMessage());
}
}}

这是我得到的:

线程“main”中的异常java.lang.Error:未解决的编译问题:

未处理的异常类型ClassNotFoundException

在main.main(main.java:12)

java mysql eclipse connection classnotfoundexception
1个回答
0
投票

很明显Eclipse无法在项目类路径中找到MySQL Jdbc驱动程序。按照Eclipse中提到的步骤进行操作 -

  1. 窗口 - >首选项 - >连接 - >驱动程序定义。

检查MySQL JDBC驱动程序是否在驱动程序定义列表中可见/存在,如下图所示。 enter image description here

如果看不到MySQL JDBC DRiver,请单击“添加”,将出现一个窗口,如下图所示。

enter image description here

从Filter中选择MySQL,选择您的MySQL版本。如果已经在Eclipse Project中注册了MySQL驱动程序,那么您应该可以选择它并单击“编辑”以检查它是否指向文件系统中的正确JAR文件。

如果没有,您可以单击添加,从列表中选择MySQL JDBC,切换到JAR选项卡,通过在文件系统中找到它来添加JAR文件,单击确定,您应该很高兴。

如果正确添加MySQL JDBC Connector,我肯定会发生ClassNotFoundException消失。

但是,您将遇到一组新错误,因为您只捕获main.java中的SQLException。

使源代码快速编译的快速修复方法是使用Exception作为catch参数,如下所示 -

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    public class main {


    public static void main(String[] args) {
        Connection conn = null;
    try {
     //Server connection details
     Class.forName("com.mysql.jdbc.Driver");
     String host = "jdbc:mysql://localhost/3306/db";
     String userName = "admin";
     String password = "admin";

     //Get the connection going.

     conn = DriverManager.getConnection(host, userName, password);
    }
    catch (Exception err) { // Catch All otherwise the compiler will flag an uncaught Connection exception
      System.out.println(err.getMessage());
    }
    }}

我希望这可以解决你的编译+构建问题。 Afaik,Eclipse忽略了Windows系统ENV变量。

© www.soinside.com 2019 - 2024. All rights reserved.