The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
The JDBC API is a Java API that can access any kind of tabular data, especially data stored in a Relational Database.JDBCAPI是一种Java API,可以访问任何类型的表格数据,特别是存储在关系数据库中的数据。
JDBC helps you to write Java applications that manage these three programming activities:JDBC帮助您编写Java应用程序来管理以下三种编程活动:
The following simple code fragment gives a simple example of these three steps:以下简单代码片段给出了这三个步骤的简单示例:
public void connectToAndQueryDatabase(String username, String password) { Connection con = DriverManager.getConnection( "jdbc:myDriver:myDatabase", username, password); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1"); while (rs.next()) { int x = rs.getInt("a"); String s = rs.getString("b"); float f = rs.getFloat("c"); } }
This short code fragment instantiates a 这个简短的代码片段实例化一个DriverManager
object to connect to a database driver and log into the database, instantiates a Statement
object that carries your SQL language query to the database; instantiates a ResultSet
object that retrieves the results of your query, and executes a simple while
loop, which retrieves and displays those results.DriverManager
对象以连接到数据库驱动程序并登录到数据库,实例化一个Statement
对象,该对象将SQL语言查询传递到数据库;实例化检索查询结果的ResultSet
对象,并执行一个简单的while
循环,该循环检索并显示这些结果。It's that simple.就这么简单。
JDBC includes four components:JDBC包括四个组件:
The JDBC APIJDBC API — The JDBC™ API provides programmatic access to relational data from the Java™ programming language.JDBC™API提供了从Java™编程语言对关系数据的编程访问。Using the JDBC API, applications can execute SQL statements, retrieve results, and propagate changes back to an underlying data source.使用JDBC API,应用程序可以执行SQL语句、检索结果并将更改传播回底层数据源。The JDBC API can also interact with multiple data sources in a distributed, heterogeneous environment.JDBCAPI还可以在分布式异构环境中与多个数据源交互。
The JDBC API is part of the Java platform, which includes the Java™ Standard Edition (Java™ SE ) and the Java™ Enterprise Edition (Java™ EE).JDBC API是Java平台的一部分,它包括Java标准版(Java SE™)和Java™企业版(Java EE™)。The JDBC 4.0 API is divided into two packages: JDBC 4.0 API分为两个包:java.sql
and javax.sql.
java.sql
和javax.sql
。Both packages are included in the Java SE and Java EE platforms.这两个包都包含在Java SE和Java EE平台中。
JDBC Driver Manager驱动程序管理器 — The JDBC JDBC DriverManager
class defines objects which can connect Java applications to a JDBC driver.DriverManager
类定义可以将Java应用程序连接到JDBC驱动程序的对象。DriverManager
has traditionally been the backbone of the JDBC architecture.DriverManager
传统上是JDBC体系结构的主干。It is quite small and simple.它很小也很简单。
The Standard Extension packages 标准扩展包javax.naming
and javax.sql
let you use a DataSource
object registered with a Java Naming and Directory Interface™ (JNDI) naming service to establish a connection with a data source.javax.naming
和javax.sql
允许您使用向Java命名和目录接口™(JNDI)命名服务注册的数据源对象来建立与数据源的连接。You can use either connecting mechanism, but using a 您可以使用任何一种连接机制,但建议尽可能使用DataSource
object is recommended whenever possible.DataSource
对象。
JDBC Test SuiteJDBC测试套件 — The JDBC driver test suite helps you to determine that JDBC drivers will run your program.JDBC驱动程序测试套件帮助您确定JDBC驱动程序将运行您的程序。These tests are not comprehensive or exhaustive, but they do exercise many of the important features in the JDBC API.这些测试并不全面或详尽,但它们确实使用了JDBC API中的许多重要特性。
JDBC-ODBC Bridge桥 — The Java Software bridge provides JDBC access via ODBC drivers.Java软件桥通过ODBC驱动程序提供JDBC访问。Note that you need to load ODBC binary code onto each client machine that uses this driver.请注意,您需要将ODBC二进制代码加载到使用此驱动程序的每台客户机上。As a result, the ODBC driver is most appropriate on a corporate network where client installations are not a major problem, or for application server code written in Java in a three-tier architecture.因此,ODBC驱动程序最适用于客户机安装不是主要问题的公司网络,或者适用于三层体系结构中用Java编写的应用程序服务器代码。
This Trail uses the first two of these four JDBC components to connect to a database and then build a java program that uses SQL commands to communicate with a test Relational Database.本教程使用这四个JDBC组件中的前两个组件连接到数据库,然后构建一个java程序,该程序使用SQL命令与测试关系数据库通信。The last two components are used in specialized environments to test web applications, or to communicate with ODBC-aware DBMSs.最后两个组件在专用环境中用于测试web应用程序,或与支持ODBC的DBMS通信。