what exactly does this do Class.forName(“com.mysql.jdbc.Driver”).newInstance();

后端 未结 5 1824
孤独总比滥情好
孤独总比滥情好 2020-12-30 02:57

While connecting to MySQL database I do following steps

Connection con = null;
Resultset rs = null;
Statement st = null;
Class.forName(\"com.mysql.jdbc.Drive         


        
5条回答
  •  渐次进展
    2020-12-30 03:34

    The Class class is located in the java.lang package, so it is distributed with java, and imported automatically into every class.

    What the forName() method does, is just return the Class object for the paramater that was loaded by the class loader. The newInstance() method then returns a new instance of the class.

    So then what happens is you call Class.forName(...) it returns com.mysql.jdbc.Driver.class. You then call newInstance() on that class which returns an instance of the class, whith no paramaters, so it's basically calling new com.mysql.jdbc.Driver();.

提交回复
热议问题