JDBC | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DescriptionJDBC is a programming-level interface for communicating with databases in a uniform manner similar to Microsoft's Open Database Connectivity (ODBC), which is the standard for WINTEL environments. The JDBC standard itself is based on the X/Open SQL Call Level Interface, the same basis as that of ODBC. This is one of the reasons why the initial development of JDBC has progressed so fast. [1] [14] JDBC enables Java developers to connect to any SQL compliant database, send SQL statements, and process return messages and data. As a point of interest, JDBC is a trademarked name and is not an acronym; nevertheless, JDBC is often thought of as standing for "Java Database Connectivity". JDBC consists of a set of classes and interfaces written in the Java programming language. This makes it possible to write database applications using a pure Java API. This allows much closer interaction than you would get by embedding C language function calls in Java programs using the Java Native Interface (JNI). [3] [7] The following code fragment gives a basic example of these three steps: [3] 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 = getInt("a");
String s = getString("b");
float f = getFloat("c");
}
Sun Microsystems has described JDBC as a "low-level" interface, meaning it is used to invoke (or "call") SQL queries directly. It works very well in this capacity and is easier to use than other database connectivity APIs. JDBC, however, was designed also to be a base upon which to build higher-level interfaces and tools. A higher-level interface would be application "user-friendly," therefore it would be a more convenient API that is translated behind the scenes into the specific interface such as JDBC. [3] Sun Microsystems also mentions two kinds of higher-level APIs that are under development on top of JDBC:
There are also other types of implementations for JDBC:
JDBC is available as part of the JDK 1.1 release but there is also an add-on kit available for JDK 1.0.2. The initial JDBC specification was released on March 8.1996 as a draft for open analysis [1] FeaturesJDBC is a set of object classes for processing transactions with databases and are written completely in Java. JDBC enables the programmer to maintain the security, robustness, and portability that make Java so exciting. However, to promote its use and maintain some level of backward compatibility, JDBC can be implemented on top of ODBC and other common SQL APIs from vendors. To compensate for the features of specific databases, vendors are allowed to extend the basic statement classes for their own designs. However, the basic statement classes must be supported at minimum with all databases for JDBC compliance. [1] To access databases, JDBC borrowed the format of the Uniform Resource Locator (URL) syntax. [1]
JDBC consists of three main layers: [1] [9]
Figure 1. The JDBC API Layers JDBC DriversThere are 4 basic types of JDBC drivers: [2] [3]
JDBC ClassesIn terms of the Java classes, the core JDBC API consists of: [1] [10] java.sql.DriverManager - matches the driver manager to the type specified in the URL; java.sql.Connection - connection-specific data structures; java.sql.Statement - container class for embedded SQL statements; Two-sub-types are:
java.sql.ResultSet - access control to results of a statement. [1] A simple SELECT statement can be realized with JDBC as follows. [2] try {
Connection con = DriverManager.getConnection (
"jdbc:mydriver:mydbserver", "userid", "password");
Statement s = con.createStatement();
ResultSet r = stmt.executeQuery("SELECT name, age, sex FROM Persons");
while (r.next()) { //for all rows of the query result do
String name = getString("name");
Int age = getInteger("age");
boolean sex = getBoolean("sex");
System.out.println("Name: "+name+", Age: "+age+", Sex: "+sex");
}
con.close(); //close database connection
} catch (SQLException e) {
System.out.println("An error occurred!");
}
A ResultSet contains the complete result (all rows) of a query. Therefore, a cursor is available which points to the current row of data. Initially the cursor is positioned before the first row. The method next() moves the cursor to the next row. There is no corresponding previous() method, therefore the result set can only be read once. Several get...() methods are available to read the data of the current row, or getObject() can be used if the datatype is unknown during runtime. [2] [10] JDBC Data TypesThe JDBC datatypes cover the basic and most used datatypes of common RDBMS. The next table shows their properties and the corresponding Java datatypes. Besides converting JDBC to vendor specific calls, the DBMS vendor specific data types must be converted to Java types. The following table shows the data types supported in JDBC. [2] [14]
These data types are defined in the JDBC class java.sql.Types and can be used to check a specific items type with the following code snippet: int currentType;
...
if (currentType == Types.VARCHAR)
System.out.println("current type is VARCHAR!");
In summary, JDBC follows an established specification and tried implementation for database-neutral communications from applications. The bridging solution to current ODBC systems allow users to maintain compatibility with older systems that may not have JDBC counterparts. Leading vendors are working actively on getting JDBC components for their databases out to market in a relatively short period of time. [1] [3] JDBC vs. ODBCAt this point, Microsoft's ODBC (Open DataBase Connectivity) API is probably 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 Java? [3] The answer is you can use ODBC from Java, but this is best done with the help of JDBC in the form of the JDBC-ODBC Bridge. The question now becomes, "Why do you need JDBC?" There are several answers to this question: [3]
In summary, the JDBC API is a natural Java interface to the basic SQL abstractions and concepts. It builds on ODBC rather than starting from scratch, so programmers familiar with ODBC will find it very easy to learn JDBC. The big difference is that JDBC builds on and reinforces the style and virtues of Java, and, of course, it is easy to use. [3] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return to Gerber Family Home Page |