java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse

前端 未结 18 2027
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 15:41

What is wrong with the code there are lots of error while debugging. I am writing a code for a singleton class to connect with the database mysql.

Here is my code

相关标签:
18条回答
  • 2020-11-22 15:59

    JDBC API mostly consists of interfaces which work independently of any database. A database specific driver is required for each database which implements the JDBC API.

    First download the MySQL connector jar from www.mysql.com, then:

    Right Click the project -- > build path -- > configure build path

    In the libraries tab press Add External Jar and select your jar.

    0 讨论(0)
  • 2020-11-22 16:00

    For Maven based projects you need a dependency.

    <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
    </dependency>
    
    0 讨论(0)
  • 2020-11-22 16:04

    It seems the mysql connectivity library is not included in the project. Solve the problem following one of the proposed solutions:

    • MAVEN PROJECTS SOLUTION

    Add the mysql-connector dependency to the pom.xml project file:

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.39</version>
    </dependency>
    

    Here you are all the versions: https://mvnrepository.com/artifact/mysql/mysql-connector-java

    • ALL PROJECTS SOLUTION

    Add the jar library manually to the project.

    Right Click the project -- > build path -- > configure build path

    In Libraries Tab press Add External Jar and Select your jar.

    You can find zip for mysql-connector here

    • Explanation:

    When building the project, java throws you an exception because a file (the com.mysql.jdbc.Driver class) from the mysql connectivity library is not found. The solution is adding the library to the project, and java will find the com.mysql.jdbc.Driver

    0 讨论(0)
  • 2020-11-22 16:04

    For IntelliJ Idea, go to your project structure (File, Project Structure), and add the mysql connector .jar file to your global library. Once there, right click on it and chose 'Add to Modules'. Hit Apply / OK and you should be good to go.

    0 讨论(0)
  • 2020-11-22 16:05

    Trivial as it may seem in my case netbeans version maven project 7.2.1 was different. There is a folder in the project called dependencies. Right click and then it brings up a popup window where you can search for packages. In the query area put

    mysql-connector
    

    It will bring up the matches (it seems it does this against some repository). Double click then install.

    0 讨论(0)
  • 2020-11-22 16:05

    I Understood your problem add this dependency in your pom.xml your problem will be solved,

    https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.38

    0 讨论(0)
提交回复
热议问题