JDBC and jdbc connect to the database

Source: Internet
Author: User

JDBC and jdbc connect to the database

The following is a well-behaved Java code for connecting to the database:

       Class.forName("com.mysql.jdbc.Driver");        Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");        Statement st=conn.createStatement();        ResultSet rs=st.executeQuery("select * from student");while(rs.next()){            Object o1=rs.getObject(1);            Object o2=rs.getObject(2);            p(o1+","+o2);        }        rs.close();        st.close();        conn.close();

This is a complete set of Java code for connecting to the database, and everyone is familiar with it. I once thought that even if I had backed this code, I would connect to the database. I found myself very wrong.

In fact, I am only carrying other people's APIs, but I have not made any progress. Chairman Mao said in a word that he should despise the enemy in strategy, attach importance to the enemy in tactics, and despise the enemy in strategy. I am carrying APIs and it is useless, i'm proud of it. I pay attention to it in "tactics", and there is a truth in its existence. I have been skilled in it, but it only improves my development speed and has nothing to do with my growth, if I could only master it for years, I would be useless.

What I really want to learn is the idea behind it, the architecture design behind it, and how it achieves scalability? What is its architectural design ?.

 

Load driver

First, give the meaning of the hardware Driver. The Driver generally refers to the Device Driver, which is a special program that allows the computer to communicate with the Device. It is equivalent to a hardware interface. The operating system can control the work of the hardware device only through this interface. If the driver of a device fails to be correctly installed, it will not work properly.

Therefore, MySQL databases are compared to hardware devices, and Java applications need to communicate with them. MySQL database vendors must write "drivers" that meet the Java-defined specifications ", at that time, Java will call the driver program, and the two will work normally.

Java defines the java. SQL. Driver Interface, which is implemented by the Driver com. MySQL. jdbc. Driver provided by the mysql database vendor. In general, Java provides specifications, that is, interfaces, and MySQL database vendors provide specific implementation for MySQL. In this way, the system is scalable by taking advantage of the polymorphism of one of the three object-oriented features. If the customer asks to use the Oracle database, the system architecture will not change.

 

Understanding of static variables

Specific implementation of static code blocks in com. mysql. jdbc. Driver:

java.sql.DriverManager.registerDriver(new Driver());

When we get the connection, we can directly use DriverManager. getConnection (parameter) to get it. Why?

Because static variables are shared by all objects in the class, we can call DriverManager. getConnection (parameter) anywhere to obtain the previously registered Driver.

The so-called registration is simply to add the static member variable to the Driver management class. After registration, the Driver in the variable can be obtained by using the class name or object anywhere and then operated.

In summary, the global sharing of static variables is used here.

 

Java interface and MySQL implementation class





 

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.