my dog learned polymorphism*
The moose likes JDBC and the fly likes connection not possible, even after using the connector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "connection not possible, even after using the connector" Watch "connection not possible, even after using the connector" New topic
Author

connection not possible, even after using the connector

aditya sural
Greenhorn

Joined: Mar 25, 2011
Posts: 8
I tried doing a simple connection with the database using java. I am using netbeans in ubuntu 9.10. I included the required connector for mysql database. This is my program-


I am getting following exceptions-
Exception in thread "main" 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.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at test.main(test.java:13)
Java Result: 1
I am new to JDBC. Please, please help
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

com.mysql.jdbc.Driver is not on your classpath.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
athi ram
Greenhorn

Joined: Feb 28, 2011
Posts: 6
I thing Driver file is missing. So check Driver File in lib folder..



Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

And remove the ".newInstance()" part. That does not belong there.


OCUP UML fundamental and ITIL foundation
youtube channel
aditya sural
Greenhorn

Joined: Mar 25, 2011
Posts: 8
I am learning this with two others. They are having no problems. We are working out these problems in our institute labs.
I would like to know where this Driver should be.
aditya sural
Greenhorn

Joined: Mar 25, 2011
Posts: 8
And also I removed .newInstance(). But still not working
anirudh jagithyala
Ranch Hand

Joined: Dec 07, 2010
Posts: 41
Did you install the SQL jdbc driver...?

The link might help you
http://www.devdaily.com/java/edu/pj/pj010024
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

For the sake of completeness, and in case you're copying very old code:
1) when a JDBC Driver is loaded, it is required to register itself with the ClassLoader (if you're new you can just assume this is 'static initialiser' magic and look into it another day)
2) When you DriverManager.getConnection(...) the DriverManager asks each of the Drivers that it knows about if they understand that URL. If one says 'yes' then that Driver is asked to return a Connection

Applying this to your code:

This causes the Class to be loaded by the ClassLoader. As in rule 1 above, it then registers itself with the DriverManager

The reason why I asked if you were referring to old code is that this code was required in Java 1.2 to manage a problem with ClassLoaders not immediately loading Classes. Java 1.2 is long gone so this is no longer required.
Worse, as it creates an instance of the Class but that Class is never used, it creates the possibility of a large number of unused instances waiting to be Garbage Collected.
Again this isn't something to worry about at this stage, just don't do it ;)
aditya sural
Greenhorn

Joined: Mar 25, 2011
Posts: 8
Yes. I am using netbeans and I added the mysql-connector jar file. But still throwing classNotFound exception
aditya sural
Greenhorn

Joined: Mar 25, 2011
Posts: 8
David O'Meara wrote:For the sake of completeness, and in case you're copying very old code:
1) when a JDBC Driver is loaded, it is required to register itself with the ClassLoader (if you're new you can just assume this is 'static initialiser' magic and look into it another day)
2) When you DriverManager.getConnection(...) the DriverManager asks each of the Drivers that it knows about if they understand that URL. If one says 'yes' then that Driver is asked to return a Connection

Applying this to your code:

This causes the Class to be loaded by the ClassLoader. As in rule 1 above, it then registers itself with the DriverManager

The reason why I asked if you were referring to old code is that this code was required in Java 1.2 to manage a problem with ClassLoaders not immediately loading Classes. Java 1.2 is long gone so this is no longer required.
Worse, as it creates an instance of the Class but that Class is never used, it creates the possibility of a large number of unused instances waiting to be Garbage Collected.
Again this isn't something to worry about at this stage, just don't do it ;)



Ok. I just copied that file for startup.It was the simplest ,so.. Thank you very much for the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: connection not possible, even after using the connector
 
Similar Threads
Unable to connect to mySQL DB.
Problem with loading driver
Exception in thread "main" java.lang.NoClassDefFoundError: Voice1
not able to connect to the database
Getting an Exception wile accessing data