1

I'm writing JDBC code in which I have this import:

import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;

While executing I'm getting:

java.lang.ClassNotFoundException: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException

Though I have already added the mysql.jar in class path. Any solution for this? Do I have to add any other jar file for this class?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
manish
  • 57
  • 4
  • 1
    Since your code compiled, the class obviously existed in the classpath at compile-time. It would seem that you didn't include the .jar file in the classpath at runtime. – Andreas Jul 31 '17 at 20:11

2 Answers2

2

I found this error while upgrading my JDBC connection JAR file to mysql-connector-java-8.0.21.jar

And realised that package name is changed from com.mysql.jdbc.exceptions.* to java.sql.*

May be this will help you, if you replace

import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;

with

import java.sql.SQLIntegrityConstraintViolationException

May this help you to understand, if you have the similar issue: JAVA: cannot find symbol PreparedStatement (JAR Upgrade)

Krunal
  • 77,632
  • 48
  • 245
  • 261
-1

"java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" is NOT a compilation error. The solution is to make sure that the JAR file is on the classpath when you run the application.

If you're using Eclipse, follow below points

  1. Right click your project folder and open up Properties.

  2. From the right panel, select Java Build Path then go to Libraries tab.

  3. Select Add External JARs to import the mysql driver.

  4. From the right panel, select Deployment Assembly.

  5. Select Add..., then select Java Build Path Entries and click Next.

  6. You should see the sql driver on the list. Select it and click first.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
NS009
  • 7
  • 1
  • 1
  • 5
  • This doesn't address the problem of the question, or at least, _"java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" is NOT a compilation error_ indicates you haven't tailored your answer to the actual question. – Mark Rotteveel Jul 31 '17 at 21:30