1)Why we use Class.forName(“oracle.jdbc.driver.OracleDriver”) while connecting to a database?
https://stackoverflow.com/questions/20078586/why-we-use-class-forname-oracle-jdbc-driver-oracledriver-while-connecting-to
https://stackoverflow.com/questions/20078586/why-we-use-class-forname-oracle-jdbc-driver-oracledriver-while-connecting-to
- Sometimes it is required to load a class during the run time. i.e, any class can be loaded into the memory location dynamically while executing the java application. The
Class.forName
is used to load any given class (within double quotes as String) at run time. For example, when we use IDE, we see there will be a GUI builder which allows us to drag and drop the buttons, text fields, etc. This drag and drop mechanism internally requires certain classes to be loaded at run time. - In the Class.forName (
sun.jdbc.odbc.JdbcOdbcDriver
), the Class belongs to the packagejava.lang.Class
and theforName()
is a static method of thejava.lang.Class
. The JDBC Drivers (String) will be loaded into the class dynamically at run time and forName method contains static block which creates the Driver class object and register with theDriverManager
Service automatically. Since theforName
()
is static, we call it using the class name (Class).
No comments:
Post a Comment