"JSP learning - comprehensive analysis of JDBC (8) appendices"

xiaoxiao2021-03-06  25

Appendix: Analysis of JDBC TM Technology

1. Definition of JDBCTM

JDBCTM is a JavaTM API for executing a SQL statement, which consists of a set of classes and interfaces written in Java programming languages. JDBC provides a standard API for tools / database developers, enabling them to write database applications with pure Java APIs.

With JDBC, send SQL statements to various relational databases is a very easy thing. In other words, there is a JDBC API, you don't have to write a program for accessing the Sybase database, and write a program for accessing the Oracle database, and write another program for accessing the Informix database, and so on. You only need to write a program with JDBC API enough to send SQL statements to the corresponding database. Moreover, using the application written in Java programming language does not need to worry about writing different applications for different platforms. Combine Java and JDBC will allow programmers to write only a program to run on any platform.

Java has rugged, safe, easy to use, easy to understand and automatically downloaded from the network, is an outstanding language to write database applications. The required method is only a method of dialogue between the Java application and various databases. JDBC is a mechanism for such uses.

JDBC extends the functionality of Java. For example, using Java and JDBC APIs can publish the Applet's web page, and the information used by the applet may also be connected to one or more internal databases from remote database companies. There are a variety of different operating systems such as Windows, Macintosh, and UNIX). As more and more programmers began using Java programming languages, the requirements from Java in conjunction with the database are also increasing.

MIS administrators like Java and JDBC combination because it makes information spread easy and economical. Companies can continue to use them to install the database and can easily access information, even if this information is stored on different database management systems. The new program is very short. Installation and version control will be greatly simplified. Programmers can only write only one version or only update once, then put it on the server, and anyone can get the latest version of the application. For business sales information services, Java and JDBC provide better ways to obtain information updates for external customers.

2. JDBC use

Simply put, JDBC can do three things: establish a connection with the database, send SQL statements and process the results. The following code segments give the basic examples of the above three steps:

