|
|
|
||||||||||||||||
|
|||||||||||||||||
| JDBC: How do I learn JDBC? Experiencing database power.. | |||||||||||
| QuickStart Tutorials Examples Metadata Prepared and Callable Statements Connectivity and Drivers Performance OO Applications Servlet Database Applications RDBMS Vendor Specific SQL-J Book References |
Packages: java.sql javax.sql |
See also
|
|||||||||
| JDBC, Java Data Base Connectivity, provide Java programs with a way to connect
to and use relational databases. JDBC 1.0 was originally included in the JDK 1.1. Now, JDBC 2.0, included in Java
2 has been significantly enhanced. Ken North provides this excellent overview of JDBC, in WebTechniques, August 1999. In April 1999, java.sun.com featured an introduction to JDBC 2.0: Business-Critical Connections: The JDBC API by Laura Geele Wang. A general introduction JDBC 2 is in the Sun Java Tutorial: JDBC Database Access by Maydene Fisher. What you get out of learning JDBC is different for people with different backgrounds. If you know SQL and ODBC, you'll find JDBC is very straight forward and even easier. However, JDBC is the basis for nearly all interaction with relational data. So if you use Java and RDBMS, you will find JDBC highly important. |
|||||||||||
|
|
|||||||||||
| Very little code is required to make JDBC work. For a query you need to Load the driver, create a Connection, create a Statement, run the statement, and then use the ResultSet. How you get the driver loaded and get a connection depends on whether you are writing a "vanilla" JDBC program or writing inside a container - such as for a BMP Entity Bean. Here is an quick overview of both. | |||||||||||
|
|
|||||||||||
|
For just the basics (vanilla JDBC) - check out this example with the four basic steps of JDBC by Craig Kane at U. Colorado. A three-step simple introduction is provided by Developer's Daily:
On WDVL, the Illustrated Encyclopedia of Web Technology, Selena Sol presents a very nice step-by-step tutorial on JDBC. It's easy to follow, after an unfortunate rough start consisting of caveats about Java you really don't need to read. The link provided starts just at the right place in the tutorial. (If you want to read all the disclaimers about Java, just click on the word "Previous Page"). |
|||||||||||
| The JavaSoft Press book JDBC Access with Java by Hamilton, Cattell, and Fisher also allows you to download JDBC examples. Also try the downloadable exampleAdhoc SQL using servlets from Java Skyline. | |||||||||||
| Metadata is a highly useful feature of JDBC. Metadata provides you with information
about the RDBMS or about a resultset that you have retrieved. Brett Spell gives us an article on the subject: Enhancing
Database Code with Metadata in JavaPro, June 1999. Also, the Adhoc SQL
servlet provides you with examples of both database and resultset metadata calls. ResultSetMetadata and DatabaseMetadata enables, among other things to determine the data types of various database fields and columns. JDBC Recipe from Developers's Daily shows how to use ResultSetMetadata. Java Tip 82: Build data-type-independent JDBC applications by Sesh Venugopal from JavaWorld shows how to construct type-independent database tables using DatabaseMetadata. |
|||||||||||
|
To really understand the power and simplicity (compared with C/C++ ODBC for instance) of JDBC, just take a look at prepared statements and callable statements. The National Centre for Software Technology (India) provides very terse descriptions of Prepared Statement and Callable Statement. For a simple PreparedStatement example see San Diego State University CS 596: Client-Server Programming slides on JDBC - slide number 16 on this page. Batch inserts One very common use of PreparedStatement is for doing large batches of inserts from an external data source. JDBC 2.0 supports this with addBatch and executeBatch(). The following sample reads a tab separated file and inserts it into an RDBMS table: // Build ?,?,? section of insert command. String tabsep="\t"; // Set token separator to tab character. \t == (char) 9 in ASCII. try {
} catch (Exception e) { Check with the JDBC and database vendor to determine whether JDBC 2.0 has
been implemented. There are also PreparedStatement insert and update
examples in ClassicCity's Forum: JAVA and Oracle
section. There are some additional examples and a CallableStatement example in this JDBC Presentation by Shitlesh Patel
of the Dallas SCJD Study Group. Large Objects: Many RDBMs's now support storage of large objects. PreparedStatements are required if you want to insert large objects into the database. Here's an excellent example of large object insertion from IBM (1999).
|
|||||||||||
|
In a world where scalability is everything, performance is king. You may get relatively good performance by using a good Type 4 driver. However, with a driver that is especially designed for the RDBMS you are using you can boost performance by following certain procedures. For instance:
By reusing Prepared Statements your program can operate much more efficiently. When the JDBC driver initially prepares a statement, it parses and error checks the statement and then sends execution plan information to the RDBMS, including the bindings of columns that you use in the statement. These operations are not repeated each time you re-use the PreparedStatement, resulting both in significant processing reductions and communication port traffic reductions. Richard Dettinger and Mark Megerian explain how important this is when using the IBM native driver in an IBM AS/400 article: JDBC Performance Tips. You should also avoid constantly creating new connections and destroying connections by using connection pooling. Hans Bergsten provides ways to do connection pooling in Improved Performance with a Connection Pool, Sep 1998 from Web Developers Journal. Connection pooling is now a standard part of JDBC 2. Database applications often operate best with several tiers that the work among several servers. Chad Darby explains the 3-tired JDBC Web architecture in Developing 3-Tiered Applications with Java Servlets by Chad Darby, Jan 1999 from Java Developers' Journal. I-Kinetics also presents some multi-tiered architecture material that involves JDBC with EJB and Corba. |
|||||||||||
| For an OO perspective, Spilling the Beans: JDBC is Easy!
by Danial Brookshier from Java Report provides some guidelines on encapsulation to isolate JDBC specific code.
And Thomas Davis explains how to package JDBC into a facade pattern so that you don't get mired down in it in Clever
Facade makes JDBC look easy from JavaWorld. But, there are still things worth exploring in JDBC, such as metadata and connection controls even beneath such a facade. On JavaCats,Qusay H. Mahmoud presents A Persistant Storage for Java Objects. |
|||||||||||
|
The JDBC API is quite extensive but even so, most of its actual work goes
on under the hood inside the JDBC driver. Which driver you use depends not only on which RDBMS you are using, but
also on what kind of architecture you have or want to support. It's worth knowing about the four types of drivers
and Ahmad Abualsamid explains them in Java Connectivity With JDBC from
Network Computing, May, 1999. For production, you will probably want to use Type 4 or Type 3. Briefly, Type 4 JDBC
drivers are generally single-tiered database clients. They are pure Java and generally the easiest to install. Loading the JDBC driver. To use the JDBC driver, you must 1) tell your program to load and activate the driver by class name and 2) specify where the driver is on the classpath. 1) Loading and activation is fairly simple. Each JDBC driver has a class name. For instance, Oracle's is "oracle.jdbc.driver.OracleDriver." Most recent versions of the JDBC specification require that all JDBC drivers must be loaded and activated with the Java program code:
2) To get to the driver class, the JDBC driver must be accessible from your program's class path. For a typical application, this just means specifying the driver's jar or zip file. For example:
|
|||||||||||
|
Setup. To run JDBC from a servlet or EJB server you need to set the classpath for the servlet engine or EJB server to include the JDBC driver. This may seem obvious (especially if you just read the previous paragraph above), but you still have to remember to do it and that can require some getting used to. Also setting the classpath is different for each kind of servlet engine and EJB server. For Tomcat, just copy the JDBC driver (jar or zip) into Tomcat's lib directory. Tomcat's startup script automatically includes the lib directory and all jars. For other servlet engines refer to the instructions for the servlet engine.
For example, IBM provides this
overview and set of instruction for configuring S/390 Websphere
to run JDBC with their DB2 database. Designing a servlet database application. When you write a servlet database application there are a number of design decisions you will need to make - such as:
Using EJB Servers. EJB servers are most beneficial if you are building an on-line transaction application. For applications that are strictly reporting or OLAP, you may find an EJB server may not as important. Connections, Drivers, and User IDs. If you decide to use EJBs, many of the connection questions are taken care of by the EJB server - one of their advantages. EJB servers typically use connection pooling software to manage connections and provide setup tools or XML configuration files to manage JDBC drivers, connection URLs and passwords. Servlet-only database applications. You can also use a connection pool manager with servlet-only database applications. Also, take a look at the preface and this tutorial from Java-beans.de on building a servlet-database application. Skyline Blvd provides a training and testing tool servlet called AdhocSQL. |
|||||||||||
| Much JDBC material is to be found after you download a JDBC driver. It is usually worth your while to look at vendor specific training material since JDBC drivers ar likely to have some idiosyncracies about how you need to use them. | |||||||||||
| IBM DB2 | In Java Support in DB2 IBM provides some examples for using DB2. | ||||||||||
| Informix | See especially Working with JDBC by Jaya Khera (2001). See also the Informix by Example pages, and this online copy of the Informix JDBC Driver Programming Guide (1998). | ||||||||||
| MySQL | For instance, You can find tutorial information for MySQL here. TECHVA U. Geneva's Java CoffeeBreak provides a number of MySQL tutorial and connection examles. | ||||||||||
| Oracle | Oracle Technet provides several sample JDBC programs. The University of Pennsylvania CIS 550 course covers the Oracle JDBC driver and has discussion on Oracle cursors that show how to use the Oracle ROWID and RefCursor extensions. For Oracle 8i specific examples see Oracle 8i, Java, and the Mustang Index, Ken North, WebTechniques, Nov 1999. And for using JDeveloper, see Oracle 8i and JDeveloper Suilte by Ken North, WebTechniques, Dec 1999. | ||||||||||
| PostgreSQL | PostgreSQL Chapter 58. JDBC Interface Peter T. Mount | ||||||||||
| BEA jdbcKona | Using the jdbcKona Drivers for Weblogic 5.0 explains basic setup, shows PreparedStatement and CallableStatement examples and extended SQL examples using BEA's jdbcKona drivers for Oracle and SQL Server. | ||||||||||
| SQLJ provides Embedded SQL functionality for the Java language. Embedded SQL provides a way to bind a Database
server procedure so that no interpretation is necessary and the SQL execution plan is known to the server ahead
of time. To get started see: SQL Embedded in Java by Ekkehard Rohwedder, JDJ May 2000. ACM SIGMOD 98 presents some learning materials and tutorials on SQLJ here. IBM provides some DB2 examples in DB2 UDB SQLJ Support. |
|||||||||||
| Highly Recommended: If you're serious about JDBC, I also recommend that you get the JDBC Developers Resource, 2nd Ed. by Art Taylor. Not exactly bed time reading, but it provides a complete reference to JDBC 1.0 with examples throughout. There is also extensive JDBC coverage in Java Enterprise in a Nutshell by William Crawford and Jim Farley, O'Reilly 1999. | |||||||||||
|
|
|||||||||||