Confusing facts...

G

getsanjay.sharma

Hello to all programmers out there.

I was just wondering:

· What is the actual difference between "import com.mysql.jdbc.driver"
and Class.forName("com.mysql.jdbc.driver") if in the end what they
both do is load the class ? Why is the second one used for Database
code?

· What is the real difference between the run() and start() method of
Thread? I know that we have to overload the run method of Thread while
writing our own threads, but why call start() when we can just call
the run() method itself? Any special reason for going the way around
when the source code of start() in the end is something like:

public void start()
{
if(target != null)
target.run();
}

· I am currently facing a peculiar problem in which on one hand using
the Class.forName("com.mysql.jdbc.Driver") works for a local program
but the same raises a ClasssNotFoundException when run on localhost ?

Thank you for your time.
 
K

Kai Schwebke

· What is the actual difference between "import com.mysql.jdbc.driver"
and Class.forName("com.mysql.jdbc.driver") if in the end what they
both do is load the class ? Why is the second one used for Database
code?

From JLS:
"An import declaration allows a named type to be referred to by a simple
name (§6.2) that consists of a single identifier. Without the use of an
appropriate import declaration, the only way to refer to a type declared
in another package is to use a fully qualified name."

So import just adds a shortcut to write shorter code.

Class.forName("...") does actually force the class loader to load
the class at runtime.
· What is the real difference between the run() and start() method of
Thread? I know that we have to overload the run method of Thread while
writing our own threads, but why call start() when we can just call
the run() method itself?

start() spawns a new thread executing run(). Calling run() directly
executes run() in the calling thread.
the Class.forName("com.mysql.jdbc.Driver") works for a local program
but the same raises a ClasssNotFoundException when run on localhost ?

Here the driver may miss in the class path.



Kai
 
T

Tom Hawtin

· What is the actual difference between "import com.mysql.jdbc.driver"
and Class.forName("com.mysql.jdbc.driver") if in the end what they
both do is load the class ? Why is the second one used for Database
code?

Import doesn't actually create any code. By convention, JDBC drivers
install themselves into java.sql.DriverManager in their static
initialiser. In order to load a class use Class.forName (the one
argument variant, or the three argument variant with true) or just use
the class in some way.

Instead of using Class.forName and DriverManager, you can use the driver
directly. In fact, if you were going to hardcode the driver name in your
Class.forName statement, you might as well just use the driver directly
- much less fuss.
· What is the real difference between the run() and start() method of
Thread? [...]

If you call run directly, there is no point in using Thread. You don't
create actually thread. You just run the task to completion then carry
on with the calling thread. With Thread.start the task runs in parallel
with the calling thread.

Thread should never have implemented Runnable. You can prevent
Thread.start/run accidents by always using a subclass of thread that
detects the problem:

public final class SafeThread extends Thread {
/**
* Indicates whether {link Thread#run} has been invoked.
*/
private final java.util.concurrent.atomic.AtomicBoolean run =
new java.util.concurrent.atomic.AtomicBoolean();
public SafeThread(Runnable doRun) {
super(doRun);
}
// ... other constructors...
@Override
public final void run() {
if (Thread.currentThread() != this) {
throw new IllegalStateException(
thread.getState()==Thread.State.NEW ?
"use start() instead of run()" :
"run() called on started thread"
);
}

if (!run.compareAndSet(false, true)) {
throw new IllegalStateException(
"run() called recursively"
);
}

super.run();
}
}

(Disclaimer: Not compiled or tested.)

Tom Hawtin
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

· What is the actual difference between "import com.mysql.jdbc.driver"
and Class.forName("com.mysql.jdbc.driver") if in the end what they
both do is load the class ? Why is the second one used for Database
code?

import com.mysql.jdbc.Driver;

enables you to write Driver instead of import com.mysql.jdbc.Driver
in the code.

And since you never use that class in your code, then there are
no point.

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

loads the class into the JVM.

Which is important for JDBC because the driver when
loaded registers itself with the DriverManager, so when
you call DriverManager.getConnection with an URL starting
with jdbc:mysql: then DriverManager knows about a driver
than can handle it.
· What is the real difference between the run() and start() method of
Thread? I know that we have to overload the run method of Thread while
writing our own threads, but why call start() when we can just call
the run() method itself? Any special reason for going the way around
when the source code of start() in the end is something like:

You call the start method, it starts the thread and calls the run
method you have written.

