JDBC interface technology (10:13:45)
Author: Zheng linfeng Zhang gongzhong
JDBC is a Java API that can be used to execute SQL statements (ApplicationProgrammingInterface application design interface ). It consists of classes and interfaces written in some Java languages. JDBC provides a standard application design interface for database application developers and Database Front-End Tool developers, allowing developers to write complete database applications in pure Java language.
1. Development History from ODBC to JDBC
Speaking of JDBC, it is easy to think of another very familiar word "ODBC ". Are there any links between them? If so, what is the relationship between them?
ODBC is short for OpenDatabaseConnectivity. It is a standard application data interface that is used to access data in a related or unrelated Database Management System (DBMS) and implemented in C language. Through ODBCAPI, applications can access data stored in a variety of different database management systems (DBMS), regardless of the data storage format and programming interface used by each DBMS.
1. ODBC Structure Model
The structure of ODBC includes four main parts: application interface, driver manager, database drive and data source.
Application Interface: shields the differences in function calls between different ODBC database drivers and provides users with a unified SQL programming interface.
Driver MANAGER: load the database drive for the application.
Database drive: Implements ODBC function calls and provides SQL requests to specific data sources. If necessary, the database drive modifies the application request so that the request complies with the grammar supported by the relevant DBMS.
Data source: it consists of the data you want to access, the operating system associated with it, the DBMS, and the network platform used to access the DBMS.
Although the ODBC driver manager mainly aims to load the database drive for ODBC function calling, the database drive itself also executes ODBC function calling and works with the database. Therefore, when the application system makes a call to connect to the data source, the database drive can manage the communication protocol. When a connection is established with the data source, the database drive can process the requests sent from the application system to the DBMS, and perform necessary translation for the analysis or design from the data source, return the result to the application system.
2. The birth of JDBC
Java has become popular around the world since it was officially announced in May 1995. A large number of programs are written in java, including database applications. Since there is no Java API, programmers have to add c-language ODBC function calls to Java programs. This makes the outstanding features of many Java unable to be fully utilized, such as platform independence and object-oriented features. As more and more programmers are interested in the Java language, more and more companies are investing more and more energy in Java program development, and more requests are required to access database APIs through java language interfaces. Due to ODBC's shortcomings, such as its inaccessibility and lack of object-oriented features, SUN decided to develop an interface for developing database applications whose Java language is an interface. In JDK1.x, JDBC is only an optional component. When JDK1.1 is published, the SQL class package (JDBCAPI) becomes a standard component of the Java language.
Ii. JDBC technology Overview
JDBC is a Java API that can be used to execute SQL statements (ApplicationProgrammingInterface, application design interface ). It consists of classes and interfaces written in some Java languages. JDBC provides database application developers and Database Front-End Tool developers with a standard application design interface, allowing developers to write complete database applications in pure Java language.
By using JDBC, developers can easily send SQL statements to almost any database. That is to say, developers do not have to write a program to access Sybase, write another program to access Oracle, and write another program to access Microsoft SQLServer. Programs Written in JDBC can automatically send SQL statements to the corresponding database management system (DBMS ). In addition, applications written in Java can run on any platform that supports Java without having to write different applications on different platforms. The combination of Java and JDBC enables developers to truly implement "WriteOnce, RunEverywhere!" during database development !"
Java is robust, secure, easy to use, and supports automatic online downloads. It is essentially a good programming language for database applications. What it needs is how Java applications connect to a variety of databases. JDBC is the key to achieving this connection.
JDBC extends Java capabilities. For example, you can use Java and JDBCAPI to publish a Web page with an Applet that can access the remote database. Or enterprises can use JDBC to connect all employees (they can use different operating systems, such as Windwos, machkers, and UNIX) to several global databases on the Intranet, these global databases may be different. As more and more developers use the Java language, the demand for the ease of database access by Java is getting stronger and stronger.
MIS administrators like Java and JDBC, because it makes it easier to publish information economically. All kinds of transactions that have been installed in the database will continue to run normally, and even these transactions are stored in different database management systems. For new database applications, the development time will be shortened, and the installation and version upgrade will be greatly simplified. A programmer can write or rewrite a program and place it on the server. Each user can access the server to obtain the latest version. For the information service industry, Java and JDBC provide a good way to update information to external users.
1. JDBC task
To put it simply, JDBC can accomplish the following three tasks:
1) establish a connection to the same database;
2) Send SQL statements to the database;
3) process the results returned by the database.
2. JDBC-an underlying API
JDBC is an underlying API, which means that it will directly call SQL commands. JDBC is fully qualified for this task and easier to implement than other databases. It is also the basis for constructing high-level APIs and database development tools. High-level APIs and database development tools should be user-friendly, easy to use, and easy to understand. However, all such APIs will eventually be translated as underlying APIs such as JDBC. Currently, two JDBC-based high-level APIs are in the development stage.
1) the SQL language is embedded into the Java Preprocessor. Although DBMS has implemented SQL query, JDBC requires that the SQL statement be transmitted to the Java program as a string parameter. The embedded SQL Preprocessor allows programmers to mix SQL statements: Java variables can be used in SQL statements to receive or provide numerical values. Then, the SQL Preprocessor translates the mixed Java/SQL programs into Java programs with JDBCAPI.
2) implement direct ing from relational databases to Java classes. Javasoft and other companies have announced that they want to implement this technology. In this "Object/relationship" ing, each row of the table will become an instance of this type, and the values in each column correspond to an attribute of the instance. Programmers can directly operate on Java objects, and the SQL calls required for access will be generated internally. You can also implement more complex ing, such as implementing multiple table rows in a Java class.
With the increasing interest in JDBC, more and more developers have begun to use JDBC-based tools for development. This makes development easier. At the same time, programmers are also developing applications that are easier for end users to access the database.
3. Comparison of JDBC, ODBC and other APIs
So far, Microsoft ODBC may be the most widely used API for accessing relational databases. It provides the ability to connect to almost any platform and any database. So why not use ODBC directly from Java?
The answer is that ODBC can be used from Java, but it is best to implement it with the help of JDBC with the JDBC-ODBC bridge. So why does JDBC be required? To answer this question, there are several aspects:
1) ODBC is not suitable for direct use in Java. ODBC is an API implemented in C language. Calling a local C program from a Java program brings a series of shortcomings similar to security, integrity, and robustness.
2) Secondly, fully accurate ODBC translation from C code ODBC to Java API writing is not satisfactory. For example, Java does not have a pointer, while ODBC uses a pointer in a large amount, including a very error-prone empty Pointer "void *". Therefore, for Java programmers, it is natural to imagine JDBC as an API that converts ODBC to object-oriented.
3) ODBC is not easy to learn. It blends simple features with complex features, and even has complicated options for very simple queries. JDBC, on the contrary, maintains the simplicity of simple things, but allows complex features.
4) JDBC APIs are required for pure Java solutions. When using ODBC, you must install the ODBC driver and driver manager on each client. If the JDBC driver is fully implemented in Java, the JDBC code can be automatically downloaded and installed to ensure its security. In addition, this will adapt to any Java platform, from Network Computer NC to Mainframe.
All in all, JDBCAPI is the most direct Java interface that reflects the basic abstract concepts of SQL. It is built on the basis of ODBC. Therefore, programmers familiar with ODBC will find it very easy to learn JDBC. JDBC maintains the basic design features of ODBC. In fact, both interfaces are based on the call-level interface (CLI) of X/OPENSQL ). The biggest difference between them is that JDBC is based on the Java style and advantages, and strengthens the Java style and advantages.
Recently, Microsoft has launched new APIs in addition to ODBC, such as RDO, ADO, and OLEDB. In fact, these Apis work in the same direction as JDBC in many aspects, that is, they strive to become an object-oriented ODBC-based class interface. However, these interfaces cannot replace ODBC at present, especially when ODBC drivers are fully formed in the market, and more importantly, they are just "beautiful packaging" of ODBC ".
4. JDBC two-tier model and three-tier model
JDBC supports two-tier models and three-tier models for database access.
3. In a two-tier model, a java Appple or JA-va application is directly connected to the database. This requires a JDBC drive that can be connected directly to the accessed database. User SQL statements are sent to the database, and the execution results of these statements are returned to the user. Databases can be connected on the same machine or on another machine over the network. This is called the "Client/Server" structure. The user's computer acts as the Client and the computer that runs the database acts as the Server. This network is an intranet. For example, the intranet connecting all employees can also be an internet.
4. In the layer-3 model, commands are sent to the "middle layer" of the service, while the "middle layer" sends SQL statements to the database. The database processes SQL statements and returns the results to the "middle layer", and then the "middle layer" returns them to the user. The MIS administrator will find the three-tier model attractive, because the "middle layer" can control access and coordinate database updates, another advantage is that a "middle layer" user can use an easy-to-use high-level API, which can be converted from the "middle layer" to the underlying call. Moreover, in many cases, the layer-3 model can provide better performance.
So far, the "middle layer" is usually implemented using C or C ++ to ensure its high performance. However, with the introduction of the optimized compiler, java bytecode is converted