简体   繁体   English

尝试在Java中使用mysql时发生ClassNotFoundException

[英]ClassNotFoundException when trying to use mysql in java

I am trying to use an external mysql database in my java application, but I keep getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 我正在尝试在Java应用程序中使用外部mysql数据库,但我一直在获取java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
when the application tries to make a connection. 当应用程序尝试建立连接时。

This is the code I use to open the connection. 这是我用来打开连接的代码。 What is causing this? 是什么原因造成的?

// Post: A connection is opened if the current connection is not valid
    private static boolean open() throws SQLException {
        // If connection is still valid, return without doing anything
        try {
            if (connection != null && connection.isValid(1)) {
                return true;
            }
        }
        catch (SQLException e1) {
            close();
        }

        // Start connecting
        String driver   = "com.mysql.jdbc.Driver";
        String url      = "jdbc:mysql://mysql.starredout.com/" + database;
        String user     = "starredout";     
        String password = "starredout";
        try {
            Class.forName(driver).newInstance();
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        connection  = DriverManager.getConnection(url,user,password);
        return true;
    }

    // Post: If a connection was open, it is closed
    private static void close() {
        try {
            if (connection == null || connection.isClosed())
                return;
            connection.close();
        }
        catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.toString());
        }   
    }

The connector jar may not be on your classpath. 连接器jar可能不在您的类路径中。 If you are launching your application via the command line you should be able to manually add it by setting the classpath using the -classpath flag and following it with the path/to/the/jar/location. 如果通过命令行启动应用程序,则应该可以通过使用-classpath标志设置类路径并在其后跟随路径/ to / the / jar / location来手动添加它。

If your code is running in a Java EE application server or the like the classpath becomes a little more complex and you will need to provide more details. 如果您的代码在Java EE应用程序服务器等中运行,则类路径会变得更加复杂,您将需要提供更多详细信息。

That error means that it couldnt find the class you tried to make it run. 该错误意味着它找不到您试图使其运行的类。
This is probably caused because you forgot to link the driver library to your build path? 这可能是因为您忘记了将驱动程序库链接到构建路径吗?
The library is probably stored in a .jar file, so you have to mention that jar in your classpath. 该库可能存储在.jar文件中,因此您必须在类路径中提及该jar。

In case you use eclipse just right click your project ==> Build path ==> configure build path, then click the Libraries tab, and click Add JARs... and select the jar you wish to include in the classpath In other IDE's it should be a similar process 如果您使用eclipse,只需右键单击您的项目==>构建路径==>配置构建路径,然后单击库选项卡,然后单击添加JAR ...,然后选择要包含在类路径中的jar在其他IDE中应该是类似的过程

You need to grab the mySql Connector jar and place it in your class path. 您需要获取mySql Connector jar并将其放置在类路径中。 You can take a look at the docs here 您可以在这里查看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM