Three ways to classify the JDBC Registration driver: "Java Fundamentals" 2012-05-17 07:34 2558 people Read reviews (0) Favorites report JDBC database Javavectorurljar about driver package JDBC in the registration driver, first import the corresponding package, such as Mysql-connector-java-5.0.8-bin.jar. A driver package is a bridge between Java and a specific database, developed by a database vendor. Each database corresponds to a drive jar, and even each version of the database has its own version of the driver jar. About the Java.sqldrivermanger class Java driver management class. Manages a set of JDBC drivers. The Javax.sql.DataSource interface is something new in the JDBC 2.0 API that provides another way to connect to a data source. Using the DataSource object is the preferred method for connecting to the data source. See the Java documentation for a deeper understanding. Divermanager.class has an attribute drivers, which is actually a vector. Can add a lot of drivers in the list, when the DriverManager to fetch the connection, if drivers has a lot of drivers, it will drivers inside the various driver URL and create a connection simultaneous come in the URL one by one, encounter the corresponding URL, then establish a connection. Three ways to register the drive: (i) Drivermanager.registerdriver (New Com.mysql.jdbc.Driver ()); Will cause two of the same drivers in the DriverManager, and will be dependent on the specific driver class. Specifically, it is: 1, load the time to register the driver (see the third registration method), the time of the instantiation is registered again. So two times. 2, because the instantiation of the com.mysql.jdbc.Driver.class, resulting in the class must be imported (that is, to import the class), so that the specific driver generated a dependency. Extension code is not convenient. (ii) System.setproperty ("Jdbc.drivers", "Com.mysql.jdbc.Driver"); Set the registration driver through the system's properties if you want to register multiple drivers, then System.setproperty ("Jdbc.drivers", "Com.mysql.jdbc.Driver:com.oracle.jdbc.Driver"); Although there is no dependency on the specific driver classes, registration is not easy, so it is seldom used. (c) Class.forName("Com.mysql.jdbc.Driver"); (for further understanding of this code, refer to another article, "Loading mechanism from Class.forName () to Classes"), which is not dependent on the specific driver class (that is, no import Package). In fact, this is just the Com.mysql.jdbc.Driver.class this class loaded into, but the key is that in this class, there is a static block, as follows: static{try{ Java.sql.DriverManager.registerDriver (New Driver ()); }catch (SQLException e) {throw new RuntimeException ("Can ' t register driver!");}} Just because of this block of code, let the class in the load when the driver registered in the! Reference resources: http://kin111.blog.51cto.com/738881/168295http://selinazzx.iteye.com/blog/366287
Three ways to register the JDBC driver