Problem registering driver jdbc - mysql
Hello,
I'm having a problem to register my driver for mysql. I'm working in win xp, netbeans 5.0 and mysql 5.0. I've put in my classpath this:
"C:\Archivos de programa\MySQL\mysql-connector-java-5.0.5"
and my code:
import java.sql.*;
publicclass SerDatos{
Connection conexion;
Statement sentencia;
ResultSet resultado;
public SerDatos(){
}
publicvoid Conectar(){
//Register driver
try{
Class.forName("src.com.mysql.jdbc.Driver");
}catch (Exception e){
System.out.println(e.getMessage());
}
// load connection mySql
try{
conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/movfinanc","root","u234");
sentencia = conexion.createStatement();
if (sentencia.execute("INSERT INTO area (Id_Area,Name) VALUES (1,'FISICA')")){
resultado = sentencia.getResultSet();
}
}catch (SQLException ex){
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
return;
}
}// fin del metodo conectar
}
when i call this class and method Conectar, i get the message of exception
src.com.mysql.jdbc.Driver
SQLException: No suitable driver found for jdbc:mysql://localhost:3306/movfinanc
SQLState: 08001
VendorError: 0
I think first line is exception about register driver, and how can fix this?
I guess if i fix first, second line will not exist, right?
Thanks for your help!!
# 1
>I'm having a problem to register my driver for mysql. I'm working in win xp, >netbeans 5.0 and mysql 5.0. I've put in my classpath this:
>"C:\Archivos de programa\MySQL\mysql-connector-java-5.0.5"
Add this "C:\Archivos de programa\MySQL\mysql-connector-java-5.0.5.jar" instead.
>Class.forName("src.com.mysql.jdbc.Driver");
use this Class.forName("com.mysql.jdbc.Driver");
instead.
See this http://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-basic.html#connector-j-examples-connection-drivermanager for more information.
Hope That Helps
# 2
Thanks for reponse, I'm trying like you said, but it doesn't works:com.mysql.jdbc.DriverSQLException: No suitable driver found for jdbc:mysql://localhost:3306/movfinancSQLState: 08001VendorError: 0any other solution?Thanks
# 3
I think you didn't unzip the driver zip you downloaded.
If so, go to C:\Archivos de programa\MySQL\mysql-connector-java-5.0.5
and unzip mysql-connector-java-5.0.5.zip. In the mysql-connector-java-5.0.5 directory, you'll find mysql-connector-java-5.0.5-bin.jar. Add this latter to your classpath instead of the other one.
By the way, use e.printStackTrace();
instead of System.out.println(e.getMessage());
As I previously told you, take a look at the official mysql connector-j documentation here http://dev.mysql.com/doc/refman/5.0/en/connector-j.html.
Hope That Helps
# 4
Where exactly do you set the classpath and where/how exactly are you running this piece of code? It look like that you runs it in a different JRE than the one where you're setting the classpath for.
# 5
I've unzip the driver, and running another time, say it:
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.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at guardaDatos.SerDatos.Conectar(SerDatos.java:32)
at prueba.PrincipalJFrame.jMenuItemPruebaGuardaActionPerformed(PrincipalJFrame.java:115)
at prueba.PrincipalJFrame.access$100(PrincipalJFrame.java:19)
at prueba.PrincipalJFrame$2.actionPerformed(PrincipalJFrame.java:77)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1170)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1211)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
SQLException: No suitable driver found for jdbc:mysql://localhost:3306/movfinanc
SQLState: 08001
VendorError: 0
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
# 6
Please answer my question.I have the idea that you don't fully understand the fenomen called "classpath".
# 7
>java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
That means that you haven't correctly set your classpath.
How did you add the mysql jdbc driver jar to your classpath ?
Anyway, there is another way to overcome this problem : add mysql-connector-java-5.0.5-bin.jar to JDK_DIRECTORY/jre/lib/ext/ and try again your application.
# 8
I have win xp:
At the control panel,
System,
Advance Options,
Environment Variables:
path: %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;;C:\Archivos de programa\MySQL\mysql-connector-java-5.0.5;C:\Archivos de programa\MySQL\MySQL Server 5.0\bin;C:\Archivos de programa\MySQL\mysql-connector-java-5.0.5\mysql-connector-java-5.0.5-bin.jar
# 9
... sorry, i forget answer other questions:
I'm running a frame and in the event of a jmenuitem, it call this class and method.
And in the User Variables this is the path:
C:\Sun\SDK\bin;C:\Archivos de programa\MySQL\mysql-connector-java-5.0.5
I don't know if need any more?
Thanks
# 10
the path configuration seems to be good.try this Class.forName("org.gjt.mm.mysql.Driver");instead of Class.forName("com.mysql.jdbc.Driver");
# 11
Noooo.. it doesn't works:(
# 12
The environment variable 'path' is definitely *not* the classpath.I still don't understand how/where exactly you're running this application, so I can't help you further.
# 13
Very strange !!!
Try this Class.forName("com.mysql.jdbc.Driver").newInstance();
See http://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-basic.html#connector-j-examples-connection-drivermanager for more information.
By the way, did you try to add mysql-connector-java-5.0.5-bin.jar to JDK_DIRECTORY/jre/lib/ext/ directory ?
(where JDK_DIRECTORY is the installation directory of your jdk)
Besides, in your environment path, there is no JAVA_HOME variable !!
# 14
Why is it very strange? And your suggestion is heavily pointless. When an CNFE is been thrown, the newInstance() will never be reached. And also, the newInstance() is not needed (for at least the recent JDBC drivers).
# 15
I've tried including .jar and its folder in the jdk directory, but it doesn't works.
About questions of BalusC, that I'm understanding is that i'm calling this method (Conectar) in a event of a jmenuitem. That's a try.:
private void jMenuItemImportarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new ImportaArchivoJDialog(this,true).addWindowFocusListener(new java.awt.event.WindowFocusListener() {
public void windowGainedFocus(WindowEvent windowEvent) {
}
public void windowLostFocus(WindowEvent windowEvent) {
CargaArchivo();
}
});
}
And if classpath is not Enviroment variable, where i can configure it?
# 16
opps I paste other event, this is the correct:
private void jMenuItemPruebaGuardaActionPerformed(java.awt.event.ActionEvent evt) {
SerDatos servidor = new SerDatos();
servidor.Conectar();
}
# 17
You're executing it as an Applet? Is it standalone or packaged in a JAR? If standalone, then you can use the system environment variable 'classpath' (instead of 'path'). If it is packaged in a JAR (which I highly recommend, if you want to make it portable), then you need to package the mysql JAR in the root path of the JAR file and point to it in the manifest.mf of the JAR.
# 18
Thanks for responseMy application is standalone, i want to make it portable, but if in the test it isn't running because the classpath.... ? I changed classpath instead path, and it still not works
# 19
I have a very similiar problem. I get the same error as mentioned in 1st post (No Suitable Driver).
I have tried running my application as a JFrame and it worked flawlessly, the error only occurs when running as JApplet.
I also tried running the applet in AppletViewer with the policy file (AllPermissions), and it still didnt work...
I'm lost....
Nejca at 2007-7-21 21:12:19 >
