Jdbc 简明教程

JDBC Tutorial

What is JDBC?

JDBC API 是一种 Java API,可以访问任何种类的表格数据,尤其是存储在关系数据库中的数据。JDBC 可与各种平台(如 Windows、Mac OS 和各种版本的 UNIX)上的 Java 配合使用。

JDBC API is a Java API that can access any kind of tabular data, especially data stored in a Relational Database. JDBC works with Java on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

Why to Learn JDBC?

JDBC 全称 *J*ava *D*ata*b*ase *C*onnectivity,它是用于 Java 编程语言和广泛数据库之间的与数据库无关的连接的标准 Java API。

JDBC stands for *J*ava *D*ata*b*ase *C*onnectivity, which is a standard Java API for database-independent connectivity between the Java programming language and a wide range of databases.

JDBC 库包括用于以下任务的 API,这些任务通常与数据库使用有关。

The JDBC library includes APIs for each of the tasks mentioned below that are commonly associated with database usage.

  1. Making a connection to a database.

  2. Creating SQL or MySQL statements.

  3. Executing SQL or MySQL queries in the database.

  4. Viewing & Modifying the resulting records.

Applications of JDBC

从根本上说,JDBC 是一套规范,它提供了全面的接口来实现对底层数据库的可移植访问。Java 可用于编写不同类型的可执行文件,比如:

Fundamentally, JDBC is a specification that provides a complete set of interfaces that allows for portable access to an underlying database. Java can be used to write different types of executables, such as −

  1. Java Applications

  2. Java Applets

  3. Java Servlets

  4. Java ServerPages (JSPs)

  5. Enterprise JavaBeans (EJBs).

所有这些不同的可执行文件都能够通过 JDBC 驱动程序来访问数据库并充分利用存储的数据。

All of these different executables are able to use a JDBC driver to access a database, and take advantage of the stored data.

JDBC 提供与 ODBC 相同的功能,允许 Java 程序包含数据库无关的代码。

JDBC provides the same capabilities as ODBC, allowing Java programs to contain database-independent code.

The JDBC 4.0 Packages

java.sql 和 javax.sql 是 JDBC 4.0 的主包。在撰写本教程时,这是最新的 JDBC 版本。它提供了用于与数据源进行交互的主要类。

The java.sql and javax.sql are the primary packages for JDBC 4.0. This is the latest JDBC version at the time of writing this tutorial. It offers the main classes for interacting with your data sources.

这些软件包中的新功能包括以下领域的变更:

The new features in these packages include changes in the following areas −

  1. Automatic database driver loading.

  2. Exception handling improvements.

  3. Enhanced BLOB/CLOB functionality.

  4. Connection and statement interface enhancements.

  5. National character set support.

  6. SQL ROWID access.

  7. SQL 2003 XML data type support.

  8. Annotations.

Interfaces and Classes of JDBC API

以下是 JDBC API 中最常用的接口和类的列表。

Following is the list of mostly used interfaces and classes in JDBC API.

  1. DriverManager class − used to load a SQL driver to connect to database.

  2. Connection interface − used to make a connection to the database using database connection string and credentials.

  3. Statement interface − used to make a query to the database.

  4. PreparedStatement interface − used for a query with placeholder values.

  5. CallableStatement interface − used to called stored procedure or functions in database.

  6. ResultSet interface − represents the query results obtained from the database.

  7. ResultSetMetaData interface − represents the metadata of the result set.

  8. BLOB class − represents binary data stored in BLOB format in database table.

  9. CLOB class − represents text data like XML stored in database table

Types of API in JDBC

JDBC 驱动程序实现因 Java 运行的各种操作系统和硬件平台而异。Sun 已将实现类型分为四种类别,即第 1、2、3 和 4 类,如下所述 −

JDBC driver implementations vary because of the wide variety of operating systems and hardware platforms in which Java operates. Sun has divided the implementation types into four categories, Types 1, 2, 3, and 4, which is explained below −

  1. Type 1 − a JDBC bridge is used to access ODBC drivers installed on each client machine. For example, JDBC-ODBC Bridge driver in JDK 1.2.

  2. Type 2 − JDBC API calls are converted into native C/C++ API calls, which are unique to the database. These APIs are vendor specific and vendor provided driver is required to be installed. It is also called JDBC Native API. For example, Oracle Call Interface (OCI) driver.

  3. Type 3 − A three-tier approach is used to access databases. The JDBC clients use standard network sockets to communicate with a middleware application server. The socket information is then translated by the middleware application server into the call format required by the DBMS, and forwarded to the database server. It is also called JDBC-Net Pure Java driver.

  4. Type 4 − A pure Java-based driver communicates directly with the vendor’s database through socket connection. This is the highest performance driver available for the database and is usually provided by the vendor itself. For example, MySQL’s Connector/J driver to connect to MySQL database.

Audience

本教程专为希望详细了解 JDBC 框架及其架构和实际用法这方面的 Java 程序员而设计。

This tutorial is designed for Java programmers who would like to understand the JDBC framework in detail along with its architecture and actual usage.

Prerequisites

在继续学习本教程之前,您应该很好地理解 Java 编程语言。由于您要处理 RDBMS,您应该事先了解 SQL 和数据库概念。

Before proceeding with this tutorial, you should have a good understanding of Java programming language. As you are going to deal with RDBMS, you should have prior exposure to SQL and Database concepts.