1

First of all, I know there are a lot of answers about this and I already checked them all, but this is a bit more specific I think.

I am developing a REST web service using Jersey, but I have this little problem when connecting to the mysql database. I have added the connector dependency in the pom.xml:

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

And if I check the build options the relative jar is in the eclipse java build path, but I get the error anyway, unless I add

Class.forName("com.mysql.jdbc.Driver");

before creating the connection.
I tried creating a java project adding the connector driver manually in the java build path and it works, so I guess it is something strictly related to maven or tomcat. I am using java 7 and tomcat 7.

Pyrox
  • 551
  • 5
  • 17
  • Can you show the error please? Are you positive it is happening on that line? Also, if you look at the pom within eclipse and click on the "Dependency Hierarchy" view of it, can you find your mysql-connector-java on the right hand side? – Josh Chappelle Dec 28 '15 at 14:19
  • Nevermind my comment. I misunderstood your question a bit. – Josh Chappelle Dec 28 '15 at 14:23
  • The error is the one in the title, which disappears if I add that line. I would like to avoid it because it shouldn't be necessary. – Pyrox Dec 28 '15 at 14:25
  • See my answer. If you are using a version of Java older than 6 then it is necessary. It's the way JDBC used to work. – Josh Chappelle Dec 28 '15 at 14:26
  • When you say `adding the connector driver manually`, you mean the jar or the instruction `Class.forName("com.mysql.jdbc.Driver");`? – ikken Dec 28 '15 at 14:26
  • Ah, nevermind. I see that you are using Java 7. It would help if I read the whole thing...:-) – Josh Chappelle Dec 28 '15 at 14:28
  • If you have multiple versions of Java on your machine, make sure eclipse isn't using an older version than Java 6 to run your program. Check your execution environment settings in eclipse. – Josh Chappelle Dec 28 '15 at 14:29
  • I only have java 7 and 8 installed on this machine, so I don't think that's the problem. Also, by creating a new simple java project and adding the jar to the build path manually it works as expected. – Pyrox Dec 28 '15 at 14:31
  • If you are using basic jdbc, you need to register the driver once using that code na? – Tom Sebastian Dec 28 '15 at 14:35
  • It shouldn't be necessary, in fact it isn't needed if I create a new project and add the jar manually – Pyrox Dec 28 '15 at 14:37
  • I've had the same issue, but could not figure out the reason for this was. But you could just leave the Class.forName call, or use a Datasource instead. [Here](https://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html#JDBC_Data_Sources) you can read, how to do it, it's really simple, and you get the added benefit of container managed connection pooling. – Adamsan Jan 08 '16 at 15:20

1 Answers1

0

This is an eclipse issue, you have to have the jar in eclipse Maven Dependencies, you can look on: Maven dependency issue in eclipse

Community
  • 1
  • 1
  • I don't know what you exactly mean by that, but I should already have the jar in maven dependencies, because I can see it if I check build options -> java build path -> libraries – Pyrox Dec 28 '15 at 14:30
  • try this: http://stackoverflow.com/questions/8620127/maven-in-eclipse-step-by-step-installation – Adrian Duta Dec 28 '15 at 14:33