Re: Confusing facts...
Kai Schwebke wrote:
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.
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"
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: