1

I have installed MYSQL 8.0.15 and I have mysql-connector-java-5.1.47-bin.jar driver, but my connection would fail. I have spent almost all day, but now I have started to think: am I using the right connector? When I was looking on website, it takes me to following page. But I couldn't see any driver there.

Where can I find the right connector/driver?

Gryu
  • 2,102
  • 2
  • 16
  • 29
Sara
  • 181
  • 1
  • 3
  • 16

1 Answers1

1

"MySQL Connector/J" is their product name for the JDBC driver.

Download MySQL Connector/J and extract the archive. You will find the file mysql-connector-java-8.0.15.jar in the top directory of that archive.

Java is platform-independent. I am using a Macbook, and I choose the platform-independent archive. All that means is that it's packages as a tar or zip archive, instead of an installer package for Linux or Windows. They used to offer MacOS installers, but they seem to have discontinued that.


There's an easier way to connect using the DriverManager interface. See example code here: https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-connect-drivermanager.html#connector-j-examples-connection-drivermanager

Make sure the mysql-connector-java-8.0.15.jar is listed in your CLASSPATH. Keep in mind my rule of Java programming:

In Java, 90% of problems are due to the CLASSPATH. The other 10% are due to GC.

https://twitter.com/billkarwin/status/809491612434767872

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • what about when it run Class.forName(JDBC_DRIVER); it throws exception, while JDBC_DRIVER = "com.mysql.cj.jdbc.Driver." – Sara Feb 24 '19 at 19:14
  • ok, I have used mysql-connector-java-8.0.15.jar but now it gives java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver – Sara Feb 24 '19 at 19:40
  • I debate that rule, at least 25% is people dealing with NPEs without knowing what they are – Rogue Feb 24 '19 at 19:51
  • Try removing the ".cj", their classpath has changed over time. If that doesn't work I'll go pull up my environment for the latest one. It should match whatever is in the jarfile you use – Rogue Feb 24 '19 at 19:52
  • 1
    @Sara If you get a `ClassNotFoundException` for `com.mysql.cj.jdbc.Driver` then either you have a 5.1.x MySQL Connector/J (and not 8.0.x), or you have **no** MySQL Connector/J at all on your classpath. – Mark Rotteveel Feb 25 '19 at 10:38
  • @MarkRotteveel This is the connector I am using *mysql-connector-java-8.0.15.jar* and i have added it to my project – Sara Mar 05 '19 at 17:08
  • 1
    @Sara I stand by my previous comment. You may have added it to your project, but that doesn't necessarily mean it is on your - runtime - classpath. – Mark Rotteveel Mar 05 '19 at 18:18
  • @MarkRotteveel ok I went to Files -> Project Structure (new menu opens) -> Modules (on the left) -> app -> Dependencies and there I can see :mysql-connector-java-8.0.15' Compile, Is tht good ? – Sara Mar 05 '19 at 19:48
  • and added into gradle as follow 'compile project(':mysql-connector-java-8.0.15')* – Sara Mar 05 '19 at 19:56
  • also I open jar and see path is com.mysql.cj.jdbc.Driver – Sara Mar 05 '19 at 20:05
  • 1
    You need to control the classpath when you compile and also when you run the app. Compile classpath doesn't implicitly make it find the jar when you run the app. – Bill Karwin Mar 05 '19 at 20:26