Java Mysql:ClassNotFoundException ->找不到驱动程序

发布于 2024-11-11 16:29:56 字数 2933 浏览 3 评论 0原文

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

-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;
    }

}

Possible Duplicate:
ClassNotFoundException com.mysql.jdbc.Driver

I have included the mysql-connector-java-5.1.16-bin.jar into my Eclipse library, this code is based on tutorials that I have found on the web since I haven't used MySQL in Java before. Unfortunately I dont see where I use the acutual mysql-connector-java-5.1.16-bin.jar and the console prints Could not find driver. meaning the ClassNotFoundException is thrown. Do you see the (probably obvious) problem here that I dont see?

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;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瞳孔里扚悲伤 2024-11-18 16:29:56

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

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

Right click your project > Build Path > Add libraries and add the jar file.

Also make sure that it is on your runtime path. Right click the main class > run as > run configurations > Classpath

圈圈圆圆圈圈 2024-11-18 16:29:56

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

The only thing I see is that you need to add the library of the MySQL Connector in your project not only in your Eclipse Library.

裂开嘴轻声笑有多痛 2024-11-18 16:29:56

你怎么运行这个?

时,您会从该 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

how do you run this?

you instantiate a class from that jar when you do

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文