Write a JDBC driver by yourself to monitor SQL statements (1)

Source: Internet
Author: User

Author is positive date 2011.01.26 21:30:00 reprint please indicate the source is blog http://www.2009fly.com

In fact, here we mainly use the working principle of JDBC, and there is not much technical content. Through this article, we hope to give you some inspiration and deepen your understanding of the working mechanism of JDBC, at the same time, I also want to learn about the proxy design mode. Note: Write it to beginners. If you are an expert in this field, please smile!

The first thing to mention is Java. SQL. drivermanager, which is the source of the load driver. We are not developing a JDBC driver for a database by ourselves here, so we do not need any knowledge about JNI. Drivermanager: the driver is managed according to the name. All drivers need to be registered with it first. drivermanager will set the ID of all drivers, that is, the driver class name is saved to its "Registry" in the memory. The Registry here is not the Registry in windows, but just a container or data structure, you can track, debug, and check what data structure is?

A driver is a driver, and its class's fully qualified name is its ID. It must be registered in drivermanager in advance. When we explicitly load the JDBC driver using class. forname {...} In the static method, call the static method registerdriver (driver Driver) of drivermanager to register yourself. Of course, if the Registry already has its own identity, this registration will be ignored. You know, in jdbc4.0, the driver name needs to be configured to META-INF/services/Java. SQL. driver file, so we do not need to display the call class. forname () is a sample, just remind me.

Stop first. Let's write down our own driver class and continue to introduce others for the moment, so as not to forget what we just said. Because our driver is only a wrapper, that is, a shadow, and does not do the actual work, it just does some decoration. The real work is still done by the real driver. Therefore, our driver is called mydriver. It must first implement the java. SQL. Driver Interface and include a real JDBC driver used to connect to a database called realdriver. Therefore, in static {...} In addition to registering our own driver, we also need to register the real realdriver, but we need to ensure that we register first, so that drivermanager will first find us in the "Registry, otherwise, we will not be able to get the expected results. Don't forget, we are going to print the SQL statement into the format we need. Let's call it "injection" because it sounds cool! Well, if our database is dbmaker (SYSCOM product), we should first put its JDBC driver jdbc30.jar under java. Lib. classpath:

Package com.2009fly;

Import... ... // Add it by yourself

Public class mydriver implements driver {

Private driver mydriver;

Private driver realdriver; // The driver that actually works

Static {

Mydriver = Class. forname ("com.2009fly. mydriver"). newinstance ();

Drivermanager. registerdriver (mydriver );

Realdriver = Class. forname ("dbmaker. SQL. jdbcodbcdriver"). newinstance ();

Drivermanager. registerdriver (realdriver );

}

............

}

Now, download all our mydriver and real realdriver and register them in the drivermanager management center. First, we all know that to operate the database, we must first obtain a connection object, and the connection is provided by the driver through the getconnection () method. In order to operate the database, we need to implement our own connection object. Of course, we still need a real connection, so we need a real realconnection member in myconnection, realconnection is used to complete real work, and our myconnection only needs to complete some operations before realconnection.

Package com.2009fly;

Import... ... // Add it by yourself

Public class myconnection implements connection {

Private connection realconnection;

Public myconnection (connection conn ){

Realconnection = conn;

}

... ...

}

As we mentioned earlier, the connection needs to be obtained by the driver. Now we need to add the getconnection () method to our com.2009fly. mydriver:

@ Override

Public connection getconnection (string URL, properties info ){

Connection conn = NULL;

If (realdriver. acceptsurl (URL )){

Conn = new myconnection (realdriver. Connect (URL, Info );

} Else {

Conn = NULL;

}

Return conn;

}

From the code above, we can see that our driver has nothing to do. What we really work on is realdriver. Now, we just have a shell or a shadow. Now we get the connection. In the same way, we construct our own statement and preparedstatemen objects to print our SQL statements. Tip: Before executing a statement, such as calling the execute () method of the statement object, you can print the executed SQL statement, in this way, our SQL statement printing method is implemented. At the same time, you can add a small function to record the time of each SQL statement execution, so that you can analyze the SQL Execution efficiency and avoid writing inefficient SQL statements.

Haha... ... Today is here, if you still do not understand the place, can email to me: zzcwfp@gmail.com.

----------------------------------

Copyright, welcome to reprint, please indicate the source before reprinting: zhengzheng blog http://www.2009fly.com

Respect others' labor achievements, that is, respect yourself!

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.