Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with my first JDBC program

 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic