0

The error I am getting is PY4JAVAERROR :

an error occurred while calling o313.load: Java.lang.classnotfoundexception: com.mysql.jdbc.Driver

has anyone encountered this before?

enter image description here

Kai-Sheng Yang
  • 1,535
  • 4
  • 15
  • 21
Paul Corcoran
  • 113
  • 1
  • 9
  • this is not a MySQL connection error. The driver just isn't on your classpath – Stultuske May 15 '22 at 11:19
  • Hi Stultuske, my mysql-connector is present in /usr/share/java/mysql-connector-java-8.0.29.jar and i have called it in the spark query. – Paul Corcoran May 15 '22 at 11:22
  • 2
    what do you think "ClassNotFoundException signifies"? – Stultuske May 15 '22 at 11:23
  • I am not using classes, I have attached a picture to my original post to illustrate the way im attempting. – Paul Corcoran May 15 '22 at 11:26
  • The fact the driver is in /usr/share/java/mysql-connector-java-8.0.29.jar does not mean it is on the classpath of your application. And you definitely are using classes. What do you think `com.mysql.jdbc.Driver` is? – Mark Rotteveel May 15 '22 at 11:27
  • @PaulCorcoran you are "not using classes"? Are you being serious? – Stultuske May 15 '22 at 11:28
  • apologies, I meant explicitly using class: to create this. I understand that under the hood it is using classes. I have called the driver in the spark.read.format in the query so I am at a loss to what is going wrong and where. It is a bit out of the comfort zone. – Paul Corcoran May 15 '22 at 11:31
  • @PaulCorcoran the problem is, you are trying to use that class, and it's not on the classpath – Stultuske May 15 '22 at 11:46

3 Answers3

0

Place mysql-connector jar in your classpath.

Prasad Tamgale
  • 325
  • 1
  • 12
0

That occurs when you don't have a MySQL connector in your classpath. If you are using a maven project you can try adding the dependency to your pom.xml

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

If you are not using a dependency management tool like Maven or Gradle you can download the jar online and then add it to your classpath. Here is the link. https://dev.mysql.com/downloads/connector/j/

Remember to do a bit of research for the correct versions before you download. Either of the two options will definitely work.

Amos Machora
  • 486
  • 3
  • 12
  • thank yoiu @AmohPrice, I have downloaded the connector for ubunut 20.04 on that link and it seems to have installed on my os. when you say add it to your classpath what do you mean? – Paul Corcoran May 16 '22 at 23:54
  • Hey @PaulCorcoran . Look at this [question](https://stackoverflow.com/questions/2396493/what-is-a-classpath-and-how-do-i-set-it) read the answers you will definitely understand. – Amos Machora May 17 '22 at 06:29
  • @PaulCorcoran if you don't understand it. A dependency management tool like Maven or Gradle will help you out because it does the downloading and adding to your classpath. so if you do not want to do the hard work. Think about it. – Amos Machora May 17 '22 at 06:32
-1

It seems the mysql connectivity library is not included in the project. Solve the problem following one of the proposed solutions:

MAVEN PROJECTS SOLUTION Add the mysql-connector dependency to the pom.xml project file:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version> // your desired version
</dependency>

Here you are all the versions: https://mvnrepository.com/artifact/mysql/mysql-connector-java

ALL PROJECTS SOLUTION Add the jar library manually to the project.

Right Click the project -- > build path -- > configure build path

In Libraries Tab press Add External Jar and Select your jar.

You can find zip for mysql-connector here

Explanation: When building the project, java throws you an exception because a file (the com.mysql.jdbc.Driver class) from the mysql connectivity library is not found. The solution is adding the library to the project, and java will find the com.mysql.jdbc.Driver

Akzy
  • 1,817
  • 1
  • 7
  • 19
  • You shouldn't automatically assume maven is being used – Stultuske May 15 '22 at 11:47
  • Yes, you are right, but it could be possible, that's why I mentioned both. Thanks for highlighting this. – Akzy May 15 '22 at 11:51
  • I am using ubuntu and have installed the mysqlconnector jar, I seem to be missing the driver set up which is what seems to be my problem. thank you for your answer – Paul Corcoran May 15 '22 at 12:19
  • Please accept the most relevant answer If this solve your question. – Akzy May 15 '22 at 12:22
  • its helped me understand that it is the jdbc driver that is missing but I still have issues adding to project. I am using ubuntu with pyspark jupiter notebooks – Paul Corcoran May 15 '22 at 12:31