What does Class.forname method do?
2013-02-22 17:39
316 查看
What does Class.forname method do?
url:http://www.xyzws.com/Javafaq/what-does-classforname-method-do/17A call to Class.forName("X") causes the class named X to be dynamically loaded (at runtime). A call to forName("X") causes the class named X to be initialized (i.e., JVM
executes all its static block after class loading). Class.forName("X") returns the Class object associated with the "X" class. The returned Class object
is not an instance of the "x" class itself.
Class.forName("X") loads the class if it not already loaded. The JVM keeps track of all the classes that have been previously loaded. This method uses the classloader of
the class that invokes it. The "X" is the fully qualified name of the desired class.
For example,
package com.xyzws; class AClass { static { System.out.println("static block in AClass"); } } public class Program { public static void main(String[] args) { try { Class c = Class.forName("com.xyzws.AClass"); } catch (ClassNotFoundException e) { } } }
The output is
static block in AClass
Here is one example that uses returned Class to create an instance of AClass:
package com.xyzws; class AClass { public AClass() { System.out.println("AClass's Constructor"); } static { System.out.println("static block in AClass"); } } public class Program { public static void main(String[] args) { try { System.out.println("The first time calls forName:"); Class c = Class.forName("com.xyzws.AClass"); AClass a = (AClass)c.newInstance(); System.out.println("The second time calls forName:"); Class c1 = Class.forName("com.xyzws.AClass"); } catch (ClassNotFoundException e) { ... } catch (InstantiationException e) { ... } catch (IllegalAccessException e) { ... } } }
The output is
The first time calls forName:
static block in AClassAClass's Constructor
The second time calls forName:
//Calss has been loaded so there is not "static block in AClass" printing out
JDBC Driver Is a Good Example
You may have experience working with JDBC Drivers. For example, the classloader attempts to load and link the Driverclass in the "org.gjt.mm.mysql" package. If successful, the static initializer is called.
Class.forName("org.gjt.mm.mysql.Driver"); Connection con = DriverManager.getConnection(url,?myLogin", "myPassword");
Let's see why you need Class.forName() to load a driver into memory. All JDBC Drivers have a static block that registers itself with DriverManager and DriverManager has
static an initializer only.
The MySQL JDBC Driver has a static initializer looks like this:
static { try { java.sql.DriverManager.registerDriver(new Driver()); } catch (SQLException E) { throw new RuntimeException("Can't register driver!"); } }
JVM executes the static block and the Driver registers itself with the DriverManager.
You need a database connection to manipulate the database. In order to create the connection to the database, the DriverManager class has to know which database driver you want to use. It does that by iterating over
the array (internally a Vector) of drivers that have registered with it and calls the acceptsURL(url) method on each driver in the array, effectively asking the driver to tell it whether or not it can handle the
JDBC URL.
################################################################################################
http://onjava.com/onjava/2006/08/02/jjdbc-4-enhancements-in-java-se-6.html
Auto-Loading of JDBC Driver
In JDBC 4.0, we no longer need to explicitly load JDBC drivers usingClass.forName(). When the method
getConnectionis called, the
DriverManagerwill attempt to
locate a suitable driver from among the JDBC drivers that were loaded at initialization and those loaded explicitly using the same class loader as the current application.
The
DriverManagermethods
getConnectionand
getDrivershave been enhanced to support the Java SE Service Provider mechanism (SPM). According to SPM, a service is
defined as a well-known set of interfaces and abstract classes, and a service provider is a specific implementation of a service. It also specifies that the service provider configuration files are stored in the
META-INF/servicesdirectory. JDBC
4.0 drivers must include the file
META-INF/services/java.sql.Driver. This file contains the name of the JDBC driver's implementation of
java.sql.Driver. For example, to load the JDBC driver to connect to a Apache Derby database, the
META-INF/services/java.sql.Driverfile
would contain the following entry:
org.apache.derby.jdbc.EmbeddedDriver
Let's take a quick look at how we can use this new feature to load a JDBC driver manager. The following listing shows the sample code that we typically use to load the JDBC driver. Let's assume that we
need to connect to an Apache Derby database, since we will be using this in the sample application explained later in the article:
Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); Connection conn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
But in JDBC 4.0, we don't need the
Class.forName()line. We can simply call
getConnection()to get the database connection.
Note that this is for getting a database connection in stand-alone mode. If you are using some type of database connection pool to manage connections, then the code would be different.
相关文章推荐
- What does Class.forname method do?
- What does Class.forName(); do?
- java class.forname method.invoke
- java Class.forname与SelectMethod=Cursor
- JAVA反射机制,get.method()/ Class.forName(className)
- Class.forName Method invoke
- undefined method `model_name' for NilClass:Class
- What does dirname(__FILE__) and basename(dirname(__FILE__)) do?
- What does the CS_CLASSDC & CS_OWNDC class style do?
- Class.forName(),getDeclaredMethod,invoke的简单组合使用
- What does if __name__ == “__main__”: do?
- No known class method for selector 'setImage:andName:'错误分析.//删除.h与.m文件时的注意事项
- java下Class.forName的作用是什么,为什么要使用它
- Class.forName(String )怎么用
- What does it mean to “delegate to a sister class” via virtual inheritance?
- forName、loadClass、NewInstance、new
- Class.forName 介绍
- 理解class.forName
- 使用JDBC时Class.forName()的作用
- Fixing the security exception : "class /"" + packageName + "/" does not match trust level of other classes in the same package"