aspose file tools*
The moose likes JDBC and the fly likes classnotfound Java->mysql jdbc problem! 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 "classnotfound Java->mysql jdbc problem!" Watch "classnotfound Java->mysql jdbc problem!" New topic
Author

classnotfound Java->mysql jdbc problem!

herbey zepeda
Greenhorn

Joined: Oct 04, 2005
Posts: 5
Hi, I wnat to connect to mySQL fromo Java, I followed all the instructions from the mySql web page, yet I havent been able to do it, when I compile the java progrm I keep getting class not found exceptions, I am using Fedora Core and Java was already installed-well I chose to install it when I installed the operating system, the point is that the paths are all assigned by he system not by me.

WHat can I do?, Ive been trying over andover again with no results, hopefully somebody can help, I tried to include all the configuration in this email

thank you


my java program is the following:
t.java
----------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class t {


public static void main (String[] args) {
System.out.println("Hello, world!\n");

Class.forName("com.mysql.jdbc.Driver");

}

}
-------------
when I javac t.java

I have the following error

4. ERROR in t.java
(at line 11)
Class.forName("com.mysql.jdbc.Driver");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Unhandled exception type ClassNotFoundException

-----
when I comment out the Class.forName the hello world rogram runs fine
---------------------------
# System-wide Java configuration file -*- sh -*-
#
# JPackage Project <http://www.jpackage.org/>

# Location of jar files on the system
JAVA_LIBDIR=/usr/share/java

# Location of arch-specific jar files on the system
JNI_LIBDIR=/usr/lib/java

# Root of all JVM installations
JVM_ROOT=/usr/lib/jvm

# You can define a system-wide JVM root here if you're not using the default one#JAVA_HOME=$JVM_ROOT/java-gcj

# Options to pass to the java interpreter
JAVACMD_OPTS=
~
--------------------

[root@localhost test]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
-------------------
echo $JAVA_HOME
--------------
ls /usr/share java*

java
java-1.3.0
java-1.4.0
java-1.4.1
java-1.4.2
java-1.5.0
javadoc
java-ext
java-utils

-------
mysql-connector-java-3.1.13-bin.jar is under

/usr/share/java
---------
in /etc/profile I have:

export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-3.1.13-bin.jar

--------
in /root/.bash_profile i have
CLASSPATH=/usr/share/java/mysql-connector-java-3.1.13-bin.jar:/usr/lib/java-ext/mysql-connector/mysql-connector-java-3.1.13-bin.jar

export CLASSPATH
----------------
when I echo $PATH i have
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

This has nothing to do with paths or configuration -- it's a basic Java coding issue.

When you call the method Class.forName(), it tries to find and load the given class. If the class file is not found at that time -- i.e., when the program is run, not when it's being compiled -- then the method will throw ClassNotFoundException, a checked exception. Like all checked exceptions, you are required to either catch it and handle it right here, or add this exception type to your own method's "throws" declaration. So, for example, you might write:



[Jess in Action][AskingGoodQuestions]
herbey zepeda
Greenhorn

Joined: Oct 04, 2005
Posts: 5
Thanks Ernest,

Even if I catch the exception though, Im still in trouble, if the class is not found at runtime, what can I do in th exception handling routine to make my program find the class. My objective is to establish a connection to mySQL and I understand that I need the driver class, then Im back in square zero again.
Or is there another way to tackle te problem of registering the driver connector class? Or maybe I did not understand what you suggested in your reply

Thank you
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If you can't find the driver at runtime, then your program can't run, and should exit. This is just as if you started the MS Word spell-checker and it found that the dictionary files weren't there. I'd hope that what the program would do is report the problem, then exit gracefully; there's nothing else it can do.

Same with JDBC code: if the driver class is missing, report it to the admin as an installation problem, and quit. Just because Java makes you handle the exception, that doesn't mean there's going to be any way to recover!
herbey zepeda
Greenhorn

Joined: Oct 04, 2005
Posts: 5
Ernest,

Im the only owner and Admin as everyhing is going on in my pc. And I would like to connect to mysql from java so I really appreciate your help but I am trying to connect to the database and keep running to process the data obtained from mysql, not so much exiting gracefully before the actual connection takes place, which of course will be important at some point in my program once I get it running

Thank you
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

OK. I think you're taking this all much too seriously. The class files don't disappear while the program's running: they're either they're when they need to be, or they're not. The Class.forName() needs to be executed once, and only once, during the program. If you get the exception, then print a message and exit; there is nothing else you can do. Then it'll be time to worry about classpaths and things -- i.e. you'll need to fiddle around to get your program running!
herbey zepeda
Greenhorn

Joined: Oct 04, 2005
Posts: 5
thanks for your patience Ernest

so,

what you mean is:

independently of the fact that the class is found or not, everytime youuse Class.forName() you have to catch the exception?
I am reading about it in the web but is not yet clear...

Actually think Ive now reached the fiddling phase when I run:
public class t {


public static void main (String[] args) {
try
{
System.out.println("Hello, world!\n");
Class.forName("com.mysql.jdbc.Driver");
System.out.println("MySQL Driver Found");
}
catch (ClassNotFoundException e)
{
System.out.println("MySQL Driver NOT Found");
e.printStackTrace();
}
}

}
------------------
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by herbey zepeda:

what you mean is:

independently of the fact that the class is found or not, everytime youuse Class.forName() you have to catch the exception?


Absolutely. The compiler forces you to deal with the possibility that the class won't be found; that's what you're doing right now. This is completely independent of whether it ever will be found or not; that's something to work out after you've compiled the program and are trying to run it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: classnotfound Java->mysql jdbc problem!