For large projects of course we have the original basic framework, but for some new technical discussion, we still call Class.forName ("Com.mysql.jdbc.Driver") connected to the database for related testing
Upload a breakpoint with HTTP large file today-xproer.httpuploader5 when you publish it, it's a little bit forgotten, all of the documents are collated.
1 Public classHttpuploaderdb {2String M_dbdriver ="Com.mysql.jdbc.Driver"; 3String M_dburl ="Jdbc:mysql://127.0.0.1/xdb_files?useunicode=true&characterencoding=utf-8"; 4 5 /*6 * Parameters:7 * sc = this.getservletcontext ()8 * */9 PublicHttpuploaderdb (ServletContext SC)Ten { One } A - PublicConnection Getcon () - { theConnection con =NULL; - - Try - { +Class.forName ( This. M_dbdriver). newinstance ();//load the driver. -con = drivermanager.getconnection ( This. M_dburl,"Root","123456"); + } A Catch(SQLException e) at { - //TODO auto-generated Catch block - e.printstacktrace (); -}Catch(ClassNotFoundException e) { - //TODO auto-generated Catch block - e.printstacktrace (); in}Catch(instantiationexception e) { - //TODO auto-generated Catch block to e.printstacktrace (); +}Catch(illegalaccessexception e) { - //TODO auto-generated Catch block the e.printstacktrace (); * } $ returncon;Panax Notoginseng } -}
Class.forName ("Com.mysql.jdbc.Driver");
When using JDBC, we will naturally have to use the following statements: Java code
- Class.forName ("Com.mysql.jdbc.Driver");
- String url = "Jdbc:mysql://127.0.0.1/test?useunicode=true&characterencoding=utf-8";
- String user = "";
- String PSW = "";
- Connection con = drivermanager.getconnection (URL,USER,PSW);
Why say it is natural, because both on-line or book tutorials are examples of this, and the program is indeed normal operation, so we also have a good conscience to find gourd painting ladle down.
Must have this sentence? No, we can replace it with a sentence like this:
Java code
1Com.mysql.jdbc.Driver Driver =NewCom.mysql.jdbc.Driver (); 2 //or:3 //new Com.mysql.jdbc.Driver (); 4String URL ="Jdbc:mysql://127.0.0.1/test?useunicode=true&characterencoding=utf-8"; 5String user =""; 6String PSW =""; 7Connection con = drivermanager.getconnection (URL,USER,PSW);
As you might see, we just need to make sure that the driver class has been loaded into the JVM before calling DriverManager's Getconnection method, and that the class initialization is done. But how to realize this function is not fastidious. The above two methods can implement this function, so the program can run normally. Note that if we do the following, the program will not function properly, because this only causes the driver class to be loaded into the JVM, but does not perform the appropriate initialization.
Java code
1 NULL ; 2 // or: 3 New ClassLoader (); 4 Cl.loadclass ("com.mysql.jdbc.Driver"
We all know that JDBC is designed using bridge mode, DriverManager is the abstraction,java.sql.driver is implementor, Com.mysql.jdbc.Driver is a concrete implementation of implementor (refer to the description of the Gof bridge mode). Note that the previous driver is an interface and the latter is a class that implements the previous driver interface.
Bridge mode, abstraction (DriverManager) is to have a implementor (Driver) reference, but we use the process, and did not register the Driver object to DriverManager, Ah, What's going on here? The JDK document describes the driver in this sentence:
When a Driver class was loaded, it should create a instance of itself and register it with the DriverManager
Oh, it's com.mysql.jdbc.Driver. Automatically helps us complete this step after loading. The source code is this:
Java code
1 Package Com.mysql.jdbc2 3 Public classDriver extends Nonregisteringdriver implements Java.sql.Driver {4 //~ Static fields/initializers5 // --------------------------------------------- // 6 //Register ourselves with the DriverManager7 // 8 Static { 9 t ry {TenJava.sql.DriverManager.registerDriver (NewDriver ()); One}Catch(SQLException E) { A Throw NewRuntimeException ("Can ' t register driver!"); - } - } the //~ Constructors - // ----------------------------------------------------------- - /** - * Construct A new driver and register it with DriverManager + * - * @throws SQLException + * If a database error occurs. A */ at PublicDriver () throws SQLException { - //Required for Class.forName (). newinstance () - } -}