Java load JDBC Driver three ways to compare

Source: Internet
Author: User
Tags stmt

Usually connect the database when the first to load the JDBC driver, this step actually there are three ways, their differences? Advantages and Disadvantages

Quick understanding of three loading methods

    1. Class.forName ("Com.mysql.jdbc.Driver");

    2. Drivermanager.registerdriver (New Com.mysql.jdbc.Driver ())

    3. System.setproperty ("Jdbc.drivers", "Com.mysql.jdbc.Driver");

Comments:

The second and third method of registration seems to be more straightforward and well understood.

However, the first and the third method can be removed from the JDBC driver to compile , the second method can not, it must have a JDBC driver can be compiled, so there is a lot of bad for our program, for the program to exchange the database will bring trouble, and, There is a static block of code in the driver class, as long as we execute the static block of code in the driver class and put the driven instance into an array list of drivers, We re-call the method Registerdrever is equivalent to putting a driver driver in the drivers list, which means that two drives are loaded , although this does not affect our program, but this is not necessary, but also affect the operation of the program.

The third one can be detached, but the method parameter setting is relatively complex, it can set multiple drivers, so the first method is generally used when loading a single driver.

The first method is to load the class into the Java virtual machine by class, and does not create an instance of the driver class. The advantage is the ability to compile without relying on a particular JDBC driver library, which reduces the dependency on the project code, and is easily transformed to read the JDBC configuration from the configuration file, allowing the database connection driver to be dynamically replaced at run time.

1, Class.forName ("Com.mysql.jdbc.Driver") most commonly used

1 Try{2Class.forName ("Com.mysql.jdbc.Driver");//Load Database driver3String url= "Jdbc:mysql://localhost:3306/databasename";//Database Connection sub-protocol4Connection conn=drivermanager.getconnection (URL, "username", "password");5Statement stmt=conn.createstatement ();6ResultSet rs=stmt.executequery ("SELECT * FROM tablename");7         while(Rs.next ()) {//keep pointing to the next record8System.out.println ("DeptNo:" +rs.getint (1));9System.out.println ("\tdeptname:" +rs.getstring (2));TenSystem.out.println ("\tloc:" +rs.getstring (3)); One }          A rs.close (); - stmt.close (); - conn.close (); the}Catch(ClassNotFoundException e) { -SYSTEM.OUT.PRINTLN ("The specified driver class could not be found!") "); -}Catch(SQLException e) { - e.printstacktrace (); +}

2, DriverManager, looks more intuitive, a way to register the corresponding DB JDBC driver, 3 at compile time need to import the corresponding Lib

1 Try{2        NewCom.mysql.jdbc.Driver ();//Create a Driver object, load the database driver3String url= "Jdbc:mysql://localhost:3306/databasename";//Database Connection sub-protocol4Connection conn=drivermanager.getconnection (URL, "username", "password");5Statement stmt=conn.createstatement ();6ResultSet rs=stmt.executequery ("SELECT * FROM tablename");7         while(Rs.next ()) {//keep pointing to the next record8System.out.println ("DeptNo:" +rs.getint (1));9System.out.println ("\tdeptname:" +rs.getstring (2));TenSystem.out.println ("\tloc:" +rs.getstring (3)); One }          A rs.close (); - stmt.close (); - conn.close (); the}Catch(SQLException e) { - e.printstacktrace (); -}

You can also register as follows:

Drivermanager.register (new com.mysql.jdbc.Driver ());
但是该方式注册了两次驱动,所以不建议使用。

3, through the system's properties set System.setproperty ("Jdbc.driver", "Com.mysql.jdbc.Driver");

1 Try{2System.setproperty ("Jdbc.driver", "Com.mysql.jdbc.Driver");//System Properties Specify database driver3String url= "Jdbc:mysql://localhost:3306/databasename";//Database Connection sub-protocol4Connection conn=drivermanager.getconnection (URL, "username", "password");5Statement stmt=conn.createstatement ();6ResultSet rs=stmt.executequery ("SELECT * FROM tablename");7         while(Rs.next ()) {//keep pointing to the next record8System.out.println ("DeptNo:" +rs.getint (1));9System.out.println ("\tdeptname:" +rs.getstring (2));TenSystem.out.println ("\tloc:" +rs.getstring (3)); One }          A rs.close (); - stmt.close (); - conn.close (); the}Catch(SQLException e) { - e.printstacktrace (); -}

Java load JDBC Driver three ways to compare

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.