【问题标题】:Java Mysql: ClassNotFoundException -> Could not find driver [duplicate]Java Mysql:ClassNotFoundException->找不到驱动程序[重复]
【发布时间】:2011-09-06 10:14:44
【问题描述】:

可能重复:
ClassNotFoundException com.mysql.jdbc.Driver

我已将mysql-connector-java-5.1.16-bin.jar 包含在我的 Eclipse 库中,此代码基于我在网上找到的教程,因为我之前没有在 Java 中使用过 MySQL。不幸的是,我没有看到我在哪里使用了精确的mysql-connector-java-5.1.16-bin.jar,控制台打印了Could not find driver.,这意味着ClassNotFoundException被抛出。您是否看到了我没有看到的(可能很明显的)问题?

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.sql.Timestamp;

public class MysqlConnector {

    private static MysqlConnector instance = null;
    private static Connection conn = null;

    private static String dbHost = "localhost";
    private static String dbPort = "3306";
    private static String database = "sample";
    private static String dbUser = "root";
    private static String dbPassword = "";

    public MysqlConnector() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://" + dbHost + ":"
                    + dbPort + "/" + database + "?" + "user=" + dbUser + "&"
                    + "password=" + dbPassword);
        } catch (ClassNotFoundException e) {
            System.out.println("Could not find driver."); //TODO LOGGER
        } catch (SQLException e) {
            System.out.println("Could not connect to database."); //TODO LOGGER
        }
    }

    public static MysqlConnector getInstance()
    {
        if(instance == null)
            instance = new MysqlConnector();
        return instance;
    }

    public boolean validateApiKey(String apikey)
    {
        if(conn != null)
        {
            Statement query;
            try {
                query = conn.createStatement();

                String sql = "SELECT startdate, expiration, active " + "FROM apikeys "
                        + "WHERE apikey = '" + apikey +"'";
                ResultSet result = query.executeQuery(sql);

                if(result.getFetchSize()>0 && result.getInt("active")==1){
                    Date now = new Date();
                    Date startdate = result.getDate("startdate");
                    Date expirationdate = result.getDate("expiration");
                    if(now.before(expirationdate) && now.after(startdate)){
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    return true;
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

}

【问题讨论】:

  • 几乎可以肯定是类路径问题。看看非常相似的票
  • 可能的重复与 Eclipse 无关。所以它不是完全重复的。
  • 我不确定我是否同意这是一个完全相同的副本,至少在这里我找到了在另一张票中找不到的答案。
  • 我的观点完全正确。我投票决定重新开放。

标签: java eclipse jdbc classpath


【解决方案1】:

Right click your project > Build Path > Add libraries 并添加 jar 文件。

还要确保它位于您的运行时路径上。 Right click the main class > run as > run configurations > Classpath

【讨论】:

  • 我认为将 jar 添加到运行时类路径也解决了问题,我会尽快确认。
【解决方案2】:

我唯一看到的是,您需要在项目中添加 MySQL 连接器的库,而不仅仅是在 Eclipse 库中。

【讨论】:

  • 连接器 jar 包含在项目库中。
【解决方案3】:

你如何运行它?

当你这样做时,你从那个 jar 中实例化一个类

Class.forName("com.mysql.jdbc.Driver")

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#forName%28java.lang.String%29

【讨论】:

    猜你喜欢
    • 2019-06-23
    • 2014-01-04
    • 2018-05-01
    • 1970-01-01
    • 2014-02-09
    • 2012-04-13
    • 2018-07-09
    • 2011-08-24
    • 2023-03-06
    相关资源
    最近更新 更多