JDBCTM Guide: Getting Started

Source: Internet
Author: User
Tags end connect odbc sql new features string stmt oracle database
This profile is quoted in the book Jdbctm Database Access from Javatm:a Tutorial and annotated Reference.
JavaSoft is currently preparing the book. This is a tutorial, as well as an important reference manual for JDBC, which will be part of the Java family in
Published in the spring of 1997 by Addison-wesley Publishing Company.


1.1 What is JDBCTM?
JDBCTM is a JAVATM API for executing SQL statements (interestingly, JDBC itself is a trademark name rather than an abbreviation; however, JDBC
is often considered to represent a "Java database connection (Java DB Connectivity)"). It consists of a set of classes and interfaces written in the Java programming language
Composition 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, you don't have to access the Sybase number
According to the library dedicated to write a program, to access the Oracle database and write a special program, to access the Informix database and another program, and so on. You only
It is sufficient to write a program with the JDBC API, which sends SQL statements to the appropriate database. Also, applications written in the Java programming language
There's no need to worry about writing different applications for different platforms. Combining Java and JDBC will make it easier for programmers to write the program once in any
platform to run on.

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. Need for
Just a way for a 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 that the applet uses may be
From the remote database. Enterprises can also use JDBC to connect all employees to one or more internal databases through an Intranet (even if the computers used by these employees
There are various operating systems, such as Windows, Macintosh, and UNIX. As more and more programmers start using the Java programming language, the Java
The need for easy access to the database is also increasing.

MIS administrators like the combination of Java and JDBC because it makes it easy and economical to spread information. Enterprises can continue to use the databases they have installed and
Easy access to information, even if the information is stored on different database management systems. The new program has a very short development period. Installation and versioning will be greatly simplified. Ride
The sequencer 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 on, Java and JDBC can provide external customers with a better way to get information updated.


What is the purpose of 1.1.1 JDBC?
Simply put, JDBC can do three things:



Establish a connection with the database,
Send SQL statement,
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");
}


1.1.2 JDBC is a low-level API that is the foundation of advanced APIs
JDBC is a "low-level" interface, which means it is used to invoke SQL commands directly. In this respect it is very functional and easier than other database connection APIs
is used, but it is also designed as an underlying interface on which you can build advanced interfaces and tools. The advanced interface is the "user-friendly" interface, which enables
It is a more comprehensible and convenient API that is converted behind the scenes into low-level interfaces such as JDBC. When writing this article, you are developing
Two advanced JDBC-based APIs:


An embedded SQL used in Java. At least one provider is planning to write it. DBMS implementation SQL: A specially designed to be used in conjunction with a database
Language. JDBC requires that the SQL statement be passed to the Java method as a String. Instead, the embedded SQL preprocessor allows programmers to direct SQL statements directly to the
Java is used in a mix. For example, you can use Java variables in SQL statements to accept or provide SQL values. The embedded SQL preprocessor will then pass
The JDBC call converts this java/sql mixture to Java.
A direct mapping of relational database tables to Java classes. JavaSoft and other providers are claiming to implement the API. In this "object/relationship" mapping, the
Each row 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 access data
The required SQL calls 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
has 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. When the required information is entered, the application automatically invokes the desired
SQL commands. 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.


Comparison of 1.1.3 JDBC with ODBC and other APIs
Currently, the Microsoft ODBC (Open Database Connectivity) API is probably the most widely used programming interface for accessing relational databases. It can be done in almost
There are almost all databases connected to the platform. Why does Java not use ODBC?

The answer to this question is: Java can use ODBC, but preferably with the help of JDBC in the form of Jdbc-odbc Bridge, which we will later
Said. Now the question has become: "Why do we need JDBC?" The answer is as follows:


ODBC is not suitable for direct use in Java because it uses the C language interface. Invoke local C code from Java in security, implementation, robustness, and program
There are a number of drawbacks to automatic portability.
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 easy
The wrong pointer "void *"). You can think of JDBC as an ODBC that is converted to an object-oriented interface, whereas an object-oriented interface is more
Easy to receive.
ODBC is difficult to learn. It mixes simple and advanced features, and the options are extremely complex even for simple queries. Instead, JDBC tries to ensure that simple work
Ease of use, while allowing for advanced functionality when necessary.
Enabling the "pure Java" mechanism requires a Java API such as JDBC. If you are using ODBC, you must manually place the ODBC driver Manager and Driver
Installed on each client computer. If you write the JDBC driver completely in Java, the JDBC code is on all Java platforms (from the network computer to the mainframe)
Can be automatically installed, ported, and secured.
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. Therefore, the familiar
ODBC programmers will find that JDBC is easy to use. JDBC retains the basic design features 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.

Recently, 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. But in these interfaces, we don't see any special features that make us choose to switch to it.
To replace ODBC, especially if the ODBC driver has been established in a more sophisticated market. They add a kind of decoration to ODBC at most, and
Has. This is not to say that JDBC does not need to evolve from its original version; However, we feel that most of the new features should be grouped into objects such as those described in the previous section
Advanced APIs such as relational mapping and embedded SQL.


1.1.42-layer model and three-layer model
The JDBC API supports both the two-tier model of database access as well as the three-tier model.

