-1

I am trying to develop Swing application and it contains connection to MySQL 5 database. So, I pulled mysql connector dependency using Maven, but I still get java.lang.ClassNotFoundException: mysql-connector-java-5.1.6.jar.

My code: pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
</project>

Connection method:

    public Connection connect() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.driver");
        Connection connection= DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/myDatabase","root","");
        return connection;
    }

Exception I get:

java.lang.ClassNotFoundException: com.mysql.jdbc.driver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at Test.connect(Main.java:29)
at Test.<init>(Main.java:21)
at Main.main(Main.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Directory structure: Directory structure

I even added the connector jar to project classpath. I am using Intellij IDE But for some reason, it won't connect. Any suggestions?

user4080732
  • 137
  • 4
  • 14

1 Answers1

0

You need to provide the dependency to mysql-connector-java via the pom.xml. Add

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

to your pom.xml (which should be named pom.xml and not maven.xml).

Your own groupId, artifactId and version should of course reflect your own project - so in the end you would have the following pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>mygroup</groupId>
  <artifactId>myartifact</artifactId>
  <version>1.0.0-SNAPSHOT</version>

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

Btw: 5.1.6 is old - check https://mvnrepository.com/artifact/mysql/mysql-connector-java for the current one (6.0.5 currently)

And - as user2004685 pointed out it should be Class.forName("com.mysql.jdbc.driver")

Sebastian
  • 877
  • 1
  • 9
  • 20