A small discovery about the Java database driver package

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.