ClassNotFoundException: com.mysql.jdbc.Driver [duplicate]

流过昼夜 提交于 2019-12-29 08:15:17

问题


This question has been asked a lot here, but i still can't fix my problem: I put mysql-connector-java-5.1.18-bin into C:\Program Files\Java\jre6\lib\ext folder. I have this code:

    // Load the database driver
    Class.forName("com.mysql.jdbc.Driver");
    // Get a connection to the database
    Connection conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/mysql", "root", "4958ps");
    // Get a statement from the connection
    Statement stmt = conn.createStatement() ;
    // Execute the query
    ResultSet rs = stmt.executeQuery( "SELECT * FROM Cust" ) ;
    // Loop through the result set
    while( rs.next() )
        System.out.println( rs.getString(1) ) ;
    // Close the result set, statement and the connection
    rs.close() ;
    stmt.close() ;
    conn.close() ;
} catch( SQLException se ) {
    System.out.println( "SQL Exception:" ) ;
    // Loop through the SQL Exceptions
    while( se != null ) {
        System.out.println( "State  : " + se.getSQLState()  ) ;
        System.out.println( "Message: " + se.getMessage()   ) ;
        System.out.println( "Error  : " + se.getErrorCode() ) ;
        se = se.getNextException() ;
    }          
} catch( Exception e ) {
    e.printStackTrace();
} 

and i get a ClassNotFoundException with the following stack trace:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at pack.Test.main(Test.java:14)

I also changed CLASSPATH variable to be C:\Program Files\Java\jre6\lib\ext\mysql-connector-java-5.1.18-bin Any ideas?


回答1:


The zip you downloaded should contain a file named mysql-connector-java-5.0.8-bin.jar or similar. Version might be different. Check that it is present and check that it is directly in your classpath (not in a subdirectory etc.).

If that's alright, check that your classpath variable is not overwritten by something else. If you execute your program manually do it like this: java -cp "C:\Program Files\Java\jre6\lib\ext\mysql-connector-java-5.0.8-bin.jar:." YourClass

If you are using an IDE such as eclipse, check your build-path settings there. (In eclipse you would right click your project -> Properties -> Java Build Path -> Add external JARs and then supply the path of your connector...jar.




回答2:


I think the problem is you are putting mysql-connector-java-5.1.18-bin file rather you have to put "mysql-connector-java-5.0.5.jar" file there.

i have made a web application in eclipse(IDE). so if you are making a web application on eclipse(IDE) then you can put "mysql-connector-java-5.0.5.jar" in the lib folder. below is the path where i put my "mysql-connector-java-5.0.5.jar" file...

C:\Users\Abhishek\workspace\MyProject\WebContent\WEB-INF\lib\mysql-connector-java-5.0.5.jar




回答3:


The CLASSPATH variable can't be just the directory, it has to have the actual jar name.




回答4:


a bat file would look like

@echo off
set CLASSPATH=program.jar;mysql-connector-java-5.1.18-bin.jar

:start

start javaw.exe MainClass

program.jar (would contain the MainClass class as the main class) and the mysql.jar would have to reside in the same directory.

For Eclipse you have to set your project Build Path to include the mysql...jar.




回答5:


I encountered the same problem and what I did to solve it:

  1. Downloaded the .jar accordingly to the server used: For mySQL I downloaded mysql-connector-java-5.1.18-bin.jar

  2. Added CLASSPATH in SYSTEM VARIABLES with the whole name of the .jar used. If you already have CLASSPATH var then add in it, as in PATH ";Path to Jar/jar file name" something like: CLASSPATH = d:\requisites\mySQL_connector\mysql-connector-java-5.1.18-bin.jar

  3. In Eclipse project: Right click on project->Build Path-.Configure build path. Add external JARs and add the jar file. When you succesfully do this step it will appear on the project in "Reference Libraries"

  4. An important step(if you use Android Development Tools framework as I am using) is to add also that JAR file in libs folder of your app. I did this by copy-paste the jar from my path(d:\requisites\mySQL_connector) to libs folder of my project.

Restarted the project and it worked on second run.




回答6:


Try this

If you create the Dynamic Java Project(In Eclipse)

  1. Check the your build path for respective project

    • Step 1. Right click on your Project and Select Properties

    • Step 2. On the right side you will get the Deployment Assembly

    • Step 3. Click On the add and select Archives from File system

    • Step 4. Click on the again add and select the your respective database jar files.



来源:https://stackoverflow.com/questions/8745872/classnotfoundexception-com-mysql-jdbc-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!