Why class. forname ("com. MySQL. JDBC. Driver ")

Source: Internet
Author: User

When using traditional JDBC to connect to a database, you always need this sentence (taking MYSQL as an example ):

Class. forname ("com. MySQL. JDBC. Driver ");

I did not go into it in the past. I just read the examples on the Internet and actually ran it, so I was too lazy to manage the internal principles. However, it is clear that we have registered a MySQL JDBC driver with drivermanage.

But why is it not very elegant to use class. forname?

This version of class. forname ("com. MySQL. JDBC. Driver"). newinstance () is also circulating on the Internet.

After experiment, I found that com. MySQL. JDBC. Driver =NewCom. MySQL. JDBC. Driver () is also acceptable, but only declare

Com. MySQL. JDBC. Driver driver = NULL, but not instantiated.

So is the driver registration performed during class initialization or during class object initialization? I don't know. The best way is to look at the source code:

 

Package com. MySQL. JDBC;

Import java. SQL. drivermanager;
Import java. SQL. sqlexception;

// Referenced classes of package com. MySQL. JDBC:
// Nonregisteringdriver

Public class driver extends nonregisteringdriver
{

Public Driver ()
Throws sqlexception
{
}

Static
{
Try
{
Drivermanager. registerdriver (New Driver ());
}
Catch (sqlexception E)
{
Throw new runtimeexception ("can't register driver! ");
}
}
}

There is no secret before the source code. com. MySQL. JDBC. Driver is very simple. It is the red sentence. It seems that it is the initialization in the static block of the class.

So we only need to figure out the timing of static block execution in Java.

Let's do an experiment: Package com. javaye;

Class {
Static {
System. Out. println ("Class A loaded ");
}
Public (){
System. Out. println ("Create a instance of ");
}
}


Public class main {
 
Public static void main (string [] ARGs) throws exception {
Class. forname ("com. javaye. ");
}
}
 

Printed result: "Class A loaded"
 

This indicates that class. forname can execute the code in the static block of the class.

Then, we modify it to the following:

Package com. javaye;

Class {
Static {
System. Out. println ("Class A loaded ");
}
Public (){
System. Out. println ("Create a instance of ");
}
}
Public class main {
 
Public static void main (string [] ARGs) throws exception {
Com. javaye. a A = NULL;

}
}
 

If nothing is printed, it means that only variables of a class are declared and static block is not executed.

Modify the settings as follows:

Package com. javaye;

Class {
Static {
System. Out. println ("Class A loaded ");
}
Public (){
System. Out. println ("Create a instance of ");
}
}
Public class main {
 
Public static void main (string [] ARGs) throws exception {
Com. javaye. a A = new COM. javaye. ();
Com. javaye. A a2 = new COM. javaye. ();
}
}

Output result:

Class A loaded
Create a instance of
Create a instance of
 

This indicates that if a class is instantiated, the static block will be executed only once.

If you are still interested, you can also explore the driver registration mechanism:

Public static synchronized void registerdriver (Java. SQL. Driver driver)
Throws sqlexception {
If (! Initialized ){
Initialize ();
}

Driverinfo di = new driverinfo ();
Di. Driver = driver;
Di. driverclass = driver. getclass ();
Di. driverclassname = Di. driverclass. getname ();
Drivers. addelement (DI); // drivers is a vector that stores the registered driver.
Println ("registerdriver:" + DI );
 

}

Getconnection is to pick out a suitable database driver from drivers:

For (INT I = 0; I <drivers. Size (); I ++ ){
Driverinfo di = (driverinfo) drivers. elementat (I );

// If the caller does not have permission to load the driver then
// Skip it.
If (getcallerclass (callercl, Di. driverclassname )! = Di. driverclass ){
Println ("Skipping:" + DI );
Continue;
}
Try {
Println ("trying" + DI );
Connection result = Di. Driver. Connect (URL, Info );
If (result! = NULL ){
// Success!
Println ("getconnection returning" + DI );
Return (result );
}
} Catch (sqlexception ex ){
If (reason = NULL ){
Reason = ex;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.