Monday, 4 February 2013

Introduction to JDBC | Java J2EE Tutorial pdf

Introduction to JDBC

JDBC provides a standard library for accessing relational databases. Using the JDBC API, we can access a wide variety of different SQL databases with exactly the same Java syntax. It is important to note that although JDBC standardizes the mechanism for connecting to databases, the syntax for sending queries and committing transactions, and the data structure representing the result, it does not attempt to standardize the SQL syntax. So, we can use any SQL extensions our database vendor supports. However, since most queries follow standard SQL syntax, using JDBC lets us change database hosts, ports, and even database vendors with minimal changes in our code. Officially, JDBC is not an acronym and thus does not stand for anything. Unofficially, “Java Database Connectivity” is commonly used as the long form of the name.
The JDBC API versus ODBC
Prior to the development of the JDBC API, Microsoft's ODBC (Open DataBase Connectivity) API was the most widely used programming interface for accessing relational databases. It offers the ability to connect to almost all databases on almost all platforms. So why not just use ODBC from the Java programming language?
1. A literal translation of the ODBC C API into a Java API would not be desirable. For example, Java has no pointers (address variables), and ODBC makes copious use of them, including the notoriously error-prone generic pointer void *. We can think of JDBC as ODBC translated into a high-level object-oriented interface that is natural for programmers using the Java programming language.
2. ODBC is hard to learn. It mixes simple and advanced features together, and it has complex options even for simple queries. The JDBC API, on the other hand, was designed to keep simple things simple while allowing more advanced capabilities where required. The JDBC API is also easier to use simply because it is a Java API, which means that a programmer does not need to worry about either memory management or
data byte alignment.
3. A Java API like JDBC is needed in order to enable a "pure Java" solution, that is, a solution that uses only Java API. When ODBC is used, the ODBC driver manager and drivers must be manually installed on every client machine. When the JDBC driver is written completely in Java, however, JDBC code is automatically installable, portable, and secure on all Java platforms, from network computers to mainframes.
4. The JDBC API includes functionality that is not available with ODBC. For example, ODBC does not support SQL99 data types, auto-generated keys, or savepoints. JDBC comes in two parts:
the portable JDBC API provided with Java, and the database-specific driver usually provided by the DBMS vendor or a third party. These drivers have to conform to a particular interface (called Driver) and map from the generic calls into something the existing database code can understand. JDBC API comes in two packages java.sql and javax.sql.

No comments: