Newbie JDBC help

TheDude05

Limp Gawd
Joined
Jan 27, 2005
Messages
393
Just started Java yesterday and need some help connecting to mysql through JDBC.

I'm using Netbeans5.0 and have added the mysql-jdbc-connector.jar to my project and I get a "class not found" error on the Class.forName line and a sqlException on the getConnection line. Heres that segment of code


Code:
Class.forName("com.mysql.jdbc.Driver").newInstance();
try {
     conn = DriverManager.getConnection(url, userName, password);
} catch (SQLException ex) {
     ex.printStackTrace();
}

I also have import java.sql.*; at the top.



I've tried using an import like so... doesn't work.

Code:
import com.mysql.jdbc.Driver;

How am I suppose to go about doing this? My preference is not have to copy the MySQL libraries to the Java folder. Help
 
The first thing you will want to deal with is getting that driver loaded. Without that driver, you will not be able to get anywhere. It looks like you are instantiating it correctly, so my guess is that you are not telling netbeans to look for that JAR (wherever it is located) in your classpath. You will want to make sure that wherever that JAR is located that you add it to your classpath when running that code. If I wanted to do this from the command line, I would do:

Code:
c:\> java -cp c:\path\to\mysql.jar;. MySQLJDBCTest

The "-cp" option specifies the classpath. It is a semi-colon delimited list of paths the Java Virtual Machine will look through when it tries to load classes. Note that I have specified a direct path to the jar file itself, it would also be sufficient to say "c:\path\to\" as well (IIRC). I also added the "." directory, assuming you were running the "java" command in the same directory as your compiled class file.

Let me know if you need more help --
 
I'm a little confused about the whole classpath thing. I thought that adding jars through netbeans would add those libraries to my projects classpath or am I not thinking right. I have a bunch of JasperReports and Jakarta jars added to my project and they all work fine.
 
I am not sure about NetBeans, I do not use it, however in the IDE I use I have to manually add JARs to the project I am working on. Do you remember doing this for your other JARs? Perhaps there is something different about this mysql JAR (it is not in the same folder as all the other JARs, perhaps?).
 
Adding Jars to Netbeans is similiar to Eclispe (I'm only using netbeans for the gui editor, code completion on this thing sucks :p). In Netbeans it has a 'folder' called libraries that you can right click on and add jars/folders, etc.

My 'MySQL' jar is in fact in another folder from the other jars. Maybe I'll try moving it and see what that does. I'm thinking it won't though because when I compile the program out to an executable jar it has a folder called lib with all my referenced jars. (Including the mysql-connector)


EDIT: Still no dice... This is driving me crazy
 
TheDude05 said:
Adding Jars to Netbeans is similiar to Eclispe (I'm only using netbeans for the gui editor, code completion on this thing sucks :p). In Netbeans it has a 'folder' called libraries that you can right click on and add jars/folders, etc.

My 'MySQL' jar is in fact in another folder from the other jars. Maybe I'll try moving it and see what that does. I'm thinking it won't though because when I compile the program out to an executable jar it has a folder called lib with all my referenced jars. (Including the mysql-connector)

Could you put your final JAR up somewhere? I can't see how the libraries in the other JARs are able to be referenced/loaded but the mysql JAR is not, when it is in the same path - my only guess is that perhaps your classpath is enumerating each JAR by its full path instead of just referencing the lib folder in the JAR. Have you tried running it un-JAR'd?
 
I'll zip it and upload it tomorrow as the files are at work. In the meantime I might play around with the mysql-connector here at home using eclispse instead.
 
Back
Top