Monday, April 2, 2012

ClassNotFoundException is the biggest pain in the ass ever!

Over the past few days, I've deviated a bit from my schedule, despite the fact that I just recreated it.  Right now I should be working on the Java classes that encapsulate the different parts of recipes and meal plans, but after creating the barebones classes with setter and getter methods, I felt it would be difficult to test any methods without having some basis for loading the objects.  In other words, I felt it would be difficult if not impossible to make sure my classes work unless I can load them interactively from the database.

A little while back, I found a bunch of really good templates for connecting my Java program to the MySQL database.  I especially liked one that separated the difference pieces of registering the connection, obtaining a connection, and using the connection.  I decided to use these pieces and separate out execution code to take a SQL statement and return a ResultSet object, leaving it up to the calling class to iterate through that and create the respective objects.  My goal was simply to only have a basic set of static methods to handle all of the database interactions so that I don't have to recreate the database connection controls in other objects.

While I think my code is set up right and should work, I'm stymied by the dumbest thing.  The very first step in using JDBC doesn't want to work and I can't think of any reason why not!

For those of you unfamiliar with JDBC, the very first step is to register what kind of database you'll be connecting to.  It takes the simplest line using a method from the Class class - Class.forName("com.mysql.jdbc.Driver");  I've read through numerous books and countless websites and this is simply the only line that's used to register the database.  They all mention that part of the registration is that you have to have the JDBC jar file in the CLASSPATH variable and then this line in your program and voila! you're good to go.

And that's where the title of this post comes into play.  I have set the CLASSPATH variable to match the JDBC containing folder.  I've set it it reference the folder and file.  I've tried both settings with 3 different JDBC builds.  I've tried this in the NetBeans IDE and I've tried it in the BlueJ IDE.  Though I've been able to access the database from NetBeans in the Services dialog, and have verified I can reach all the tables, I still can't connect my program to it.  I've uninstalled and reinstalled MySQL many times with different security settings (though I don't figure that's the problem). Hours and hours now I've been working on this. 3 days I've been working on this.

And every single freaking time I try this first step to database connectivity, completely independent of any attempt to actually connect or query the database, the Class.forName("com.mysql.jdbc.Driver"); fails with the same exception - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver.


I'm at a loss.  I don't know where to turn or who to ask.  I've read other people's examples, I've read forums, I've read user guides and wikis and websites.  And every single one of them says this should work.  Without this piece working, my entire project might as well be scrapped and I might as well accept failure.  So if anyone reading this has any ideas, any ideas at all... help... please! 


*Update: I did it. I don't quite know how, but I did it.  I re-installed SQL Server with Norton disabled after reading something about how the firewall may block all incoming connections to the database.  Still no success but my NetBeans Services thing stopped working.  Then I came across this website: http://www.linglom.com/2007/12/05/accessing-mysql-on-netbeans-using-jdbc-part-i-create-a-connection/ and found my solution.  I couldn't get my code to work or connect to the right JDBC connector until I followed this website's instructions to add the JDBC driver to the package library.  Suddenly everything started working and I can move on. YAY!!!!

No comments:

Post a Comment