wood burning stoves 2.0*
The moose likes JDBC and the fly likes can't connect to MySQL Connector/J Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "can Watch "can New topic
Author

can't connect to MySQL Connector/J

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
I installed MySQL Connector/J but can't seem to load the driver. I set my classpath:

But when I execute thid code:


I get error:


What am I doing wrong?

thanks,
Alex


All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61415
    
  67

Is this in a web application? (The fact that you hace struts in your classpath leads me to ask this).

If so, the various servlet containers usually pay no attention to the system classpath. You need to put the jar file in one of the locations where the container will pick it up.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
no, this is not a web application. It's just a Swing Application on my local drive. Is my classpath set correctly? Have no idea what could be going wrong.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Is your JAR file actually in c:\mysql\ ?
I have a slightly older MySQL installation but my JAR file is in a subdirectory of c:\mysql\.

In my classpath I need:

So you might need:


I think that's a pretty strong case for changing the folder and/or JAR file names to something a bit more human-friendly!

Jules
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
I changed autoexec.bat to:
set CLASSPATH=.;C:\mysql\mysql-connector-java-3.0.14-production\mysql-connector-java-3.0.14-production-bin.jar;C:\jakatra-struts-1.2.1\lib\struts.jar;%PATH%;%JAVA_HOME%\lib\tools.jar;.%CLASSPATH%

but still does not work...this is getting annoying...
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Do the new settings show up in your classpath when you type: echo %classpath% in your command window? if not, you need to run autoexec.bat from your command window or reboot.

Can you confirm (by checking it out in the Windows Explorer) that your MySql JAR file is in the directory that you're setting in your classpath?

I've taken your exact code and it works fine.

You may also want to try running your code like this:


Jules
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
ok, if I include *jar file when I run class, it works fine. So what's the problem then?
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Well, I'm no expert on this, but I think it's something like the following:

Windows has different types of environment variable. These are, e.g. System and User. It's the system classpath that you want to set if running on your own PC (though I'm not sure it matters). What does matter is that if you simply set the classpath in a command window then, as soon as you close that command window, the classpath is lost. That is, next time you open a command window you will get the user classpath (if it exists) or otherwise the system classpath. I think that's basically accurate, but don't go publishing it! UNIX has similar behaviour if environment variables are set in sub-shells, etc.

Additionally, if you set environment variables in autoexec.bat you have to run it (i.e. on the command line or by rebooting) before they take effect.

Does that clarify?

So, is it working without the -cp option now? If not, what does "echo %classpath%" output? What does it output after you've typed autoexec?

Jules
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
Success!
Because I was using JCreator I had to set jar file in classpath of a project manually.
Thanks for your help guys!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can't connect to MySQL Connector/J