This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
connection not possible, even after using the connector
aditya sural
Greenhorn
Joined: Mar 25, 2011
Posts: 8
posted
0
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
And remove the ".newInstance()" part. That does not belong there.
OCUP UML fundamental
ITIL foundation
aditya sural
Greenhorn
Joined: Mar 25, 2011
Posts: 8
posted
0
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
posted
0
And also I removed .newInstance(). But still not working
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
posted
0
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
posted
0
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.