Connection con = DRIVERMANAGER.GETCONNECTION ("JDBC: ODBC: Wombat", "Login", "Password"); statement stmt = con.createstatement (); ResultSet RS = Stmt.executeQuery ("SELECT A, B, C from Table1" WHILE (rs.next ()) {INT x = rs.getint ("a"); string s = rs.getstring ("b"); float f = rs.getfloat ("c");} The above code During JDBC-based database access, a classic summary, of course, the subsequent part of this section will explain a detailed analysis of it.

3. JDBC API

JDBC is a "low" interface, that is, it is used to call the SQL command directly. In this regard, its function is excellent and is easy to use than other databases, but it is also designed as a foundation interface, which can establish advanced interfaces and tools. The advanced interface is an "friendly" interface for the user. It is a more easy to understand and more convenient API, which is converted to a low-level interface such as JDBC behind the scenes.

In the Object / Relationship mapping of the relational database, each row in the table corresponds to an instance of the class, and the value of each column corresponds to an attribute of the instance. Thus, the programmer can operate directly to the Java object; the SQL call required to access data will be automatically generated in "cover". More complex mappings can also be provided, such as combining rows in multiple tables into a Java class.

As people's interest in JDBC is increasing, more and more developers have been using JDBC-based tools to make the program's writing easier. Programmers have been writing to make the end user's access to the database more simple applications. For example, an application provides a menu for selecting a database task. After the task is selected, the application will give prompts and blank to fill in the information required to perform the selected task. The required information Enter the application will automatically call the desired SQL command. With the assistance of such a program, even if the user does not understand the syntax of SQL, the database task can also be performed.

4. Comparison of JDBC and ODBC and other APIs

Currently, Microsoft's ODBC API may be the most widely used and used to access programming interfaces for relational databases. It can connect almost all databases on almost all platforms. Why does Java do not use ODBC? The answer to this question is: Java can use ODBC, but it is best to use the JDBC-ODBC bridge with the help of JDBC. This will then say later. The problem now has become: "Why do you need JDBC? The answer is obvious: ODBC is not suitable for use directly in Java because it uses a C language interface. There are many disadvantages from Java to call local C code in security, implementation, robustness, and automatic portability of procedures. Field translation from ODBC C API to Java API is not available. For example, Java does not have a pointer, while ODBC is widely used (including pointers "void *"). You can imagine JDBC to be converted to ODBC-oriented interface, and object-oriented interfaces are easier to receive for Java programmers. ODBC is difficult to learn. It is mixed with simple and advanced features, and even for simple queries, its options are also extremely complicated. Instead, JDBC should ensure the simple functionality of simple functions, while the advanced features are allowed when necessary. Enable the "Pure Java" mechanism requires Java API like JDBC. If you use ODBC, you must manually install the ODBC driver manager and driver on each client. If you write a JDBC driver using Java, JDBC code can automatically install, transplant and ensure security on all Java platforms (from network computers to large machines).

In summary, JDBC API is a natural Java interface for basic SQL abstractions and concepts. It is built on ODBC instead of from scratch. Therefore, the programmer familiar with ODBC will find that JDBC is easy to use. JDBC retains the basic design characteristics of ODBC; in fact, both interfaces are based on the X / Open SQL CLI (call-level interface). The biggest difference between them is that JDBC is optimized based on Java style and advantages, making it easier to use.

Currently, Microsoft introduces new APIs outside ODBC: RDO, ADO, and OLE DB. These designs are the same as JDBC in many ways, ie they are object-oriented database interfaces and are based on classes that can be implemented on ODBC. However, in these interfaces, we have not seen that there is a special feature to choose them to replace ODBC, especially in the case of the ODBC driver has established a relatively complete market. Their most is also a decoration in ODBC.

5. JDBC support for B / S and C / S mode

JDBC API supports both two-layer model (C / S) for database access, and also supports three-layer models (B / S). In two layers models, Java Applets or applications will talk directly to the database. This will require a JDBC driver to communicate with the accessed specific database management system. The user's SQL statement is sent to the database, and the result will be sent back to the user. The database can be located on another computer, and the user is connected to the above. This is called client / server configuration, where the user's computer is a client, providing a computer for a server for a server. The network can be intranet (it can connect the company staff) or the Internet.

In the three-layer model, the command first is sent to the "intermediate layer" of the service, and then it is sent to the database by it. The database is handled on the SQL statement and send the result back to the intermediate layer, and the intermediate layer is sent back to the user. The MIS executives found that the three-layer model was very attractive because the intermediate layer can be used to control the type of access to the company's data and the available update. Another benefit of the intermediate layer is that users can utilize the easy-to-use advanced APIs, while the intermediate layer will convert it to the corresponding low-level calls. Finally, in many cases the three-layer structure can provide some performance benefits. So far, the intermediate layer is usually written in a C or C language, and these languages ​​are executed faster. However, with the introduction of the optimization compiler (which converts Java byte code to efficient machine-specific machine-specific, the intermediate layer will become increasingly practical. This will be a big progress that allows people to make full use of the advantages of Java (such as rugged, multi-threaded and security). JDBC is very important for accessing the database from the intermediate layer of Java.

6. Consistency of SQL

Structured Query Language (SQL) is a standard language for accessing relational databases. Difficulties are: Although most DBMS (database management systems) use standard SQLs for their basic functions, they do not meet the recent standard SQL syntax or semantics defined for more advanced features. For example, not all databases support storage programs or external connections, and databases that support this feature are inconsistent. That of the true standard in SQL is able to expand to include more and more functions. But at the same time, the JDBC API must support existing SQL.

One way to solve this problem is to allow any query string to be transferred to the DBMS driver involved. This means that the application can use any more SQL features, but it must take such a risk: it is possible to make an error on some DBMS. In fact, the application query does not even have to be SQL, or it can be a dedicated organism for SQL designed for a specific DBMS (eg, document or image query).

The second method of JDBC handling SQL consistency is to provide an essentials of the ODBC style, which will be discussed in subsequent sections. Syntax provides a standard JDBC syntax for several common SQL disagreements. For example, there is a syntax for the call to date text and stored procedures.

For complex applications, JDBC uses a third method to process SQL consistency problems that use the DatabaseMetadata interface to provide descriptive information about DBMS, so that the application can adapt to each DBMS requirements and functions.

Since the JDBC API will be used as the foundation API of the Advanced Database Access Tool and API, it must also pay attention to all of its upper buildings. "JDBC Standard TM" represents the standard level of the JDBC function that the user can rely on. To use this, the driver must at least support ANSI SQL-2 Entry Level (ANSI SQL-2 represents the specific list of the Standards used in 1992 in 1992, US National Standards Bureau). The driver developer can use the Test Kit with JDBC API to determine if their drivers meet these standards.

转载请注明原文地址:https://www.9cbs.com/read-69193.html

New Post(0)