2

EDIT: Fixed the issue - I wasn't declaring the dependency within the apache tomcat plugin:

<extraDependencies>
                                <extraDependency>
                                    <groupId>org.apache.derby</groupId>
                                    <artifactId>derby</artifactId>
                                    <version>10.1.3.1</version>
                                </extraDependency>
                                <extraDependency>
                                    <groupId>javax.mail</groupId>
                                    <artifactId>mail</artifactId>
                                    <version>1.4</version>
                                </extraDependency>
                                <extraDependency>
                                    <groupId>mysql</groupId>
                                    <artifactId>mysql-connector-java</artifactId>
                                    <version>5.1.38</version>
                                </extraDependency>
                            </extraDependencies>

As well as being declared in the pom as seen below.


Question:

I'm trying to use the mysql jdbc driver in my maven project:

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

But i'm getting:

java.lang.ClassNotFoundException: "com/mysql/jdbc/Driver";
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.ollynural.app.database.retrievedatabase.DatabaseAccessor.returnSummonerDTOFromDatabaseUsingName(DatabaseAccessor.java:53)

EDIT: Tried using the string instead of CLASS, and error is:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

And DatabaseAccessor returnSummonerDTOFromDatabaseUsingName is:

    String URL = prop.getProperty("URL");
    String USER = prop.getProperty("USER");
    String PASS = prop.getProperty("PASS");
    String CLASS = prop.getProperty("CLASS");
    String TABLE_UNIVERSITY_INFO =     prop.getProperty("TABLE_BASIC_INFO");
    try {
        Class.forName(CLASS);
    } catch (ClassNotFoundException e) {
        System.out.println("Can't find SQL Driver");
        e.printStackTrace();
    }

The properties are:

URL="jdbc:mysql://localhost:3306/league_database_schema";
USER="username";
PASS="password"
CLASS="com.mysql.jdbc.Driver";

The properties are getting to the class fine, but I have no idea why it can't find the class. I'm relatively new to maven, and this was working before i moved over to intellij and eclipse, but any help would be incredibly helpful!

Thank you

6
  • Can you please try Class.forName("com.mysql.jdbc.Driver")? Looks rather strange, that the exception shows the full qualified name with slash. And can you use other libraries, which are added using maven? Just to be sure, that Maven and your build path are configured correctly. Commented Dec 14, 2015 at 23:55
  • Yes I can use other libraries fine, I think you may have a point i'll try that now :) Commented Dec 14, 2015 at 23:58
  • Tried above, and now am getting 'java.lang.ClassNotFoundException: com.mysql.jdbc.Driver' So not the problem unfortunately Commented Dec 15, 2015 at 0:02
  • How are you launching this thing? Commented Dec 15, 2015 at 0:44
  • Running using: mvn tomcat7:run and doing mvn clean compile package and install before Commented Dec 15, 2015 at 7:58

2 Answers 2

3

I would recommend checking the repository to see if the jar file has actually been installed.

Within the repository, you should find it in: (repository name)/mysql/mysql-connector-java/5.1.38/

There should be a jar file mysql-connector-java-5.1.38.jar and a pom file mysql-connector-java-5.1.38.pom

I believe that should be it for the repository, if anyone has more ideas/corrections please share. Thanks.

Sign up to request clarification or add additional context in comments.

1 Comment

Have checked yesterday, it is there - I also deleted all the repository files and then got Maven to redownload the driver and other dependencies.
0

I can't tell by the fragment you posted, but is your dependency declared in the dependencies section of your pom, and not in dependencyManagement?

1 Comment

I'm quite new to working with a pom, I'm looking at: this link about it, but could you explain what it is/why it is needed? I will try putting it in a dependencyManagement tag

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.