have been using the database drive jar package, but did not look carefully. Only know Class.forName (); Go to load driver class.
Today I accidentally think of Class.forName (); not just load classes, does the class referenced in the jar package not be loaded directly when the virtual machine is started?
And then wrote a bit of code to do the test:
1 Packagecom.mariadb.test;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 ImportJava.sql.ResultSet;6 Importjava.sql.SQLException;7 Importjava.sql.Statement;8 //import Org.mariadb.jdbc.Driver;9 Ten Public classTest { One Public Static voidMain (string[] args)throwsClassNotFoundException, A SQLException { - //class.forname ("Org.mariadb.jdbc.Driver"); //there's a file in the new version of the driver package that replaces the phrase . - theConnection conn =DriverManager -. getconnection ("JDBC:MARIADB://LOCALHOST:3306/TEST?USER=ROOT&PASSWORD=BDQN"); - -Statement Statement =conn.createstatement (); + -ResultSet rs = statement.executequery ("select * from ' tb1 '"); + A while(Rs.next ()) { atSystem.out.println (Rs.getint (1) + "T" + rs.getstring (2)); - } - } -}
Then the test result is that even if class.forname () is not used, the program can still read the data in the database, so it is assumed that the classes in the jar package will be loaded.
But it's not quite right, if I have a project referencing a lot of jar package is not very slow to start.
Then ask someone else, and say that the class in the jar package is loaded only when it is used.
But how do you explain the problem of executing static blocks of code in the driver class in the driver jar package?
Finally, the explanation for the phenomenon of driving the jar package is found.
So if you use JDBC version 4.0 or more, a similar file exists, Class.forName (); This step can be omitted. The contents of the file are the full names of the driver classes in this jar.
PS: This article for Bo Master original, without permission not reproduced.
A small discovery about the Java database driver package