JDBC Learning notes (2)

Source: Internet
Author: User

The previous blog briefly describes the simple knowledge of the JDBC connection, which is described in detail below. Mr. Li Yong has a good metaphor for JDBC connections:

The first load driver, is actually some class, is the jar package, to be loaded into the classpath inside. Actual programs, services and databases are not on the same machine. The first thing to do is to establish a network connection that you can use to access the database through the Java language. For example, two cities to transport goods between, but there is a river, so to build a bridge, this is to establish a connection, and the car transport goods, the car is statement. And the query statement is the goods, these goods sent after the data obtained is resultset. Once sent, the data will be emptied, the car destroyed, and the bridge removed.

One Registration driver.

You can register using the following six methods, but are generally not common, with the first one being used the most.

1, DriverManager. Registerdriver (New Oracle.jdbc.driver.OracleDriver ());

Here is the code for the registered driver:

The bottom line of the above picture has a drivers, which is defined as follows, which is a list that can put multiple drivers in.

When the driver is loaded, after you call drivermanager.getconnection (XXX), the driver list is driven by the URL to see which driver can connect to the database. If all drivers are unable to establish a connection then the error is.

2, System.setproperty ("Jdbc.driver", "Oracle.jdbc.driver.OracleDriver");

This approach also allows the virtual machine to load the driver. And you can load more than one at a time, for example:

System.setproperty ("Jdbc.driver", "Oracle.jdbc.driver.OracleDriver:com.mysql.jdbc.Driver");

3, Class.forName ("Oracle.jdbc.driver.OracleDriver");

This is the most common method, he just loaded the class into the virtual machine, but there is no construction instance. The driver is usually put into the driver list, but how is it implemented? When a class is loaded into a virtual machine, it is called the static code block of the class, which is the initialization of the class content. So look at the code inside the JDBC driver that implements the interface (this is MySQL): It can be seen that when a class is loaded into a virtual machine, the contents of the static code block are executed.

What is the best of the three methods above? The third, of course, because:

The first to second type is equivalent to registering two times, the third one is registered only once.

The first depends on the database driver, if not the driver, of course, can not be compiled. The second, even without a driver, can be compiled.

It is therefore recommended to use a third approach.

Two Establish connections

The general connection is established as follows:

Here's the main thing about URLs.

String url = "Jdbc:oracle:thin: @localhost: 1521:orcl"= "Scott"= "Scott"= Drivermanager.getconnection (URL, user, password);

JDBC is a protocol in which the last side of the URL orcl is a db instance. Each database has a different URL format, which summarizes the URL format for most of the databases:

Three other

Once the connection is established, the user can access the database, which is equivalent to building the bridge. But how to access, of course, the use of SQL statements such as queries, but how these statements to the database, of course, to use the car to transport, the car is to create a statement object, it can send the SQL statement to be sent, it will also transfer the results of the query and so on. But the question is, where is the result of data execution? Keep it on the bus? No, he'll give it to the ResultSet object, which is the structure of a two-dimensional table, and the user can use the values found in the ResultSet object to do a series of operations. Of course, the final release of resources, the order of release is also very important, first created after the release. Why should we release resources? Because a database has a lot of applications to operate, after using the database to close in time, or when more than a certain worth of time will lead to a large system load, resulting in downtime.

         //  3, create statement        Statement stmt = conn.createstatement ();         // 4, executes the statement, returns the result set        ResultSet rs = stmt.executequery (sql);         // 5, processing results         while (Rs.next ()) {                    }        //  6, close connection         rs.close ();       Stmt.close ();       Conn.close ();
The code is a simple demonstration, just to familiarize yourself with the process. This article is from the "JDBC Recipes a problem-solution approach" and the video of Mr. Li Yong, a biography of Intelligence podcast.

JDBC Learning notes (2)

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.