Java JDBC Database access technology

Source: Internet
Author: User
Tags connect odbc odbc connection requires port number

Before we know about JDBC, we can make a review of ODBC to better understand JDBC. See the name also know that these two relationships are not general, they achieve the same function, for the application connection and operation of the database support. So, let's start with ODBC.

Odbc

ODBC (Open Database Connectivity) is a short name for open databases interconnection, an application interface that uses SQL. It is a series of specifications and APIs for accessing the database. Then Api+sql can perform operations on the database. It is not dependent on the DBMS, that is, you can connect most databases in the same way through ODBC. It includes the application interface, drive Manager, database drive, data source. Let's look at the relationship between the various parts of ODBC through a graph:

Jdbc

As I said before, JDBC and ODBC actually have the same functionality, but not quite the same. First ODBC is based on the C + + language, so it is not very consistent with Java object-oriented thinking, communication is more difficult. As a result, JDBC is out of sight, that is, JDBC is a Java language based database access API interface. Then the other content is basically the same as ODBC. As long as you understand the ODBC, then the concept of basic understanding of JDBC. So what you need to do is to get a good look at the interface of new faces, and the functionality of these interfaces is the same as ODBC, but it's just that there are subtle differences in implementation. Here's a look at the structure of the JDBC diagram:

Connection mode

As you can see from the structure diagram above, JDBC offers a number of different ways to connect. This is a general understanding of it, because this is mainly the database manufacturers concerned about, we know, and some do not quite understand.

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

1. JDBC-ODBC Connection Bridge: This approach is based on ODBC, which says that communication between Java applications and ODBC is a bit cumbersome. However, the standard application of ODBC as a database access is very broad. So JDBC makes it easy to access the database that used ODBC by mapping the function call of ODBC.

2. Local API driver: A call to convert a JDBC call into a client-side binary code base on the database interface. But this interface library relies on the manufacturer, because we are not invoking JDBC interface implementations provided by the database vendor.

3, Pure Java Local protocol: that is, the JDBC call mapped to the DBMS Network Listening protocol function call, the Listener monitor the request after the implementation of the relevant database operations. The listener is provided by the manufacturer.

Introduction to Common interfaces

DriverManager

We don't need to know about how the driver is registered. What we need to know is how to invoke the method to load the database driver. The Class.forName () method that calls this method requires passing a string object that contains the driver class name as an argument. As follows:

Class.forName ("Oracle.jdbc.driver.OracleDriver")

Connection The driver is loaded, the connection to the database requires a call to the Drivermanager.getconnection () method, which requires a database URL as a parameter, and a different database URL, but it all conforms to the protocol name + IP address + The format of the port number + database name. The database username and password, if any, must be added as follows:

       String url = "Jdbc:oracle:thin: @localhost: 1521:PDBORCL";    
String username = "123";    
String Password = "123";    
       Connection conn = drivermanager.getconnection (URL, username, password);

StateMent executes a static SQL statement, it can also combine multiple SQL statements into a batch, which is submitted to the database as a whole. We use the Connection object to create the statement object, and then execute SQL with the statement execute method. In addition, the PreparedStatement object is inherited from the statement object, and here we use PreparedStatement as an example, it should be noted that batches can only be performed with statement objects.

PreparedStatement  pstmt = conn.preparestatement ("select * from T_user where userid=?");    
Pstmt.setint (1, id);    
ResultSet rs = Pstmt.executequery ();

ResultSet executes a result set after an SQL query, results has a pointer to the current row and can be used to read the data in the result set. Initially, the pointer points to the front of the first line. The next () method of the object can move the pointer. False if the line after next () is valid to return true. Therefore, the next () method in the loop is used as the basis for judgment.
Here's a brief introduction to JDBC, which is the role of a communicator in the use of JDBC. This reminds me of the design pattern that Yao played in the NBA: Adapter mode, which makes it a cross-platform feature of the Java application.  At the same time, JDBC, ODBC, etc. is also a typical embodiment of interface-oriented programming ideas. Yes, there's an OLE DB missing here, not mentioned. Next time ...

Author: csdn Blog Zhuojiajin

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.