• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class.forName error

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> "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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...)
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... or

This will give you the fully qualified name of whatever the name 'Class' is referring to.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff,

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

Thanks!
Amy
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic