Java MySQL : ClassNotFoundException -> Could not find driver

标签 java eclipse jdbc classpath

Possible Duplicate:
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;
    }

}

最佳答案

右键单击您的项目 > 构建路径 > 添加库 并添加 jar 文件。

还要确保它位于您的运行时路径上。 右键单击主类>运行方式>运行配置>类路径

关于Java MySQL : ClassNotFoundException -> Could not find driver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6203858/

相关文章:

使用 Subversive 时出现 Eclipse 错误

java - 我们可以使用普通的jdbc连接来进行Spring事务管理吗

SQL - 选择具有最匹配列的行

java - 通过jdbc批量插入记录时内存不足

java - @NotThreadSafe 未找到错误

java - Eclipse Maven 依赖项中存在冲突的 jar

java - 即时更改 Eclipse 标题中按钮上的文本

java - WSDL 使用 - 使用什么工具?

java - 字符串十六进制二进制 (java)

java - 如何在 Eclipse 中轻松运行不同的配置?