Why we use Class.forName(“oracle.jdbc.driver.OracleDriver”) while connecting to a database?

前端 未结 7 805
轮回少年
轮回少年 2020-11-30 01:41

What is the actual use of Class.forName(\"oracle.jdbc.driver.OracleDriver\") while connecting to a database? Why cant we just import the same class, instead why

7条回答
  •  失恋的感觉
    2020-11-30 02:27

    When we want to execute static block of a class, without creating its object then we can use class.forName(). Most of the work that Driver class do, exists in its static block.

    Now what we require in our JDBC connectivity is to get the driver registered with DriverManager and to obtain connections with it, so this can be achieved simply by getting static block executed and there is no requirement to create object of that class. This approach will give a better performance.

提交回复
热议问题