-1

why I cant to MySQL and have error in this code

public class test1 {

    public static void main(String[] args) {
        try {
        //  Class.forName("com.mysql.jdbc.Driver");
            Class.forName("com.mysql.jdbc.Driver");

        } catch (ClassNotFoundException e) {
            System.err.println(e.getMessage());
            e.printStackTrace();
        }
    }

}

I am downloading connection java and set classpath in my setting but my problem not destroyed please help to me

my error is

com.mysql.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    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 test1.main(test1.java:7)
  • What the error message says? – Mouad EL Fakir Mar 14 '17 at 12:33
  • com.mysql.jdbc.Driver java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 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 test1.main(test1.java:7) – MohammadReza Ghafarpour Mar 14 '17 at 12:34
  • thanks for your help and vote :( – MohammadReza Ghafarpour Mar 14 '17 at 12:38
  • "and set classpath in my setting" apparently this classpath doesn't include that JAR with driver. To help you more we would need to know classpath settings and location of that JAR. – Pshemo Mar 14 '17 at 12:45
  • I doubt if you really improted jdbc driver properly..IDE gave you suggestion to wrap around try catch it seems. – minigeek Mar 14 '17 at 12:54
  • @minigeek "IDE gave you suggestion to wrap around try catch" `Class.forName` is declared that it `throws ClassNotFoundException`. So since there is that *possibility* our code needs to handle such scenario by either using try-catch, or by adding declaration that error will be rethrown in our method. It doesn't mean that exception will be thrown for sure because compiler knows that classpath doesn't contain such class (classes can be even created dynamically later). – Pshemo Mar 14 '17 at 13:03
  • @Pshemo oh.thanx.. I thought it automatically detects.. Thanx for info :) – minigeek Mar 14 '17 at 13:04
  • @minigeek Compiler would be able to detect that some type is not present in classpath if we would use it directly like `Driver d = new Driver();`. But in `Class.forName("com.mysql.jdbc.Driver");` `"com.mysql.jdbc.Driver"` is just a string, without meaning for compiler. Anyway in such case compiler wouldn't ask us to add some try-catch, but he would simply fail to compile such code. – Pshemo Mar 14 '17 at 13:06
  • yes my ide suggestion but have error – MohammadReza Ghafarpour Mar 14 '17 at 13:08
  • @Pshemo ohh ohk :)) – minigeek Mar 14 '17 at 13:09
  • @MohammadRezaGhafarpour did you put .jar file in lib? – minigeek Mar 14 '17 at 13:10

2 Answers2

1

Try using Maven to to make sure you have the latest driver..This would probably make it easier.

First upgrade your project to maven:

Right click your project and click Configure -> Convert to Maven Project.

Once done find your pom.xml file in your project and add the following dependency:

XML to ADD to POM.XML:

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.6</version>
</dependency>

Then try, this should work. Using maven is a lot easier with managing dependencies like this.

Example POM.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Test</groupId>
  <artifactId>Test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <dependencies>  
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>6.0.6</version>
    </dependency>  
  </dependencies>

</project>

EDIT 1: Trying using this instead:

Class.forName("com.mysql.cj.jdbc.Driver");
DazstaV3
  • 61
  • 9
1

Error clearly says you don't have added the driver to your project!!! You have to add the *.jar  driver to your project.

Put this in your lib directory, then restart

Why is this happening?

Class.forName("com.mysql.jdbc.Driver"); it tries to load the driver, but it is not getting it, this is the reason you are getting java.lang.ClassNotFoundException

You can find the jar file here

minigeek
  • 2,766
  • 1
  • 25
  • 35