aspose file tools*
The moose likes JDBC and the fly likes Connecting to a MySQL DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connecting to a MySQL DB" Watch "Connecting to a MySQL DB" New topic
Author

Connecting to a MySQL DB

J Dunn
Greenhorn

Joined: Oct 29, 2008
Posts: 5
It's probably something simple that im missing but I cant figure out how to start the connection. I have the mysql-connector-java-####-bin in the same directory I am running the code from but it generates an error saying class not found on the line with Class.forname() when I try to start up the driver so that I can make a connection.

Any tips would be appreciated, alternatively if you have an idea of an easier way to make the connection I am open to that. I am not attached to doing it this way, I just need to be able to connect to a DB on the same box so that I can add entries and run queries.

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Welcome to JavaRanch!

You need to explicitly add the jar to the classpath. Being in the same directory isn't enough for Java to find the driver.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Also, we have a forum specifically for JDBC questions. I'll move this to it for you.
J Dunn
Greenhorn

Joined: Oct 29, 2008
Posts: 5
I tried compiling with:

javac -classpath $HOME/path/mysql-connector-java-5.1.6-bin.jar testconnection.java

Which I believe should add it to the class path but it has the same result.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39425
    
  28
That will only work if you have a folder called path.
J Dunn
Greenhorn

Joined: Oct 29, 2008
Posts: 5
I'm compiling with the actual path between the home folder and the driver, I just left it out since it wasn't relevant.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Connecting to a MySQL DB