wood burning stoves 2.0*
The moose likes JDBC and the fly likes Mysql JDBC Cant load..... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Mysql JDBC Cant load....." Watch "Mysql JDBC Cant load....." New topic
Author

Mysql JDBC Cant load.....

Miguel Enriquez
Ranch Hand

Joined: Mar 13, 2004
Posts: 94
Here the code:

//------------------------------
import java.sql.*;

public class getmySQLConnection
{
public static void main(String[] args)
{
DB db = new DB();
Connection conn=db.dbConnect(
"jdbc:mysql://localhost:3306/test", "root", "");
}

}

class DB
{
public DB() {}

public Connection dbConnect(String db_connect_string,
String db_userid, String db_password)
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(
db_connect_string, db_userid, db_password);

System.out.println("connected");
return conn;

}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
};
//------------------------------

here the path:

C:\programa\java\Mysql>path
PATH=c:\SunJava\AppServer\jdk\bin\;c:\sun\AppServer\jdk\jre\bin;C:\Program Files
\Borland\Delphi7\Bin;C:\Program Files\Borland\Delphi7\Projects\Bpl\;C:\WINDOWS\s
ystem32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\system32\nls;C:\WINDOWS\s
ystem32\nls\ESPANOL;C:\Program Files\Sybase\Pocket PowerBuilder 1.0;C:\PROGRA~1\
Borland\Delphi6\Bin;C:\PROGRA~1\Borland\Delphi6\Projects\Bpl;C:\Program Files\Qu
ickTime\QTSystem\;C:\Program Files\Common Files\Autodesk Shared\;C:\SunJava\AppS
erver\bin;C:\Sun\AppServer\bin;C:\Sun\AppServer\jdk\lib;C:\PROGRA~1\CompacW\Bin;

i have the connector in:

C:\programa\java\Mysql>dir c:\SunJava\AppServer\jdk\bin\*.jar
Volume in drive C has no label.
Volume Serial Number is 13A8-A897

Directory of c:\SunJava\AppServer\jdk\bin

07/12/2005 08:29 p.m. 440,483 mysql-connector-java.jar
1 File(s) 440,483 bytes
0 Dir(s) 1,404,538,880 bytes free

C:\programa\java\Mysql>

here the error:

C:\programa\java\Mysql>java -cp . getmySQLConnection
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:268)
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:164)
at DB.dbConnect(getmySQLConnection.java:23)
at getmySQLConnection.main(getmySQLConnection.java:8)

C:\programa\java\Mysql>

i download the .jar file from: http://dev.mysql.com/downloads/connector/j/3.2.html

my mysql version is: 3.23.55-nt

thanks for your advices...
Miguel Enriquez
Ranch Hand

Joined: Mar 13, 2004
Posts: 94
I downloaded the netbeans and try here and worked, why in Cmd Line not?

thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18712
    
    8

Because the jar containing the driver isn't in your classpath. While you are running in Netbeans it has its own way of handling the classpath, but the command line is different. Here's a link to the tutorial explaining it:

http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html
Miguel Enriquez
Ranch Hand

Joined: Mar 13, 2004
Posts: 94
the classpath are ok...see please the last path...

C:\>set
ALLUSERSPROFILE=C:\Documents and Settings\All Users.WINDOWS
APPDATA=C:\Documents and Settings\Miguel Enriquez\Application Data
CLASSPATH=C:\IDSE2\jre141\lib\rt.jar;c:\SunJava\AppServer\jdk\bin\mysql-connector-java.jar
CommonProgramFiles=C:\Program Files\Common Files
COMPUTERNAME=HPMIGUEL
ComSpec=C:\WINDOWS\system32\cmd.exe
HOMEDRIVE=C:
HOMEPATH=\
LOGONSERVER=\\HPMIGUEL
NUMBER_OF_PROCESSORS=2
NWLANGUAGE=English
OS=Windows_NT
Path=c:\SunJava\AppServer\jdk\bin\;c:\sun\AppServer\jdk\jre\bin;C:\Program Files
\Borland\Delphi7\Bin;C:\Program Files\Borland\Delphi7\Projects\Bpl\;C:\WINDOWS\s
ystem32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\system32\nls;C:\WINDOWS\s
ystem32\nls\ESPANOL;C:\Program Files\Sybase\Pocket PowerBuilder 1.0;C:\PROGRA~1\
Borland\Delphi6\Bin;C:\PROGRA~1\Borland\Delphi6\Projects\Bpl;C:\Program Files\Qu
ickTime\QTSystem\;C:\Program Files\Common Files\Autodesk Shared\;C:\SunJava\AppS
erver\bin;C:\Sun\AppServer\bin;C:\Sun\AppServer\jdk\lib;C:\PROGRA~1\CompacW\Bin;

PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
PGLOCALEDIR=C:\Program Files\PostgreSQL\8.0\share\locale
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 15 Model 2 Stepping 9, GenuineIntel
PROCESSOR_LEVEL=15
PROCESSOR_REVISION=0209
ProgramFiles=C:\Program Files
PROMPT=$P$G
QTJAVA=C:\Program Files\JavaSoft\JRE\1.3.1_15\lib\ext\QTJava.zip
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\WINDOWS
TEMP=C:\DOCUME~1\MIGUEL~2\LOCALS~1\Temp
TMP=C:\DOCUME~1\MIGUEL~2\LOCALS~1\Temp
USERDOMAIN=HPMIGUEL
USERNAME=Miguel Enriquez
USERPROFILE=C:\Documents and Settings\Miguel Enriquez
windir=C:\WINDOWS
WINDOWS_LOGIN=0

C:\programa\java\Mysql>dir C:\SunJava\AppServer\jdk\bin\*.jar
Volume in drive C has no label.
Volume Serial Number is 13A8-A897

Directory of C:\SunJava\AppServer\jdk\bin

07/12/2005 08:29 p.m. 440,483 mysql-connector-java.jar
1 File(s) 440,483 bytes
0 Dir(s) 1,145,425,920 bytes free

C:\programa\java\Mysql>

NOTE i run my program: (not know if the -cp . are the problem, anyway i copy tje mysql connector to C:\programa\java\Mysql and test, but the problem continue...).

C:\programa\java\Mysql>java -cp . getmySQLConnection
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:268)
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:164)
at DB.dbConnect(getmySQLConnection.java:23)
at getmySQLConnection.main(getmySQLConnection.java:8)

Thanks
Miguel Enriquez
Ranch Hand

Joined: Mar 13, 2004
Posts: 94
WORKED!!!


but not liked me.....

i added my workdir(c:\program\java\mysql) My pc properties advanced environment variables.... classpath edit
when i try run only typed: java myprogram (before i use java -cp . getmySQLConnection) i think -cp override the classpath....


My dude is why when i copy the mysql java connector to c:\program\java\mysql and typed: java -cp . getmySQLConnection not worked ???

anny comments

thanks
any comments?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
If you are compiling and running Java apps from the command line, I strongly
suggest you learn ant. Once you have a build file you like, it's easy to
modify it for another project:

http://ant.apache.org/
[ December 08, 2005: Message edited by: Jeff Albrechtsen ]

There is no emoticon for what I am feeling!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mysql JDBC Cant load.....