• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAVA and MYSQL Problem

 
Roberto Aley
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Roberto Aley
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18304
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic