6
votes

I'm on OSX Mavericks, running Intellij 12, trying to add a datasource already defined in XML, using the persistence facet.

I can find and select the source fine, but when I try to test the connection I get:

Connection to XXXXX failed: Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:190) at com.intellij.persistence.database.console.RemoteJdbcServer.main(RemoteJdbcServer.java:15)

Looks like it's just a classpath problem, but I'm having a surprisingly rough time figuring out what I have to do to get the mysql jar on the classpath.

Every question I've found deals with adding the jar to the classpath of your compiled or testing application, that's NOT what I'm looking for here. My application connects to mysql fine, it's the IntellijIDE that's having problems. Where do I need to add the path to this file?

6

6 Answers

13
votes

For Intellij 15 (maybe lower versions) this works:

Add the JDBC library using Intellij:

  1. Goto View -> Tool Windows -> Database.
  2. In the Database window, select a database and click the icon for data source properties (cmd+i).
  3. In the next window goto MySQL in the Drivers section. In the window left under driver files click download. That's it.
4
votes

In IntelliJ goto View -> Tool Windows -> Database

Inside Click + button -> DataSource -> MySql

as shown below

enter image description here

In below screen enter your mysql connection details and click Test Connection...

make sure you download mysql driver for connection

enter image description here

2
votes

Either of the following options should work

add jar to the global libraries of the project.

put jar in the lib directory of intellij.

put jar in the ext lib of the jre that intellij is running under.

0
votes

Go to Itellij IDEA menue: File-->project structure-->Modules-->choose your module-->Dependencies-->Click + to add Library-->Choose mysql-connector-java-->Apply & OK

0
votes

My IntelliJ 2018.2.5 is pure standalone, without Maven and is not showing a Database menu under View/Tool Windows. The only thing that worked for me was documented here: Adding Jar files to IntellijIdea classpath and the solution was to add the JAR to Project Structure. I do not understand why IntelliJ does not pick up CLASSPATH. Adding the JAR to IntelliJ's lib tree also did NOT do it.

0
votes

I had this same issue, I did all suggested until I noticed there is a "Problems" index in the Project Structure window of IntelliJ. It said that there was a problem because my directory "lib" was not included in the Artifact. I just clicked "Fix" and after that, everything worked. Maybe this helps someone else.

enter image description here