Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to connect to mySQL DB.

 
John Pisci
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Apologies if this question is asked a lot (I have used the search but couldn't find anything!)

I'm trying to connect to a mySQL database using the following code, but I keep getting a ClassNotFoundException: com.mysql.jdbc.Driver error.
I think it may be a problem with the mysql connector needing to be in the classpath, but as I intend to use migLayout, I need the classpath to be pointing to that.
My current classpath is ".;L\My Projects\MyJavaApps\miglayout-3.7"

I've added the 'mysql-connector-java-5.1.7-bin' file to the above folder in the hope that would satisfy it's need to be in the classpath, but it looks like it doesn't.

Any help/ pointers would be greatly appreciated!

Many thanks!

My code:


Full error message:
 
John Pisci
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've set the classpath as "...mysql-connector-java-5.1.7-bin.jar" and now it works.

However, I'm using miglayout in my application and the miglayout folder needs to be added to classpath for it to work.
I've tried adding the mysql-connector-java-5.1.7-bin.jar the miglayout folder and pointed the classpath to .jar. I can connect to the db this way, but cannot launch my miglayout application.
I then tried pointing the classpath to the miglayout folder (which has the .jar file in) and the miglayout app works, but I cannot connect to the db.

I know I'm doing something wrong, so how can I set it so I can have both working?

Thanks!

 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How exactly are you running the application?

If as plain Java class using java.exe, you need to set the classpath using -cp or -classpath argument.
If as JAR file using java.exe -jar, you need to set the classpath in the manifest file.
 
John Pisci
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!

I didn't realise I could have more than 1 filepath in the classpath.

I've been stuck on that all day!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic