aspose file tools*
The moose likes JDBC and the fly likes Need help with my first JDBC program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Need help with my first JDBC program" Watch "Need help with my first JDBC program" New topic
Author

Need help with my first JDBC program

Souvvik Basu
Ranch Hand

Joined: Apr 05, 2010
Posts: 96
Hi
I am trying to write a standalone java class (with main function) to understand the working of JDBC. I have downloaded the MySQL installer for Windows from the mysql site and installed it. Now I am trying to run the following code to see if a successful connection is being made.



It compiles fine, but at runtime, throws an exception
java.lang.ClassNotFoundException : com.mysql.jdbc.Driver

Can anyone please point out what I need to do? My guess is, either my class name in incorrect, or I need to put the mysql jar in a particular folder. Which one is it? Also, this code above is not written by me. I got it from a tutorial site. Hence, is it possible that the values of

are incorrect for my particular case?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30776
    
157

Yes. You need to put that jar in your classpath. Since your program is being run at the command line, you can type "java -cp /path/to/jar FirstExample"


[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
Souvvik Basu
Ranch Hand

Joined: Apr 05, 2010
Posts: 96
I have a variable named Path = C:\Program Files (x86)\Java\jdk1.6.0_21\bin
I have another variable named JAVA_HOME = C:\Program Files (x86)\Java\jdk1.6.0_21

I have kept the jar in both the above folders, and also inside folder C:\Program Files (x86)\Java\jdk1.6.0_21\lib.

My java file is in the same place where I have all my other java files. And then I use the commands
javac FirstExample.java
java FirstExampe

The error is still there.

Can you please tell me where exactly you want me to put it, apart from these places? I must mention that I do not have any environment variable called CLASSPATH, and this hasn't prevented me from running my other java files successfully.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30776
    
157

Jeanne Boyarsky wrote:Yes. You need to put that jar in your classpath. Since your program is being run at the command line, you can type "java -cp /path/to/jar FirstExample"


Repeating this answer. The JDBC jar you mentioned needs to be in your environment CLASSPATH variable or the -cp argument and the command line. Non-JDBC programs would work just fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with my first JDBC program