Results 1 to 15 of 15
- 07-19-2010, 06:19 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
How to configure Java mysql driver and CLASSPATH?
Hello,
I'm using this code to test out the jdbc mysql driver:
--------------
import java.sql.Connection;
import java.sql.DriverManager;
public class Test {
public static void main(String[] args) {
Class.forName("com.mysql.jdbc.Driver");
}
}
I've put the mysqljdbc.jar in the appropriate folder and set the CLASSPATH as well to .;C:\Program Files\Java\jre6\lib\ext\mysqljdbc.jar;C:\Program Files\Java\jre6\lib\ext\QTJava.zip
When I compile I get a ClassNotFoundException.
What am I doing wrong??
Thanks, any help is greatly appreciated.
- 07-19-2010, 08:05 PM #2
Do you have code errors?
Show us the stack trace
- 07-19-2010, 08:22 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
Hi gafa, thanks for the reply.
as requested,
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at advjavaassignment.Main.main(Main.java:15)
thanks again.
- 07-19-2010, 08:23 PM #4
What jar file is the file: com.mysql.jdbc.Driver.class in?
How are you issuing the java command? Show the command line.
Show the classpath setting by opening a command prompt and Entering: Set
Then copy the contents here by:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.Last edited by Norm; 07-19-2010 at 08:26 PM.
- 07-19-2010, 08:35 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
the class is in the jar file mysqljdbc.jar
CLASSPATH=.;C:\Program Files\Java\jre6\lib\ext\mysqljdbc.jar;C:\Program Files\Java\jre6\lib\ext\QTJava.zip
The code I'm using is as is shown above, I'm just trying to connect to the database since I'm going to be working on a data driven app.
Thanks for the reply and for the patience, i'm new to java as you may have noticed.
- 07-19-2010, 08:48 PM #6
And the command line you used?
- 07-19-2010, 08:53 PM #7
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
javac Test.java using windows command prompt.
- 07-19-2010, 09:33 PM #8
javac is the compiler. It does not execute the class file that it creates.
Can you COPY and PASTE here the contents of the command prompt window?
Don't type it in or edit it, we need to see the FULL contents.
- 07-19-2010, 09:41 PM #9
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
I:\Java\Assignment\Other Stuff>javac Test.java
Test.java:8: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be
thrown
Class.forName("com.mysql.jdbc.Driver");
^
1 error
I:\Java\Assignment\Other Stuff>
- 07-19-2010, 09:42 PM #10
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
the stacktrace i showed you earlier was from netbeans as i tried the same code there and it autogenerated the try catch.
thanks.
- 07-19-2010, 10:03 PM #11
Is the problem solved now?
- 07-19-2010, 10:27 PM #12
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
no, still the same compilation error
- 07-19-2010, 10:49 PM #13
Add a try{}catch block around the code that can throw the exception
- 07-20-2010, 07:18 AM #14
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
Hi again norm, after some digging about and trial & error I may have found something, however I'm a bit confused.
When I use this code as below
import java.sql.Connection;
import java.sql.DriverManager;
public class Test {
public static void main(String args[]) {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver Loaded");
}
}
I get this result when attempting to compile:
I:\Java\Assignment\Other Stuff>javac Test.java
Test.java:9: unreported exception java.lang.ClassNotFoundException; must be caug
ht or declared to be thrown
Class.forName("com.mysql.jdbc.Driver");
^
1 error
I:\Java\Assignment\Other Stuff>
==============================
When I use this code :
import java.sql.Connection;
import java.sql.DriverManager;
public class Test {
public static void main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver Loaded");
} catch(Exception e) {
e.printStackTrace( System.err);
}
}
}
It compiles fine and runs fine :
I:\Java\Assignment\Other Stuff>javac Test.java
I:\Java\Assignment\Other Stuff>java Test
Driver Loaded
I:\Java\Assignment\Other Stuff>
=============================
How and why does the try catch affect compilation of this code?
Why is it throwing a class not found exception ONLY when not using a try catch?
I'm a bit confused , though glad that there seems to be a solution.
This seems to be a common issue for many beginners.
Thanks again.
- 07-20-2010, 09:51 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Um, you probably ought to go through some more basic Java stuff if you don't know about Exceptions and Exception handling.
Have a tutorial.
Put simply it's not throwing the exception. The compiler (exceptions are thrown at runtime, not compile time) is telling you that Class.forName() throws a checked exception, which means you have to handle it. It won't allow the code to compile without you handling the exception.
Similar Threads
-
configure hibernate with mysql in eclipse galileo
By surya.bhavanasi in forum EclipseReplies: 0Last Post: 07-07-2010, 06:23 AM -
Problems with MySQL Driver
By islan in forum JDBCReplies: 7Last Post: 08-06-2009, 04:47 PM -
Configure Classpath
By Doctor Cactus in forum New To JavaReplies: 3Last Post: 01-08-2009, 01:11 PM -
can't register a MySQL driver
By prfalco in forum New To JavaReplies: 4Last Post: 02-03-2008, 11:13 PM -
mysql driver problem
By mokingsu in forum JDBCReplies: 4Last Post: 01-17-2008, 05:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks