Database Access Technology-JDBC

Source: Internet
Author: User
Tags odbc connection

Before learning about JDBC, we can review ODBC to better understand JDBC. The two relationships are not common. They implement the same function and provide support for application connection and database operations. Therefore, we should start with ODBC.
ODBCODBC (Open Database Connectivity) is an application interface that uses SQL. It is a series of specifications and database access APIs. Then the API + SQL can perform database operations. It does not rely on DBMS, that is, ODBC can connect to most databases in the same way. It includes application interfaces, drive managers, database drives, and data sources. The following figure shows the relationship between each part of ODBC:

JDBC previously said that the functions of JDBC and ODBC are the same, but the implementation is not the same. First, ODBC is based on the C ++ language. Therefore, it is difficult to communicate with Java because it is inconsistent with Java's Object-oriented thinking. Therefore, JDBC comes out, that is, JDBC is a JAVA-based database access API interface. The other content is basically the same as ODBC. As long as you understand ODBC, you can basically understand JDBC in terms of concept. What you need to do is to understand the interfaces with new faces again, and the functions of these interfaces are the same as those of ODBC, but there are slight differences in implementation. Let's take a look at the structure of JDBC:
The preceding structure shows the connection methods. JDBC provides different connection methods. Let's take a rough look at this, because this is mainly a matter of concern to the database vendors. We can just know it, and some do not quite understand it.
1,JDBC-ODBC Connection Bridge: This method is based on one ODBC. The above mentioned communication between java applications and ODBC is a little troublesome. However, ODBC is widely used as a standard for database access. Therefore, by calling the ODBC ing ODBC function, JDBC ensures that the databases that used ODBC can also be easily accessed.
2,Local API driver: Converts a JDBC call to a call to the client binary code library of the database interface. However, this interface library depends on the manufacturer, because we are not calling the JDBC interface implementation provided by the database vendor.
3,Pure Java local Protocol: Ing JDBC calls to network listening protocol calls of DBMS. the listener listens to requests and then performs related database operations. The listener is provided by the vendor.
Introduction to common interfacesDriverManagerWe don't need to know how to register the driver. We need to know how to call a method to load the database driver. That is, the Class. forName () method. To call this method, you must pass a String object containing the driver Class name as the real parameter. As follows:

    Class.forName("oracle.jdbc.driver.OracleDriver")
ConnectionAfter the driver is loaded, you need to call DriverManager to establish a connection with the database. getConnection () method. This method requires the database URL as the parameter. There are some differences between different database URLs, but they all comply with the format of "protocol name + IP address + port number + database name. If you have a database user name and password, add the following:
        String url = "jdbc:oracle:thin:@localhost:1521:pdborcl";String username = "123";String password = "123";        Connection conn = DriverManager.getConnection(url, username, password);
StateMent executes static SQL statements. It can combine multiple SQL statements into one batch and submit them to the database as a whole. We use the Connection object to create a Statement object, and then use the execute method of Statement to execute the SQL Statement. In addition, the PreparedStatement object is inherited from the Statement object. Here we use the PreparedStatement object as an example. Note that the batch processing can only be executed using the Statement object.
      PreparedStatement  pstmt = conn.prepareStatement("select * from t_user where userId=?");      pstmt.setInt(1, id);      ResultSet rs = pstmt.executeQuery();
The Result set after the ResultSet executes an SQL query. The Result has a pointer to the current row and can be used to read data in the Result set. Initially, the Pointer Points to the front of the first line. The Next () method of this object can move the pointer. If the row after Next () is valid, True is returned; otherwise, False is returned. Therefore, the Next () method is used as the judgment basis for loop.
Now the brief introduction of JDBC is over, and JDBC acts as a communicator in use. This reminds me of the Design Pattern of Yao Ming playing in the NBA: the adapter pattern, which makes Java applications cross-platform feature. At the same time, JDBC and ODBC are also typical examples of interface-oriented programming ideas. By the way, an ole db is missing, which is not mentioned here. Next time ......

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.