Arne
 
M

Mark Space

Kai said:
Here the driver may miss in the class path.

Right. If I understand correctly, the import statement requires the
class to be present at compile time. The Class.forName() method does
not. Thus, you can have a ClassNotFoundException on the second because
there's no guarantee that the user loaded the jar/class for you, or set
the classpath correctly.

The second is also more "dynamic." In other words, you're loading the
MySQL JBDC driver above, but you could load any class, for any database.
Normally, you'd snarf the name of the driver out of a configuration
file somewhere, then load the name supplied. You don't normally
hardcode the name into your program. Thus, your program could work with
any database vendor, not just MySQL.

For example, Tomcat (a Java webserver) allows you to specify the driver
and database to use in a separate configuration file like this:

<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">


<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="javauser" password="javadude"
driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>
<Context/>

That's part of the server configuration, which is read on startup. I
don't know a lot of the details myself, but you can pick out the driver
name and the database from the Resource tag. It's pretty easy to
imagine that any driver could be used (if you found one you liked
better) or any database could be used too.

Config snarfed from here:
http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html
 
G

getsanjay.sharma

Thanks a lot my friends for your answers and sorry for the double
posting. It was a mistake. Also apologies to all those people who must
have got my mail by mistake, I am not very used to newsgroup postings.

The question hanging in my mind is why the function:

Class.forName("com.mysql.jdbc.Driver") works in a standalone setup(a
normal void main program) while the same line when using servlets
throws a "com.mysql.jdbc.Driver not found Exception"? I thought this
shouldn't happen since I am creating projects in Eclipse and importing
the required jar files in the same way as always.

Even the code:

com.mysql.jdbc.Driver d = new com.mysql.jdbc.Driver();
Connection conn = d.connect(url, properties_object);

throws a ClassDefNotFoundException. I guess I am against a blank wall.
It would be really appreciated if someone could shed light on this....

Thanks and regards.
 
L

Lew

Thanks a lot my friends for your answers and sorry for the double
posting. It was a mistake. Also apologies to all those people who must
have got my mail by mistake, I am not very used to newsgroup postings.

The question hanging in my mind is why the function:

Class.forName("com.mysql.jdbc.Driver") works in a standalone setup(a
normal void main program) while the same line when using servlets
throws a "com.mysql.jdbc.Driver not found Exception"? I thought this
shouldn't happen since I am creating projects in Eclipse and importing
the required jar files in the same way as always.

You don't import JARs. What are you actually doing?
Even the code:

com.mysql.jdbc.Driver d = new com.mysql.jdbc.Driver();
Connection conn = d.connect(url, properties_object);

throws a ClassDefNotFoundException. I guess I am against a blank wall.
It would be really appreciated if someone could shed light on this....

Either include the JAR as a library in the web-app project properties or store
in in the web/WEB-INF/lib/ folder.

You need to study how servlet containers find classes.
 
G

getsanjay.sharma

You don't import JARs. What are you actually doing?
I right click on the project properties, go to built options and add
external jar files to the project. While this procedure seems to work
for a normal java application (connect to DB, retrieve values, display
in console), it is for some reason not working with my web container.
I tried putting the jar file inside the lib directory of "myProject/
WEB-INF/lib/" and it still gives the same problem.
You need to study how servlet containers find classes.
I have previously worked with bridge type drivers in my project
(Oracle DB) and it worked fine. This is the first time I am using a
pure driver for building my web app. Maybe some different procedure is
required for pure drivers in a web scenario.

If someone could help me out, it would be really appreciated because I
am facing this kind of problem for the first time.

Thanks and regards,
Orochi.
 
C

chandan

I right click on the project properties, go to built options and add
external jar files to the project. While this procedure seems to work
for a normal java application (connect to DB, retrieve values, display
in console), it is for some reason not working with my web container.
I tried putting the jar file inside the lib directory of "myProject/
WEB-INF/lib/" and it still gives the same problem.


I have previously worked with bridge type drivers in my project
(Oracle DB) and it worked fine. This is the first time I am using a
pure driver for building my web app. Maybe some different procedure is
required for pure drivers in a web scenario.

If someone could help me out, it would be really appreciated because I
am facing this kind of problem for the first time.

Thanks and regards,
Orochi.

which version of tomcat are you using? 4.x or 5.x, if it is 5.x then
try once more by placing the mysql-connector.jar in the shared folder.

Chandan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top