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)
I even added the connector jar to project classpath. I am using Intellij IDE But for some reason, it won't connect. Any suggestions?