JSP Learning--Comprehensive analysis of JDBC (8) Appendix

Source: Internet
Author: User
Tags definition connect odbc query stmt sybase database client oracle database
JS Appendix: JDBC TM Technology Analysis

1. Definition of JDBCTM

JDBCTM is a JAVATM API for executing SQL statements, which consists of a set of classes and interfaces written in the Java programming language. JDBC provides tools/database developers with a standard API that enables them to write database applications with a pure Java API.

With JDBC, it's easy to send SQL statements to various relational databases. In other words, with the JDBC API, there is no need to write a program specifically to access the Sybase database, write a program for accessing an Oracle database, write another program for accessing the Informix database, and so on. All you need to do is write a program with the JDBC API. It can send SQL statements to the appropriate database. Also, applications written in the Java programming language do not need to worry about writing different applications for different platforms. Combining Java with JDBC will allow programmers to write the program once and let it run on any platform.

Java is robust, secure, easy-to-use, easy to understand, and automatically downloadable from the Web, and is an excellent language for writing database applications. All you need is a way for the Java application to talk to a variety of different databases. And JDBC is the mechanism for this purpose.

JDBC extends the functionality of Java. For example, a Web page containing applets can be published with the Java and JDBC APIs, and the information used by the applet may be from a remote database enterprise or by using JDBC to connect all employees to one or more internal databases via the Intranet (even if the staff uses a computer with Wi Ndows, Macintosh and UNIX, and many different operating systems. As more and more programmers begin to use the Java programming language, the need for easy access to the database from Java is increasing.

MIS administrators like the combination of Java and JDBC because it makes it easy and economical to spread information. Organizations can continue to use their installed databases and access information easily, even if they are stored on different database management systems. The new program has a very short development period. Installation and versioning will be greatly simplified. Programmers can write only one application or update it once, and then put it on the server, and then anyone can get the latest version of the application. For business sales information Services, Java and JDBC can provide external customers with a better way to get information updated.

2. Use of JDBC

Simply put, JDBC can do three things: connect to the database, send SQL statements, and process the results. The following code snippet gives a basic example 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 makes a classic summary of database access based on JDBC, which, of course, is explained in detail in subsequent sections of this section.

3. JDBC API

JDBC is a "low-level" interface, which means it is used to invoke SQL commands directly. It is very functional in this regard and is easier to use than other database connection APIs, but it is also designed as an infrastructure interface on which to build advanced interfaces and tools. The advanced interface is the "user-friendly" interface, which uses an API that is easier to understand and easier to convert behind the scenes into low-level interfaces such as JDBC.

In the object/relationship mapping for a relational database, each row in the table corresponds to an instance of the class, and the value of each column corresponds to one of the properties of the instance. As a result, programmers can manipulate Java objects directly, and the SQL calls required to access data are automatically generated under cover. You can also provide more complex mappings, such as combining rows from multiple tables into a Java class.

As people become increasingly interested in JDBC, more and more developers have been using JDBC based tools to make programs easier to write. Programmers have also been writing applications that attempt to make access to the database easier for end users. For example, an application can provide a menu that selects database Tasks. When a task is selected, the application gives hints and blanks to fill in the information needed to perform the selected task. Required information input the application will automatically invoke the required SQL command. With the help of such a program, a database task can be performed even if the user does not know the syntax of SQL at all.

4. JDBC comparison with ODBC and other APIs

Currently, Microsoft's ODBC API may be the most widely used programming interface for accessing relational databases. It can connect almost all the databases on almost any platform. Why does Java not use ODBC? The answer to this question is: Java can use ODBC, but it is best to use it in the form of Jdbc-odbc Bridge with the help of JDBC, which we'll talk about later. Now the question has become: "Why do we need JDBC?" The answer is obvious: ODBC is not for use directly in Java because it uses the C language interface. Calling native C code from Java has many drawbacks in terms of security, implementation, robustness, and automatic portability of programs. The literal translation from the ODBC C API to the Java API is undesirable. For example, Java has no pointers, and ODBC uses a wide range of pointers (including error-prone pointer "void *"). You can imagine JDBC as an ODBC that is converted to an object-oriented interface, while object-oriented interfaces are easier to receive for Java programmers.

ODBC is difficult to learn. It mixes simple and advanced features, and the options are extremely complex even for simple queries. In contrast, JDBC tries to ensure simplicity of simple functionality while allowing for advanced functionality when necessary. Enabling the "pure Java" mechanism requires a Java API such as JDBC. If you use ODBC, you must manually install the ODBC Driver Manager and drivers on each client computer. If you write the JDBC driver completely in Java, the JDBC code can be installed, ported, and secured automatically on all Java platforms (from the network computer to the mainframe).

In summary, the JDBC API is a natural Java interface for basic SQL abstractions and concepts. It is built on ODBC rather than from scratch. As a result, programmers who are familiar with ODBC will find JDBC 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 based on Java style and advantages and is optimized to make it easier to use.

Currently, Microsoft has introduced new Api:rdo, ADO, and Ole DB outside of ODBC. These designs are in many ways the same as JDBC, that is, they are object-oriented database interfaces and are based on classes that can be implemented on ODBC. In these interfaces, however, we do not see any special functionality that allows us to turn to them instead of ODBC, especially if the ODBC driver has been established in a more sophisticated market. At best, they add a kind of decoration to ODBC.

5. JDBC support for b/S and C/s modes

The JDBC API supports both the two-tier model of database access (c/s) and the three-tier model (b/s). In a two-tier model, Java applets or applications will be directly talking to the database. This will require a JDBC driver to communicate with the specific database management system that you are accessing. The user's SQL statement is sent to the database, and the results are sent back to the user. The database can be located on another computer and the user is connected to it over the network. This is called a client/server configuration where the user's computer is a client and the computer that provides the database is the server. A network can be an Intranet (it can connect company employees), or it can be the Internet.

In the three-tier model, the command is sent first to the "middle tier" of the service, which then sends the SQL statement to the database. The database processes the SQL statements and sends the results back to the middle tier, which then sends the results back to the user. MIS executives found that the three-tier model was attractive because the middle tier was available to control access to corporate data and the types of updates that could be made. Another advantage of the middle tier is that the user can take advantage of an Easy-to-use advanced API, and the middle tier will convert it to the corresponding low-level call. Finally, in many cases, the three-tier architecture provides some performance benefits.

So far, the middle tier is usually written in languages such as C or C + +, and these languages perform faster. However, with the introduction of the optimizer compiler (which translates Java byte code into efficient machine-specific code), it becomes more practical to implement the middle tier in Java. This will be a big step forward, enabling people to take full advantage of Java's many advantages (such as robustness, multithreading, and security). JDBC is important for accessing databases from the Java middle tier.

6. SQL Consistency

Structured Query Language (SQL) is the standard language for accessing relational databases. The difficulty is that while most DBMS (database management Systems) use standard forms of SQL for their basic functionality, they do not conform to the standard SQL syntax or semantics recently defined for more advanced functionality. For example, not all databases support stored programs or external connections, and the databases that support this feature are inconsistent with each other. People want the real standard part of SQL to scale to include more and more features. However, the JDBC API must also support existing SQL.

One way the JDBC API solves this problem is to allow any query string to be uploaded to the DBMS driver involved. This means that an application can use as many SQL functions as possible, but it must take the risk that there may be an error on some of the DBMS. In fact, an application query is not even necessarily SQL, or it can be a dedicated creature of SQL designed for a particular DBMS (for example, a document or an image query).

The second way JDBC handles SQL consistency issues is to provide ODBC-style conversion clauses, which are discussed in a later section. The escape syntax provides a standard JDBC syntax for several common SQL disagreements. For example, there is an escape syntax for calls to date literals and stored procedures.

For complex applications, JDBC uses a third method to handle SQL consistency issues It leverages the DatabaseMetaData interface to provide descriptive information about the DBMS, enabling applications to adapt to the requirements and functionality of each DBMS.

Because the JDBC API will be used as the base API for developing advanced database access tools and APIs, it must also be aware of the consistency of all its superstructure. "Compliant JDBC Standard TM" represents the standard level of JDBC functionality that users can rely on. To use this description, the driver must at least support ANSI SQL-2 Entry level (ANSI SQL-2 represents the standard adopted by the U.S. National Standards Agency for 1992. The Entry level represents a specific list of SQL features. Driver developers can use the test kits that are available with the JDBC API to determine whether their drivers meet these standards.

"Compliant JDBC Standard TM" indicates that the provider's JDBC implementation has passed the conformance test provided by JavaSoft. These conformance tests examine whether all classes and methods defined in the JDBC API exist and, if possible, check that the program has SQL Entry level functionality. Of course, these tests are not complete, and JavaSoft currently has no intention of scaling the implementations of each provider. But this consistency definition does provide some credibility to the JDBC implementation. As more and more database providers, connection providers, Internet providers, and application programmers accept the JDBC API, JDBC is rapidly becoming a standard for Java database access.




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.