java.lang.ClassNotFoundException:com.mysql.jdbc.Driver - 不工作

时间:2014-02-17 10:36:59

标签: java mysql jdbc

我正在尝试运行此程序

import java.sql.*;
import java.io.*;

public class FirstExample
{
     // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost/EMP";

    //  Database credentials
    static final String USER = "root";
    static final String PASS = "pass";

    public static void main(String[] args) 
    {
        Connection conn = null;
        Statement stmt = null;
        try
            {
                //STEP 2: Register JDBC driver
                Class.forName("com.mysql.jdbc.Driver");

                //STEP 3: Open a connection
                System.out.println("Connecting to database...");
                conn = DriverManager.getConnection(DB_URL,USER,PASS);

                //STEP 4: Execute a query
                System.out.println("Creating statement...");
                stmt = conn.createStatement();
                String sql;
                sql = "SELECT title,duration,protocol,URL,thumbURL,favorite FROM Videos";
                ResultSet rs = stmt.executeQuery(sql);

                //STEP 5: Extract data from result set
                while(rs.next())
                {
                    //Retrieve by column name
                    String title = rs.getString("title");
                    String duration = rs.getString("duration");
                    String protocol = rs.getString("protocol");
                    String URL = rs.getString("URL");
                    String thumbURL = rs.getString("thumbURL");
                    String favorite = rs.getString("favorite");

                    //Display
                    System.out.println("Title:" + title);
                    System.out.println("Duration:" + duration);
                    System.out.println("Protocol:" + protocol);
                    System.out.println("URL:" + URL);
                    System.out.println("ThumbURL:" + thumbURL);
                    System.out.println("Favorite:" + favorite);
                }

                //STEP 6: Clean-up environment
                rs.close();
                stmt.close();
                conn.close();
            }
            catch(SQLException se)
            {
                //Handle errors for JDBC
                se.printStackTrace();
            }
            catch(Exception e)
            {
                //Handle errors for Class.forName
                e.printStackTrace();
            }
            finally
            {
                //finally block used to close resources
                try
                {
                    if(stmt!=null)
                    stmt.close();
                }
                catch(SQLException se2)
                {
                }// nothing we can do
                try
                {
                    if(conn!=null)
                    conn.close();
                }
                catch(SQLException se)
                {
                    se.printStackTrace();
                }//end finally try
            }
        System.out.println("Goodbye!");
        }
    }

但是我得到了ClassNotFoundException

D:\XML Tests>javac FirstExample.java

D:\XML Tests>java FirstExample
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at FirstExample.main(FirstExample.java:21)
Goodbye!

咨询了许多可用的问题,即将PATH系统变量设置为Connector目录,它仍然无法正常工作。 有什么帮助吗?

3 个答案:

答案 0 :(得分:2)

在类路径中添加包含mysql驱动程序类com.mysql.jdbc.Driver的jar。

答案 1 :(得分:0)

MySQL驱动程序jar添加到项目类路径并完成。

答案 2 :(得分:0)

该错误试图告诉您,当您使用MySql进行数据存储时,它无法找到所需的com.mysql.jdbc.Driver。所以你需要做的就是下载JConnector。然后将该jar文件导入到项目类路径中。然后你就不会得到错误。