File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Class.forName error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class.forName error" Watch "Class.forName error" New topic
Author

Class.forName error

Amy Caine
Ranch Hand

Joined: Jan 29, 2005
Posts: 30
Hello There Again,

I am using Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); in one of my programs, but it keeps on coming up with the error "the method forName(String) is not defined for the type Class". What am I doing wrong? The book I am reading doesn't specify that I need to do any special imports. I am importing java.sql.*. Since it throws a class not found exception, I already have it in a try block with a catch (ClassNotFoundException e) block behind it.

Any suggestions??

Thanks!
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

> "the method forName(String) is not defined for the type Class".

That's an unusual error. You haven't defined your own Class class, have you?

> What am I doing wrong? The book I am reading doesn't specify that I need to do any special imports.

You shouldn't; you should simply have the class specified by the fully-qualified name in your string available in your classpath.
Amy Caine
Ranch Hand

Joined: Jan 29, 2005
Posts: 30
Nope, the class I am working with doesn't even have the word "Class" in it.

I created another program using it and it works fine. I even tried copying and pasting the code from one program to another and editing it to fit the needs of the one I am doing now, but that doesn't work. I still get the same error.

Weird, huh? Any other ideas? I'm fresh out.

Thanks!
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
Try:

java.lang.Class.forName("...")

and see if that works. (This is not something you should have to do, but it could help diagnose the problem...)


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

... or

This will give you the fully qualified name of whatever the name 'Class' is referring to.
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
This may or may not be the problem, but, do you have this:



...at the top of your source code?

Or is, for some reason, the CLASSPATH set differently in the project that works, compared to the project that doesn't?

- Jeff
[ April 08, 2005: Message edited by: Jeff Jetton ]
Amy Caine
Ranch Hand

Joined: Jan 29, 2005
Posts: 30
Joel, David and Jeff,

Thanks for the replies! I am not sure what the issue is here. I cannot figure this out; it is so weird! And, to make it even stranger, I just re-coded it in a completely different package (I'm using eclipse) and it recognizes Class.forName(String). What is the difference between the two? I am importing java.sql.* at the beginning. I will try the other qualifiers that you have suggested, and I'll let you know tonight!

Thanks everyone,
Amy
Amy Caine
Ranch Hand

Joined: Jan 29, 2005
Posts: 30
Jeff,

What do you mean by CLASSPATH? Where can I find those settings?

Thanks!
Amy
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
Originally posted by Amy Caine:
Jeff,

What do you mean by CLASSPATH? Where can I find those settings?


The CLASSPATH is an environment variable that the java compiler looks at for hints on how to find some classes when it compiles. (Generally, these would be your own, user classes, so this probably isn't your problem). You can specify a different CLASSPATH when you run the compiler, and javac will temporarily use that.

How to set it depends on your platform and what, if any, IDE you're using. Sun has some info on the CLASSPATH, as it pertains to the Windows version of the java SDK.

There are other compiler options that might (just maybe) cause Java to not know what a class is. The "target" and "bootclasspath" options, for example. Still, Class.forName() has been around since 1.0. Hmmmm... I'm just tossing out things here--I'm really not sure what the culprit is.

- Jeff
Amy Caine
Ranch Hand

Joined: Jan 29, 2005
Posts: 30
OK, I tried using java.lang.forName(...) and it worked. So thanks! I also tried the Class.class.forName() and there it was! All ready to work. Man oh man...

Now, I have one more issue and then I will stop bugging everyone for a bit; I promise. I am trying to connect to a db and insert records into it. The insert works, but I also get an exception, saying that "No ResultSet was produced SQL State: null". I have my code in try / catch blocks so it catches the errors. It inserts the record fine, but still gives me the error BEFORE it reports the insert. So, I commented out the code to write the record to the database and I get strange results: every field comes up as null in the console (there are only 6 fields right now). Here is my code:



There is also a Customer class in the same package that has the setFirstName(), setLastName(), setAddress(), etc. methods that it is using. That is also how I am creating a new customer object.

The results, when run WITH the setCustomerInfoToDB, are these:

Exception: No ResultSet was produced SQL State: null
Customer from DB:
Clark Kent 211 North Metropolis IL 60651

When run WITHOUT the setCustomerInfoToDB (I just commented it out), these are the results:

Customer From DB:
null null null null null null

Why is it giving the nulls? Any ideas??

Thanks!
Amy Caine
Ranch Hand

Joined: Jan 29, 2005
Posts: 30
Sorry everyone,

I didn't realize that colon and then '0' was the code for getting one of those little guys in there. The code that should be there, minus the spaces, is:

String url = "jdbc : odbc : CustomersDB";

If that still didn't work, here is the nitty gritty (take the spaces out):

String url = "jdbc colon odbc colon CustomersDB";

in both cases.
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
I think your problem is here:



Try changing it to:



The executeQuery() method returns a results set and is used for SELECT statements. For SQL commands that do not return a result set (INSERT, CREATE TABLE, etc.) use executeUpdate();

Hope that helps!

- Jeff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class.forName error
 
Similar Threads
How to use the getConnection() method?
connection pooling
Connecting to remote database
True statement
When static { .. } executed ?