In a two-tier model, Java applets or applications will be directly talking to the database. This will require a JDBC driver to access the specific database
Management system for communication. 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 a different computer, and the user
Connect to the top through 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. Network can
To be an Intranet (it can connect company employees) or 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 statement and
The results are sent back to the middle tier, and the middle tier sends the results back to the user. MIS executives found that the three-tier model was attractive because the middle tier could be used to control the number of companies
According to the types of visits and updates that can be made. Another advantage of the middle tier is that users can take advantage of the advanced API that is easy to use, and the middle tier will convert it to the corresponding
of low-level calls. 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 optimizer compiler (it puts Java
The introduction of byte code into highly efficient machine-specific code, it becomes more and more practical to implement the middle tier in Java. It's going to be a lot of progress and it
Enables people to take full advantage of the many advantages of Java (such as robustness, multithreading, and security features). JDBC access to the database from the Java middle tier is very
Important.


1.1.5 SQL Consistency
Structured Query Language (SQL) is the standard language for accessing relational databases. The difficulty is that although most DBMS (database management systems) have a basic
Features use standard forms of SQL, but they do not conform to the standard SQL syntax or semantics recently defined for more advanced functionality. For example, not all of the data
Libraries 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 expand
Exhibition 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 any number of SQL features, but it must take the risk that there may be an error on some of the DBMS. In fact, application queries are not even necessarily
SQL, or it can be a dedicated (for example, document or image query) of SQL designed for a particular DBMS.

The second way JDBC handles SQL consistency issues is to provide ODBC-style conversion clauses. This will be in the 4.1.5 section "statement Object SQL Escape Syntax"
discussed in.

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 uses the DatabaseMetaData interface to provide information about the DBMS
Descriptive information, 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. "In line with
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 standards adopted by the U.S. National Standards Bureau in 1992.) The Entry level represents a specific list of SQL features. Driver Open
The sender can use the test kits that are available with the JDBC API to determine whether their drivers meet these criteria.

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


1.2 JDBC Products
When writing this article, several JDBC based products have been developed or are in the process of being developed. Of course, the information in this section will soon become obsolete information. Therefore, there
For the latest information, check out the JDBC Web site and find it by starting at the following URL:

Http://java.sun.com/products/jdbc


1.2.1 JavaSoft Framework
JavaSoft provides three JDBC product components that are part of the Java Development Kit (JDK):


JDBC Driver Manager,


JDBC Driver Test Kit, and


Jdbc-odbc Bridge.


The JDBC Driver Manager is the backbone of the JDBC architecture. It's actually small and simple; its main role is to connect Java applications to the correct
JDBC driver, and then exit.

The JDBC Driver Test Kit provides a certain level of credibility for the JDBC driver to run your program. Test package drivers only through the JDBC driver
Order is considered to conform to the JDBC standard TM.

The Jdbc-odbc Bridge enables ODBC drivers to be used as JDBC drivers. Its realization provides a way for the rapid development of JDBC, its long-term goal
A way to access some of the less common DBMS (if JDBC is not implemented for these uncommon DBMS).





Type of 1.2.2 JDBC driver
The JDBC drivers we currently know can be categorized into the following four categories:


JDBC-ODBC Bridge plus ODBC driver: JavaSoft Bridge product provides JDBC access using ODBC drivers. Note that the ODBC binary code must be
(In many cases, the database client code, too) is loaded on each client computer that uses the driver. Therefore, this type of driver is best suited for enterprise
Network (the installation of the client on this network is not the main problem), or the three-tier application server code written in Java.
Local API-Some drivers written in Java: This type of driver converts JDBC calls on the client API to Oracle,
A call to Sybase, Informix, DB2, or other DBMS. Note that, like the bridge driver, this type of driver requires that some binary code be loaded
To each client computer.
JDBC Network Pure Java driver: This driver converts JDBC to a DBMS-independent network protocol, which is then converted by a server
is a DBMS protocol. This kind of network server middleware can connect its pure Java client to many different databases. The specific protocol used depends on
Provider. Typically, this is the most flexible JDBC driver. It is possible that all providers of this solution offer products that are appropriate for Intranet use. For
To make these products also support Internet access, they must handle additional requirements for WEB security, access through firewalls, and so forth. Several mention
The provider is adding JDBC drivers to their existing database middleware products.
Native protocol Pure Java driver: This type of driver converts a JDBC call directly to the network protocol used by the DBMS. This will allow from the client machine
To call the DBMS server directly on the device is a practical solution for Intranet access. Since many of these protocols are private, the database
The donor himself will be the main source, and several providers are already working on it.
Finally, we anticipate that the class 3rd and 4 drivers will be the preferred method of accessing the database from JDBC. 1th, Class 2 driver in direct pure Java driver
The preface will be used as a transitional scheme before it is listed. There may be some variants of the 1th or 2 driver (not listed in the table below), which require a connection
Connectors, but these are usually more undesirable solutions. The 3rd and 4 class drivers provide all the benefits of Java, including automatic installation (for example, by making
Use the JDBC driver applet applet to download the driver.

The following table shows the 4 types of drivers and their properties:


Driver kind of pure JAVA? Network protocol
1-JDBC-OCBC Bridge is not directly
2-non-direct based on local API
3-JDBC network is to require connectors
4-based on the local protocol is directly



1.2.3 JDBC Driver Access
At the time this article was written, there were dozens of driver drivers for the 1:ODBC driver that could be used in conjunction with the JavaSoft Bridge. Have about
More than 10 drivers that belong to type 2 are written based on the DBMS's local API. There are only a few drivers that belong to type 3. There are currently at least 2
is a driver of type 4, but by the end of 1997, we expect that the main DBMS will have a driver of type 4.

To get the latest information about the driver, check the JDBC Web site at: http://java.sun.com/products/jdbc. Provide a 3rd kind
The first providers of drivers are SCO, Open Horizon, Visigenic, and WebLogic. The leading provider of JavaSoft and database Connectivity Intersolv
The Jdbc-odbc bridge and the JDBC Driver Test kit were developed in collaboration.


1.2.4 Other Products
Development tools for various JDBC applications are under development. Please check the JavaSoft Web page for updated information.



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.