This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Customer Requirements for Developers and have Marcho Behler on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Unable to connect to mySQL DB. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Unable to connect to mySQL DB." Watch "Unable to connect to mySQL DB." New topic
Author

Unable to connect to mySQL DB.

John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
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

Joined: Dec 19, 2008
Posts: 44
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

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Dec 19, 2008
Posts: 44
Thanks!

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

I've been stuck on that all day!
 
 
subject: Unable to connect to mySQL DB.
 
It's not a secret anymore!