Java Mysql 简明教程

Java & MySQL - Overview

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.

从根本上说,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.

Pre-Requisite

在继续下一步之前,你需要对以下两个主题有深入的了解:

Before moving further, you need to have a good understanding of the following two subjects −

JDBC Architecture

JDBC API 支持两层和三层数据库访问处理模型,但一般来说 JDBC 架构由两层组成:

The JDBC API supports both two-tier and three-tier processing models for database access but in general, JDBC Architecture consists of two layers −

  1. JDBC API − This provides the application-to-JDBC Manager connection.

  2. JDBC Driver API − This supports the JDBC Manager-to-Driver Connection.

JDBC API 使用驱动程序管理器和特定于数据库的驱动程序来提供与异构数据库之间的透明连接。

The JDBC API uses a driver manager and database-specific drivers to provide transparent connectivity to heterogeneous databases.

JDBC 驱动程序管理器可以确保使用正确的驱动程序来访问每个数据源。驱动程序管理器能够支持连接到多个异构数据库的多个并发驱动程序。

The JDBC driver manager ensures that the correct driver is used to access each data source. The driver manager is capable of supporting multiple concurrent drivers connected to multiple heterogeneous databases.

以下是架构图,显示驱动程序管理器相对于 JDBC 驱动程序和 Java 应用程序的位置:

Following is the architectural diagram, which shows the location of the driver manager with respect to the JDBC drivers and the Java application −

jdbc architecture

Common JDBC Components

JDBC API 提供了以下接口和类:

The JDBC API provides the following interfaces and classes −

  1. DriverManager − This class manages a list of database drivers. Matches connection requests from the java application with the proper database driver using communication sub protocol. The first driver that recognizes a certain subprotocol under JDBC will be used to establish a database Connection.

  2. Driver − This interface handles the communications with the database server. You will interact directly with Driver objects very rarely. Instead, you use DriverManager objects, which manages objects of this type. It also abstracts the details associated with working with Driver objects.

  3. Connection − This interface with all methods for contacting a database. The connection object represents communication context, i.e., all communication with database is through connection object only.

  4. Statement − You use objects created from this interface to submit the SQL statements to the database. Some derived interfaces accept parameters in addition to executing stored procedures.

  5. ResultSet − These objects hold data retrieved from a database after you execute an SQL query using Statement objects. It acts as an iterator to allow you to move through its data.

  6. SQLException − This class handles any errors that occur in a database application.

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.