aspose file tools*
The moose likes Linux / UNIX and the fly likes JAVA and MYSQL Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "JAVA and MYSQL Problem" Watch "JAVA and MYSQL Problem" New topic
Author

JAVA and MYSQL Problem

Roberto Aley
Greenhorn

Joined: Aug 03, 2008
Posts: 2
Hi I'm using Slackware 12.1, I configured MySQL and I can start it. After that, I downloaded the connector of http://dev.mysql.com/downloads/connector/j/5.1.html and I took the mysql-connector-java-5.1.7-bin.jar inside of this tar.gz, I copied the .jar connector to /usr/lib/jdk1.6.0_10/jre/lib/ext where I have my VM. Inmediatly I edited the file /etc/profile and I added to the path:



The connector have permissions 644.

And I did a test with this code:



And he throws me this log:

roberto@Slackware:~$ javac PruebaMySQL.java
roberto@Slackware:~$ java PruebaMySQL
java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at PruebaMySQL.<init>(PruebaMySQL.java:7)
at PruebaMySQL.main(PruebaMySQL.java:14)


Sorry for my english.
And I hope you can help me.

( Jesper Young: Corrected code tags )
[ December 15, 2008: Message edited by: Jesper Young ]
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5845
    
    7

Roberto, welcome to Java Ranch!

JAR files are not loaded from the PATH, but rather from CLASSPATH. Or better yet, use the -classpath option on the JVM command line to specify the location of the JAR file.

Also, you really should be using com.mysql.jdbc.Driver.
[ December 12, 2008: Message edited by: Peter Johnson ]

JBoss In Action
Roberto Aley
Greenhorn

Joined: Aug 03, 2008
Posts: 2
Thanks Peter.

I changed by com.mysql.jdbc.Driver.

I compiled with this code:


Is this code good?

But I obtained this results:

roberto@Slackware:~$ javac -classpath "$CLASSPATH:.:/usr/lib/jdk1.6.0_10/jre/lib/ext/mysql-connector-java-5.1.7-bin.jar" PruebaMySQL.java
roberto@Slackware:~$ java PruebaMySQL
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at PruebaMySQL.<init>(PruebaMySQL.java:7)
at PruebaMySQL.main(PruebaMySQL.java:14)


Thanks and sorry for my english.

EDIT:

It works!!!

I put on the terminal:


And compile with:
roberto@Slackware:~$ javac PruebaMySQL.java
roberto@Slackware:~$ java PruebaMySQL


And it works, Thanks!!!

But if I close the terminal program, the CLASSPATH was cleans, Can I change it permanently?.

Thanks.

EDIT2:

I don't had a CLASSPATH as environment variable, but, I create a new variable in /etc/profile Thanks.

[ December 12, 2008: Message edited by: Roberto Aley ]
[ December 12, 2008: Message edited by: Roberto Aley ]
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5845
    
    7

You can set CLASSPATH in your .bashrc or .profile (or similar) file.

Personally, I never set a global CLASSPATH. Instead, I would set up an Ant or Maven script to do the compile.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16308
    
  22

Actually, your problem is that you specified the MySQL jar on the classpath to javac - the compile stage, but it actually needed to be on the classpath for java - the execution stage.

You don't usually need to include a JDBC driver jar in the classpath when compiling, since JDBC is invoked via the javax.jdbc interface package, which is part of the enterprise JRE. The only time you'll need to do so is if you use some sort of vendor-specific extensions to JDBC.

On the other hand, if you don't specify it on the runtime, the Class.forName() call will fail when it goes to find the vendor JDBC class you've supplied to it and it isn't in the classpath.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAVA and MYSQL Problem