by calling the method Class.forName. This explicitly loads the driver class. Since it does not depend on any external setup, this way of loading a driver is the recommended one for using the DriverManager framework.
If a has been written so that loading it causes an instance to be created and also calls DriverManager.registerDriver with that instance as the parameter (as it should do), then it is in the DriverManager's list of drivers and available for creating a connection.
It may sometimes be the case that more than one JDBC driver is capable of connecting to a given URL. For example, when connecting to a given remote database, it might be possible to use a JDBC-ODBC bridge driver, a JDBC-to-generic-network-protocol driver, or a driver supplied by the database vendor. In such cases, the order in which the drivers are tested is significant because the DriverManager will use the first driver it finds that can successfully connect to the given URL.
First the DriverManager tries to use each driver in the order it was registered. (The drivers listed in jdbc.drivers are always registered first.) It will skip any drivers that are untrusted code unless they have been loaded from the same source as the code that is trying to open the connection.
It tests the drivers by calling the method Driver.connect on each one in turn, passing them the URL that the user originally passed to the method DriverManager.getConnection. The first driver that recognizes the URL makes the connection.
Shailesh
Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
The method forName() is static method of Class,when call it, it can load class from your classpath,
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver") // only load class you can load many drivers ,but which db u connecting , look follow code String url="jdbc:microsoft:sqlserver://10.41.30.148:1433;DatabaseName=caf_pm_pmp_db"; Connection conn= DriverManager.getConnection(url,"sa",""); // decide which db you connecting [ June 26, 2006: Message edited by: Han Dian ]
The direct way to register the driver is, DriverManager.registerDriver(new OracleDriver);//just an example
What Class.forName("Driver") does is, It will register the driver by calling the same method DriverManager.registerDriver(new OracleDriver);
See the below example,
Class SampleDriver{ SampleDriver(){} static{ DriverManager.registerDriver(new SampleDriver()); } }
Note: As you all know, The static block will be called when we load a class. That means when we put Class.forName("SampleDriver") the static block will be called where the DriverManager.registerDriver(new SampleDriver()) is invoked.
Hi friends, First thing is that Class.forName("class_name"); loads the class in Java Virtual Machine. Here where it loads is important. in your case it loads the driver_class; second thing is that if you load multiple drivers i.e driver1,driver2 etc. it doesnot matter.It uses only one. Because one more important thing is that while getting connection which url you use. String url="JDBC:ODBC:MYDSN"; e.g.Connection con=DriverManager.getConnection(url,"user","password");
MyDriver driver = new MyDriver() ( OR MyDriver.getInstance() );
DriverManager.registerDriver(driver);
Not recommended over Class.forName("MyDriver") if they do the same thing?
It's preferred in order to keep the code flexible. Keeping the driver class name, DB URL, DB username and DB password outside of the code makes it much easier to change those, which come shandy during development (when you may want to use a local DB like MYSQL or Postgres instead of SQLServer or Oracle).
Class.forName("class name");
is generlly used to load a class specified.
and then internally this method call the method a static block of specified class as follows -
santosh kimothi wrote:Class.forName("class name");
is generlly used to load a class specified.
and then internally this method call the method a static block of specified class as follows -
DriverManager.registerDriver(new Driver());
then it creates object of the specified Driver.
1. Driver myDriver = new MyDriver();
2. DriverManager.registerDriver(myDriver);
will register the same driver twice by creating two instances of the same MyDriver, because while doing ne MyDriver() internally it will load the MyDriver class that's where it encounters static block
A. static {
DriverManager.registerDriver(myDriver);
}
there-after it comes to your piece of code i.e. (1) & (2) above whereby you are just trying to create another instance & register the second that instance with DriverManager. So, now we have two instances of the same driver registered & as code inside static block in (A) above registers it first while loading the MyDriver class so DriverManager will always pick it up for connectivity & your 'myDriver" in (1) & (2) are never invoked.
So, to conclude doing "new MyDriver()" doesn't add any value instead it registers duplicate driver with DriverManager which will never be used. That is why preferred approach laid down is --
Class.forName("org.jdbc.myDriver");
May I know does loads the driver class means import the driver class using import statement?
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
4
posted
0
Fei Wong wrote:May I know does loads the driver class means import the driver class using import statement?
No, it doesn't
An import statement tells the compiler which class to look for. Class#forName() instructs the Class<?> class to find a class-loader and load that particular Class<?> object into the memory used by the JVM. A fuller description about how that registers connections and bridges and drivers has already been given in this thread.