ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi - am having troulble connecting to mysql (on linux) from java. I am running a simple jdbc program to test the connection and get the error - "Error : Driver not found for URL: jdbc:mysql://localhost/test" . I downloaded mysql-connector-java-3.0.7-stable-bin.jar from mysql.com, installed in $J2EE_HOME/lib, $JAVA_HOME/lib and added to my CLASSPATH. What am I missing ?
ALso, I have multiple java packages installed in /usr/java -
I'm using code essentially identical to yours and I can connect. The only thing I can think is that it isn't finding the mysql-connector jar file. I've got mine at $JAVA_HOME/jre/lib/ext. The other thing to try is explicitly reference the jar file at run time with the classpath flag.
As for the multiple java installs, it should be fine as long as each is in its own directory under /usr/java. However, with that many installations, I'd be REAL tempted to set everything with a -cp flag at run time.
Hmm. I've only got J2SDK 1.4.1_02 on my box so everything points there. I wonder if your $CLASSPATH is causing trouble since it is pointing to two jar files in two different Java versions. The other potential problem is that you have two different versions of the mysql-connector jar in your 1.4.1 install. That might be confusing the compiler (although I would have expected it to complain). Is there a reason why you have both installs on your CLASSPATH? I know that multiple versions of Java can co-exist on a single machine, but when coding you might want to have only one on the CLASSPATH. However, I'm a little out of my depth here, so maybe one of the Java gurus here could comment.
When you tried java -cp, did you explicity point to the jar (/usr/java/j2sdk1.4.1_02/jre/lib/ext/mysql-connector-java-3.0.7-stable-bin.jar) or did you use $CLASSPATH? Just for giggles, it might be worth compiling and running using explicit jar files
Finally, here is some code that absolutely works on my computer. It looks an awful lot like yours:
Again thanks. The only reason that I have both in the CLASSPATH variable was for a test. I just tried explicitly defining both versions of mysql connect (from /tmp) (java -cp /tmp/mysql-connector-java-3.0.7-stable/mysql-connector-java-3.0.7-stable-bin.jar Connect, and java -cp /tmp/mysql-connector-java-2.0.14/mysql-connector-java-2.0.14-bin.jar Connect) with a run and get same error. BTW, I get the same error w/ your code. I'll keep looking ....................
I guess the prob is with ur connection string , have u gave permissions to any user using the grant command.. ?
theres a simple example in the unzipped dir of the jdbc driver ..read that..& try it will work
Folks - thank you again for your help - I found my difficulty. The reason that java did not find the correct jar was due to the different environments that I have installed on my machine. Java was originally installed as part of the RH 8 distribution (gcj) and when I updated/added for tomcat I also installed more later versions of java. This is where all my java env. variables pointed to. But when I comiled it, by default, used the original java environment from RH, which I guess knew nothing about the new tomcat installs. By compiling explicitly with the $JAVA_HOME all worked as expected. Does anyone know how/if I should uninstall the original gcj javac so that by default is uses my later installs?
Thanks again - Gregg
You probably don't have to delete the gcj version but you do probably need to go through your $PATH and remove that directory. You can always use the which command to figure out which javac the system is using (which javac).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.