Results 1 to 5 of 5
Thread: MySQL JDBC Driver ClassNotFound
- 06-17-2011, 07:24 AM #1
Senior Member
- Join Date
- Jun 2011
- Posts
- 100
- Rep Power
- 0
MySQL JDBC Driver ClassNotFound
Hi,
I have problem to make a connection to mySQL db
Here's the code in NetBeans 7
public class TestConnection {
public static void main(String[] argv) throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
if (!con.isClosed())
{
System.out.println("Connected");
con.close();
}
else {System.out.println("Not connected");}
}
}
I'm using XP n I have set the classpath in the system environment variable to the folder that hold the jar for msql connection.
However I get this error message :
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java: 200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51)
at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at TestConnection.main(TestConnection.java:14)
I've browsed the topics n see many things but mostly about classpath which I already set, so now in quite a confusion.
Anyway I intended to first check the connection to MySQL before anything else which I never do before.
I also use javac from command line with including -cp <path to mysql connection jar file> file.java....then run java file, also generating around the same error message
Thanks in advanceLast edited by Levian; 06-17-2011 at 07:28 AM.
- 06-17-2011, 07:51 AM #2
Senior Member
- Join Date
- Jun 2011
- Posts
- 100
- Rep Power
- 0
Changing the code into :
public static void main (String[] args)
{
Connection conn = null;
try
{
String userName = "root";
String password = "root";
String url = "jdbc:mysql://localhost:3306/test";
Class.forName ("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
System.out.println (e);
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
Just see from one post that seems working n here's the debug in netbeans
debug:
Cannot connect to database server
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
BUILD SUCCESSFUL (total time: 1 second)
I've set the library in Tools - Libraries...adding the path to mysql connector jar file n even specifically point to the jar file itself.
- 06-17-2011, 08:12 AM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
You are not adding MySQL library to your project. To add this using netbeans
Right click your project, you can see it in project window.
Then, click properties>libraries>Add Library>Import> select MySQL JDBC Library.
- 06-17-2011, 08:31 AM #4
Senior Member
- Join Date
- Jun 2011
- Posts
- 100
- Rep Power
- 0
That solve the problem with netbeans but I'm curious as to how that's done with command line if you know.
Anyway the problem is solved, thanks a lot
- 06-17-2011, 09:37 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
ClassNotFoundException: com.mysql.jdbc.Driver
By tBKwtWS in forum New To JavaReplies: 9Last Post: 06-15-2011, 07:01 PM -
com.mysql.jdbc.Driver Exception !
By RealSilhouette in forum New To JavaReplies: 6Last Post: 06-10-2011, 09:30 AM -
classnotfound exception in jdbc
By tariq9112003 in forum Advanced JavaReplies: 5Last Post: 06-07-2011, 10:47 AM -
com.mysql.jdbc.Driver
By uthpalaw in forum EclipseReplies: 2Last Post: 10-14-2010, 05:09 AM -
ClassNotFoundException com.mysql.jdbc.Driver
By Heather in forum JDBCReplies: 4Last Post: 03-31-2010, 12:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks