File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Setting up JDBC with MySQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Setting up JDBC with MySQL" Watch "Setting up JDBC with MySQL" New topic
Author

Setting up JDBC with MySQL

Martin Clifford
Greenhorn

Joined: Dec 02, 2002
Posts: 23
Hi everyone!
Sorry to bother, but I've searched, and attempted, and failed at setting this up, and it's kind of frustrating.
Is there someone out there who might be able to tell me how to get the MySQL JDBC drivers to work? I have my classpath set to:
c:\java\bin;C:\jdbc\mysql-connector-java-2.0.14-bin.jar
This includes the java path, and also the path to MySQL Connector/J. I'm new to JDBC, obviously, but I keep getting the "No Suitable Driver" Exception.
Any and all help is GREATLY appreciated )


Martin
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

If we could see your code where you are trying to establish the connection...
Also, when you say that is your classpath, where did you set that classpath?
At runtime? -- java -classpath....
Environment Variables?


GenRocket - Experts at Building Test Data
Martin Clifford
Greenhorn

Joined: Dec 02, 2002
Posts: 23
Okay, for some reason it's working now. Here's the code that I used to make it work:

Problem now is, I have NO idea why it's working. Maybe it's because before I wasn't using Class.forName().newInstance(); at all.
Does that sound like the culprit?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

not using Class.forName was exactly your problem. That is what actually loads the Driver for that DriverManager (among others) uses to access your Database. The Driver is what converts your JAVA instructions into instructions that a DBMS can understand. Kind of like a Gateway.
Good job trouble shooting!
Martin Clifford
Greenhorn

Joined: Dec 02, 2002
Posts: 23
So even if you set the enviro var, you still have to load the driver with Class.forName() for the DriverManager, huh?
Okay, I get it now (I think). I'll keep plugging at it, but thanks for the help! )
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

The classpath tells both the java compiler and the VM where to find additional API's/Libraries, whatever you want to call them.
When you put your Connector/J Driver in your classpath, all that did was add another search path so that when the compiler or the VM needs these libraries, it knows where to find them.
What JDBC does is use Reflection to load the Driver. That basically means that the Connector/J Driver.class file get's loaded and compiled on the fly, or at runtime instead of at compile time. I don't know why they did it this way.
It's just like with the standard API set. Just because they are available via the classpath doesn't mean they are being used.
So as you have probably infered, you not only need the Driver in your classpath so java can find it, but you also need to load it.
In fact, you only need the Connector/J Driver in your classpath when you are executing the code, not when compiling, because it compiles the driver at runtime. However, I have it in my classpath for both compile and execution. Just in case.
Here is a link to a tutorial I have started on the very basics of JDBC. I basically simplified the very simple JDBC Tutorial from Sun.
But it hits on a few details that Sun thought most people would infer. I however didn't so I though I would help others like me.
[ December 11, 2002: Message edited by: Gregg Bolinger ]
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
Gregg, the type 4 is also named III - what's up with that
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Thanks. Fixed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting up JDBC with MySQL