Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Class.forName

5 views
Skip to first unread message

tamasu

unread,
Nov 2, 2009, 3:11:54 PM11/2/09
to
Hi,

First of all I am new to java. I would like to connect to a database
using the Class.forName method but I am having some difficulties

If the said method is set in the main form everything works as it
should but if I can't figure out how I can instansiated through a
class. I am receiving java.lang.ClassNotFoundException

I can confirm that the driver is correct as I managed to connect
through netbeans using the same driver details

The driver I am using is - com.mysql.jdbc.Driver and the ConnectionUrl
is jdbc:mysql://localhost/<DatabaseName>

Any ideas?

Kind Regards
Jansen

Lew

unread,
Nov 2, 2009, 3:40:27 PM11/2/09
to
tamasu wrote:
> First of all I am new to [J]ava. I would like to connect to a database

> using the Class.forName method but I am having some difficulties
>
> If the said method is set in the main form everything works as it
> should but if I can't figure out how I can instansiated through a
> class. I am receiving java.lang.ClassNotFoundException
>

Can you be more specific as to what you mean by instantiating "through
a class"? Technically, loading the driver "in the main form" is
loading it through a class. As another technical nit, it's not
instantiating anything to call 'Class.forName()', just loading the
driver class.

The way to load the driver is to make sure that at some point before
you need database access some method is called that in turn calls
'Class.forName()' for the driver.

Has your study of Java so far taught you the difference between static
methods and instance methods?

--
Lew

Lew

unread,
Nov 2, 2009, 3:41:56 PM11/2/09
to
tamasu wrote:
> If the said method is set in the main form everything works as it
> should but if I can't figure out how I can instansiated through a
> class. I am receiving java.lang.ClassNotFoundException
>

I forgot to mention about ClassNotFoundException that either the fully-
qualified name (FQN) of the driver class is wrong or it's not in the
class path for the Java run.

--
Lew


John B. Matthews

unread,
Nov 2, 2009, 4:57:41 PM11/2/09
to

Roedy Green

unread,
Nov 2, 2009, 10:05:26 PM11/2/09
to
On Mon, 2 Nov 2009 12:11:54 -0800 (PST), tamasu
<tamas...@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>. I am receiving java.lang.ClassNotFoundException

the driver you load must exist in a jar on the classpath or in the
ext directory.

see
http://mindprod.com/jgloss/runerrormessages.html#CLASSNOTFOUNDEXCEPTION
http://mindprod.com/jgloss/classpath.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

An example (complete and annotated) is worth 1000 lines of BNF.

0 new messages