Java – what class What does the forname () method do for JDBC?

See the English answer > what is the purpose of 'class forName(“MY_JDBC_DRIVER”)’? 4

Class.forName("com.MysqL.jdbc.Driver").newInstance();

After JDBC 4.0, you do not need to call this method But people always use this method after JDBC version 4.0 My question is, if I use JDBC 4.0, why do I need to use this method? Class. What does the forname () method do for JDBC 4.0 and beyond?

This is my sample code I only use MySQL connector Jar is added to my library and works well when I run this code

Connection con = DriverManager.getConnection("jdbc:MysqL://localhost:3306","root","root");
Statement stmt = con.createStatement();
System.out.println("Created DB Connection....");

Solution

It gets the class object represented by the given FQN If it has not been loaded before, it will also load the class This has the side effect of initializing static class variables and running any static blocks

With the latest JDBC version, you no longer need class Forname() to load the driver, it is required to use an older driver version

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>