• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

connection not possible, even after using the connector

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
com.mysql.jdbc.Driver is not on your classpath.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thing Driver file is missing. So check Driver File in lib folder..



 
Bartender
Posts: 2662
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And remove the ".newInstance()" part. That does not belong there.
 
aditya sural
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And also I removed .newInstance(). But still not working
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you install the SQL jdbc driver...?

The link might help you
http://www.devdaily.com/java/edu/pj/pj010024
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. I am using netbeans and I added the mysql-connector jar file. But still throwing classNotFound exception
 
aditya sural
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic