2

I am trying to connect to a MySQL Database using the mysql-connector-jar, which gets imported via maven. I have the dependency in the POM file as follows:

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

And I initialize my connection with the code below:

String dbUrl = "jdbc:mysql:host";
String dbClass = "com.mysql.jdbc.Driver";
String query = "update aem_assets set path = (?), assetid = (?), filename = (?), cadid = (?) where id = (?)";
try {
    Class.forName(dbClass).newInstance();

However, I keep getting java.lang.ClassNotFoundException: com.mysql.jdbc.Driver not found. I am trying to avoid manually adding the jar to the build path since my company uses maven for builds and we want this tool to be run from multiple instances without having to add jars.

I have also tried it without the newInstance() method.

I am referencing this article for implementation: https://dimitrisli.wordpress.com/2010/08/06/jdbc-mysql-maven-working-example/

Edit: I have added the JAR file to the module dependency. enter image description here

However it is STILL giving me the error. Am I missing something?

Blaris
  • 55
  • 1
  • 8
  • 1
    check this one http://stackoverflow.com/questions/8779631/jdbc-driver-class-not-found-com-mysql-jdbc-driver – Nidhi257 Feb 23 '17 at 17:19
  • You need the JAR on the runtime classpath, not at build time – Gyro Gearless Feb 23 '17 at 17:29
  • Did you notice the note on the blog page that says "During runtime the MySQL driver is loaded into classpath using the reflective *Class.forName();* By the way, since JDBC 4.0, this is not necessary and this line can be omitted." – Gord Thompson Feb 23 '17 at 17:47
  • How are you executing this. The driver is not on the runtime classpath. Also except with a very old (10+ years ago), buggy MySQL driver, using `newInstance()` has never been necessary. – Mark Rotteveel Feb 23 '17 at 17:54
  • I repeat my question: How are you executing this? – Mark Rotteveel Feb 23 '17 at 20:17
  • A servlet calls it. I am deploying the code to a localhost server where a front end javascript calls the backend servlet, which executes the method with the database initialization. – Blaris Feb 23 '17 at 20:21

0 Answers0