Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can't connect to MySQL Connector/J

 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, if I include *jar file when I run class, it works fine. So what's the problem then?
 
Julian Kennedy
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Success!
Because I was using JCreator I had to set jar file in classpath of a project manually.
Thanks for your help guys!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic