The evolution of JDBC

Source: Internet
Author: User

1. Evolution of the way data is stored
① string
② Array
③ file
④ Database
Where ① and ② are present in memory and end with the termination of the program and are not persisted to the local disk
③ and ④ store data on a local disk, and ④ can manage the data to select different levels of database based on different scenarios.
Evolution of the 2.JDBC
Origin:
Java program to read and write data on the disk, the major database vendors to provide a corresponding driver, Java provides the appropriate interface to connect.
Evolution 1: Object-oriented Programming

    @Test     Public void TESTORCLJDBC1()throwsSQLException {Driver Driver =NULL;//Get Driver object through the OOP ' s polymorphicDriver =NewOracledriver (); String URL ="Jdbc:oracle:thin: @localhost: 1521:orcl"; Properties info =NewProperties (); Info.setproperty ("User","Scott"); Info.setproperty ("Password","Tiger");        Connection conn = driver.connect (URL, info);        SYSTEM.OUT.PRINTLN (conn);    Conn.close (); }

An object-oriented polymorphic is applied to create a driver instance.
Cons: Not flexible enough, reusability is too poor, just for the Oracle database, but also need a properties object parameters. The equivalent of a skip interface (JDBC) is connected directly to a certain data.

Evolution 2: interface-oriented Programming 1

    @Test     Public void TESTJDBC2()throwsException {//Oracle        //String drivername = "Oracle.jdbc.driver.OracleDriver";        //String URL = "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL";        //Properties info = new properties ();        //Info.setproperty ("User", "Scott");        //Info.setproperty ("Password", "Tiger");        //MySQLString drivername ="Com.mysql.jdbc.Driver"; String URL ="Jdbc:mysql://localhost:3306/test"; Properties info =NewProperties (); Info.setproperty ("User","Root"); Info.setproperty ("Password","123456");//Get the Driver object through reflectClass<?> clazz = Class.forName (drivername); Driver Driver = (Driver) clazz.newinstance ();//Get connectionConnection conn = driver.connect (URL, info);        SYSTEM.OUT.PRINTLN (conn);    Conn.close (); }

The driver object is dynamically determined at run time by reflection, which is an interface-oriented programming concept (the string can be arbitrarily changed, but the object cannot be changed casually after it is generated). Attention to a lot of experience

Evolution 3: interface-oriented Programming 2
The problem that existed in the original 1, 2 did not solve all, here we first solve one, we want to connect when the parameter type does not have the properties type, the string is the best. Through the API we discovered an implementation class DriverManager for driver, which provides a static method
getconnection (string url, string user, string password), three parameters are of type string, and all we need is it. But it also has a description,
"Attempts to establish a connection to the given database URL. The DriverManager attempts to select a appropriate driver from the set of registered JDBC drivers. " Try to establish a connection for a given database URL, and try to select an available driver from the registered JDBC driver , so here we need to register the database driver. It also provides a static method Registerdriver (Driver Driver) for registering the driver. That's what we're going to do.

//Remove Properties    @Test     Public void TESTJDBC3()throwsException {String Driverclass ="Com.mysql.jdbc.Driver"; String URL ="Jdbc:mysql://localhost:3306/test"; String user ="Root"; String Password ="123456";//Get driverClass<?> clazz = Class.forName (Driverclass); Driver Driver = (Driver) clazz.newinstance ();//Register driverDrivermanager.registerdriver (driver);//Get connectionConnection conn = drivermanager.getconnection (URL, user, password);        SYSTEM.OUT.PRINTLN (conn);    Conn.close (); }

Did you find that the program looks much prettier than the Origin 1? Of course, this is not the end, and then evolve.
Evolution 4: interface-oriented Programming 3
Because MySQL is open source, we can look at its source code, here we find it through the driver, there is such a piece of code:

// ~ Static fields/initializers    // ---------------------------------------------    //    // Register ourselves with the DriverManager    //    static {        try {            java.sql.DriverManager.registerDriver(new Driver());        catch (SQLException E) {            thrownew RuntimeException("Can‘t register driver!");        }    }

See this code, is not an instant feel good happy? It has been written in the static code block to register the driver, we only need to load the class to be loaded, and class loading need I say more?

@Test    publicvoidtestJdbc4throws Exception {        "com.mysql.jdbc.Driver";        "jdbc:mysql://localhost:3306/test";        "root";        "123456";        Class.forName(driverClass);        Connection conn = DriverManager.getConnection(url, user, password);        System.out.println(conn);        conn.close();    }

There's a bit of a question here, and I think about it, Class.forName (Diverclass) Gets a Class object, and it loads a new Driver () in the static code block, which I understand here, combined with "and try to Select an available driver in the registered JDBC driver "is it possible to assume that it automatically converts a class object into a driver object?" This is my understanding, but also hope to read this blog friend more think, know its principle can give me comments, thank you.
Don't think it's over here, but you have to go on.
Evolution 5: interface-oriented Programming 4
Every time you write so many connection string is not too troublesome, with a configuration file read it, how convenient, the exception has been thrown, we give treatment once

@Test     Public void TestJdbc5() {Connection conn =NULL;Try{//read the Driverclass, url, user, password from properties file ...Properties proper =NewProperties (); Proper.load (NewFileInputStream ("Jdbc.properties")); String Driverclass = Proper.getproperty ("Driverclass"); String URL = proper.getproperty ("url"); String user = Proper.getproperty ("User"); String Password = proper.getproperty ("Password");//Load driverClass.forName (Driverclass);//get Connectionconn = drivermanager.getconnection (URL, user, password); }Catch(ClassNotFoundException e)        {E.printstacktrace (); }Catch(IOException e)        {E.printstacktrace (); }Catch(SQLException e)        {E.printstacktrace (); }finally{if(Conn! =NULL) {Try{Conn.close (); }Catch(SQLException e)                {E.printstacktrace (); }            }        }    }

It's over, isn't it? Well? is the code so much more? Easy, look at me. Write the code in a way that will be done in a few lines.

@Test    publicvoidtestJdbc6(){        null;        try {            conn = JDBCUtils.getConnection();        catch (Exception e) {            e.printStackTrace();        finally {            ifnull){                try {                    conn.close();                catch (SQLException e) {                    e.printStackTrace();                }            }        }    }
 Public StaticConnectiongetconnection()throwsexception{//read the Driverclass, url, user, password from properties file ...Properties proper =NewProperties (); Proper.load (NewFileInputStream ("Jdbc.properties")); String Driverclass = Proper.getproperty ("Driverclass"); String URL = proper.getproperty ("url"); String user = Proper.getproperty ("User"); String Password = proper.getproperty ("Password");//Load driverClass.forName (Driverclass);//get ConnectionConnection conn = drivermanager.getconnection (URL, user, password);returnConn }

OK, is it done? Didn't. Khan, not yet, what do you say?
This is just a JDBC connection, are we just going to connect? After the connection also has the data addition and deletion change to investigate. It's too late today, I have to go to bed, tomorrow after school to write it, sleepy.

Originally also prepared to javase part of the summary of the collection to write, but this blog with a great effort, and to be left until tomorrow or the day after. Sleep, everybody refueling!

The evolution of JDBC

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.