Java DriverManager Simple Introduction __java

Source: Internet
Author: User
Tags postgresql
Have a good laugh .

Eros and The Matchmaker
A: "Do you know why there are more divorces in Western countries than in China?" ”
B: "This is not easy, because the Western Cupid Cupid is a doll, and China is matchmaker, the experience is certainly much richer." The video tutorial

Hello everyone, I recorded the video "Java Elegant programming Way" has been published in the CSDN College, interested students can buy to watch, I believe you will be harvested a lot of knowledge. Thank you for your support ...

Video address: http://edu.csdn.net/lecturer/994 asked Questions

DriverManager class of simple learning ... solve the problem

DriverManager the basic services of a set of JDBC drivers in the java.sql package

Package com.evada.de;
Import java.sql.Connection;
Import Java.sql.Driver;
Import Java.sql.DriverManager;

Import java.util.Enumeration;
 /** * Manages a single, reusable connection with a static factory approach * Created by Ay on 2016/5/1.
        * * public class connutils{Private Connutils () {} private static Connection con;//Create a connection to the database in a static code block static{try{
        PostgreSQL driver name Class.forName ("Org.postgresql.Driver");
        Database connection path String URL = "Jdbc:postgresql://127.0.0.1:5432/postgres";
    Username and password con = drivermanager.getconnection (URL, "Postgres", "888888");
    }catch (Exception e) {throw new RuntimeException (E.getmessage (), E);

}//Using a static Method-static factory method, returns the Connection instance public static Connection Getcon () {return con;} Main function test with public static void main (string[] args) {//Get all database-driven enumeration<driver> driverenum = Driverman
    Ager.getdrivers ();
    Print out all drive information while (Driverenum.hasmoreelements ()) {System.out.println (driverenum.nextelement ());
 }
}
}

Run Result:

org.mariadb.jdbc.driver@7cef4e59
com.alibaba.druid.proxy.druiddriver@3ada9e37
com.alibaba.druid.mock.mockdriver@4ec6a292
Org.postgresql.driver@71c7db30

EXPLANATION, because our project package has the following image of the database driver, so will print the following information

the difference between the two:
Drivermanager.registerdriver (New Driver ());
Class.forName ("Com.mysql.jdbc.Driver");

Org.postgresql.Driver class source code as follows, you can see PostgreSQL is through a single case form, new a driver driver, that is, in the connection to the database is only a new one driver

static {
        Sharedtimer = new Sharedtimer (logger);

        try {
            //Register register
            ();
        } catch (SQLException var1) {
            throw new Exceptionininitializererror (VAR1);
        }

        protocols = new string[]{"jdbc", "PostgreSQL"};
    }

    public static void register () throws SQLException {
        if (isregistered ()) {
            throw new IllegalStateException (" Driver is already registered ...);
        } else {
            Driver registereddriver = new Driver ();
            Drivermanager.registerdriver (registereddriver);
            Registereddriver = Registereddriver;
        }
    

Then look at the source code of DriverManager, it will driver packaging into the Driverinfo,

Public
static synchronized void Registerdriver (Java.sql.Driver Driver,
            driveraction da)
        throws SQLException {

        /* Register The driver if it has not already been added to our list */
        if (driver!= null) {
            //Will Drive Wrap to Driverinfo
            registereddrivers.addifabsent (New Driverinfo (Driver, DA));
            Compatibility with the original DriverManager
            throw new NullPointerException ();
        }

        println ("Registerdriver:" + driver);

    }

Class.forName ("Com.mysql.jdbc.Driver");
Conclusion: Drivermanager.registerdriver (New Driver ()); More new one Driver waste; Class.forName ("Com.mysql.jdbc.Driver"); Single case form, more recommended ... Reading Comprehension

from "A Liter of Tears" everyone's mind, I accept and put in the bottom of my heart but mom, I do not want a very comfortable place I was thinking about how to live in the future now I can only accept the care of everyone
Stand also unstable, said not to say even though, this is my own body, I can not give up. I do not want to give up with their feet to walk homesick, phone mom, can't sleep, afraid to close their eyes to call home, dialed several times, I want to listen to Mother's voice but always press bad, save me, mother ~~~~~
No, I can do things, no more. other

If there is a little happiness to you, let happiness continue to pass, welcome reprint, point praise, top, welcome to leave valuable comments, thank you for your support.

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.