This blog is to record how to use JDBC to operate the general process of the database.
The JDBC operation steps are as follows:
1. Registration Driver
2. Establish a connection
3. Creating statement objects
4. Execute the statement
5. Working with result sets
6. Close Resources
Look at the following code:
Package Base;import Java.sql.drivermanager;import Java.sql.resultset;import java.sql.sqlexception;import Com.mysql.jdbc.connection;import Com.mysql.jdbc.statement;public class Basedriver {public static void Test () throws sqlexception{//Registered driver Drivermanager.registerdriver (new Com.mysql.jdbc.Driver ());//Establish connection Connection conn = (Connection) Drivermanager.getconnection ("", "root", "password01!"); /Create statement Statement STM = (Statement) conn.createstatement ();//execute statement, get result resultSet ResultSet = Stm.executequery ("SELECT * From T_user ");//traverse result set while (Resultset.next ()) {System.out.println (resultset.getobject (0) +" \ T "+resultset.getobject (1) + "\ T" +resultset.getobject (2) + "\ T" +resultset.getobject (3)); Release resources Resultset.close (); Stm.close (); Conn.close ();}}
1. Registration Driver
There are two ways to register a drive:
A.drivermanager to register
Drivermanager.registerdriver (New Com.mysql.jdbc.Driver ());
B. Registering with a class
Class.forName ("Com.mysql.jdbc.Driver");
What exactly did the registration-driven process do? Listen to me slowly:
The A-mode registration driver mainly does the following work:
First, let's take a look at the source code of DriverManager:
public class DriverManager {//List of registered JDBC drivers private final static Copyonwritearraylist<driveri nfo> registereddrivers = new copyonwritearraylist<> (); private static volatile int logintimeout = 0; private static volatile java.io.PrintWriter logwriter = null; private static volatile java.io.PrintStream LogStream = null; Used in println () to synchronize LogWriter private final static Object Logsync = new Object (); }
The above is a partial code of the DriverManager class that is compiled by decompile
Private final static copyonwritearraylist<driverinfo> registereddrivers = new copyonwritearraylist<> (); Register the driver in a list. With the driver package of the database, after calling getconnection, it will go to one of the driver registration lists to see if the connection can be established, and if not found at the end, an exception will be thrown.
B. How does the registration drive work:
Also, let's take a look at the source code:
/** * returns the {@code Class} object associated with the class or * interface with the given string name. invoking this method is * equivalent to: * * < blockquote> * {@code class.forname (className, true, Currentloader)} * </blockquote> * * where {@code currentLoader} denotes the defining class loader of * the current class. * * <p> for example, the following code fragment returns the * runtime {@code class} descriptor for the class named * {@code java.lang.Thread}: * * <blockquote> * {@code class t = class.forname ("Java.lang.Thread")} * </blockquote> * <p> * a call to {@code forname ("X")} causes the class named * {@code X} to be initialized. * * @param className the fully qualified name of the desired class. * @return the {@code class} object for the class with the * specified name. * @exception LinkageError if the linkage fails * @exception exceptionininitializererror if the initialization provoked * by this method fails * @ exception classnotfoundexception if the class cannot be located */ @CallerSensitive public static Class<?> forname (String classname) throws classnotfoundexception { RETURN&NBSP;FORNAME0 (classname, true, classloader.getclassloader ( Reflection.getcallerclass ())); }
According to the above anti-compiled class source code heavy forname source code, is also loaded in a driver list, by traversing the driver list to detect whether there is a matching target driver registration, if not, throw an exception.
Difference:
Drivermanager.registerdriver (New Com.mysql.jdbc.Driver ()); A static code initialization block is called when the JVM loads the class, new Com.mysql.jdbc.Driver () A driver instance is also instantiated, so this method will appear in the driver registration list with two identical MySQL driver registrations.
Class.forName ("Com.mysql.jdbc.Driver"); Because the forname parameter is a string type, only static code blocks are called when the class is loaded to create an instance that drives only one MySQL driver registry entry in the registry.
This article is from the "Love Coffee" blog, please be sure to keep this source http://4837471.blog.51cto.com/4827471/1589423
JDBC Registration Process Analysis