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 sample code that comes with this tutorial creates a database that is used by a proprietor of a small coffee house called The Coffee Break, where coffee beans are sold by the pound and brewed coffee is sold by the cup.本教程附带的示例代码创建了一个数据库,供一家名为“咖啡休息”的小咖啡馆的老板使用,咖啡豆按磅出售,煮好的咖啡按杯出售。
The following steps configure a JDBC development environment with which you can compile and run the tutorial samples:以下步骤配置JDBC开发环境,您可以使用该环境编译和运行教程示例:
build.xml
filebuild.xml
文件Install the latest version of the Java SE SDK on your computer.在您的计算机上安装最新版本的Java SE SDK。
Ensure that the full directory path of the Java SE SDK 确保Java SE SDK bin
directory is in your PATH
environment variable so that you can run the Java compiler and the Java application launcher from any directory.bin
目录的完整目录路径位于PATH
环境变量中,以便可以从任何目录运行Java编译器和Java应用程序启动器。
This tutorial has been tested for the following DBMS:本教程已针对以下DBMS进行了测试:
Note注意: Java DB is no longer included in recent versions of the JDK.:Java DB不再包含在JDK的最新版本中。Java DB was a rebranding of Apache Derby.Java DB是Apache Derby的翻版。If you would like to use Java DB, then download the latest version from The Apache DB Project.如果您想使用JavaDB,请从Apache DB项目下载最新版本。
Note that if you are using another DBMS, you might have to alter the code of the tutorial samples.请注意,如果您正在使用另一个DBMS,则可能必须更改教程示例的代码。
If you are using Java DB, it already comes with a JDBC driver.如果您使用的是JavaDB,那么它已经附带了JDBC驱动程序。If you are using MySQL, install the latest version of the JDBC driver for MySQL, Connector/J.如果您使用的是MySQL,请安装最新版本的MySQL JDBC驱动程序Connector/J。
Contact the vendor of your database to obtain a JDBC driver for your DBMS.请与数据库供应商联系,以获取用于DBMS的JDBC驱动程序。
There are many possible implementations of JDBC drivers.JDBC驱动程序有许多可能的实现。These implementations are categorized as follows:这些实现分类如下:
Type 1: Drivers that implement the JDBC API as a mapping to another data access API, such as ODBC (Open Database Connectivity).:将JDBC API实现为另一个数据访问API映射的驱动程序,如ODBC(开放数据库连接)。Drivers of this type are generally dependent on a native library, which limits their portability.这种类型的驱动程序通常依赖于本机库,这限制了它们的可移植性。The JDBC-ODBC Bridge is an example of a Type 1 driver.JDBC-ODBC网桥是类型1驱动程序的一个示例。
Note: The JDBC-ODBC Bridge should be considered a transitional solution.:JDBC-ODBC网桥应被视为过渡解决方案。It is not supported by Oracle.Oracle不支持它。Consider using this only if your DBMS does not offer a Java-only JDBC driver.只有在DBMS不提供java的JDBC驱动程序时,才考虑使用此方法。
Type 2: Drivers that are written partly in the Java programming language and partly in native code.:部分用Java编程语言编写,部分用本机代码编写的驱动程序。These drivers use a native client library specific to the data source to which they connect.这些驱动程序使用特定于它们所连接的数据源的本机客户端库。Again, because of the native code, their portability is limited.同样,由于本机代码,它们的可移植性受到限制。Oracle's OCI (Oracle Call Interface) client-side driver is an example of a Type 2 driver.Oracle的OCI(Oracle调用接口)客户端驱动程序就是类型2驱动程序的一个示例。
Type 3: Drivers that use a pure Java client and communicate with a middleware server using a database-independent protocol.:使用纯Java客户端并使用独立于数据库的协议与中间件服务器通信的驱动程序。The middleware server then communicates the client's requests to the data source.然后,中间件服务器将客户机的请求传递给数据源。
Type 4: Drivers that are pure Java and implement the network protocol for a specific data source.:纯Java驱动程序,用于实现特定数据源的网络协议。The client connects directly to the data source.客户端直接连接到数据源。
Check which driver types comes with your DBMS. Java DB comes with two Type 4 drivers, an Embedded driver and a Network Client Driver.检查DBMS附带的驱动程序类型。Java DB附带两个类型4驱动程序,一个嵌入式驱动程序和一个网络客户端驱动程序。MySQL Connector/J is a Type 4 driver.MySQL连接器/J是一个4型驱动程序。
Installing a JDBC driver generally consists of copying the driver to your computer, then adding the location of it to your class path.安装JDBC驱动程序通常包括将驱动程序复制到计算机上,然后将其位置添加到类路径中。In addition, many JDBC drivers other than Type 4 drivers require you to install a client-side API.此外,除Type4驱动程序外,许多JDBC驱动程序都要求您安装客户端API。No other special configuration is usually needed.通常不需要其他特殊配置。
These steps use Apache Ant, a Java-based tool, to build, compile, and run the JDBC tutorial samples.这些步骤使用基于Java的工具ApacheAnt来构建、编译和运行JDBC教程示例。Go to the following link to download Apache Ant:转到以下链接下载Apache Ant:
Ensure that the Apache Ant executable file is in your 确保Apache Ant可执行文件位于PATH
environment variable so that you can run it from any directory.PATH
环境变量中,以便可以从任何目录运行它。
The sample 如果您的DBMS是java DB,则使用SQLXML对象中描述的示例RSSFeedsTable.java
, which is described in Using SQLXML Objects, requires Apache Xalan if your DBMS is Java DB.RSSFeedsTable.java
需要Apache Xalan。The sample uses Apache Xalan-Java. Go to the following link to download it:该示例使用ApacheXalanJava。请转到以下链接下载:
https://xml.apache.org/xalan-j/
The sample code, 示例代码
, consists of the following files:JDBCTutorial.zip
由以下文件组成:JDBCTutorial.zip
properties
javadb-build-properties.xml
javadb-sample-properties.xml
mysql-build-properties.xml
mysql-sample-properties.xml
sql
javadb
create-procedures.sql
create-tables.sql
drop-tables.sql
populate-tables.sql
mysql
create-procedures.sql
create-tables.sql
drop-tables.sql
populate-tables.sql
src/com/oracle/tutorial/jdbc
CachedRowSetSample.java
CityFilter.java
ClobSample.java
CoffeesFrame.java
CoffeesTable.java
CoffeesTableModel.java
DatalinkSample.java
ExampleRowSetListener.java
FilteredRowSetSample.java
JdbcRowSetSample.java
JDBCTutorialUtilities.java
JoinSample.java
ProductInformationTable.java
RSSFeedsTable.java
StateFilter.java
StoredProcedureJavaDBSample.java
StoredProcedureMySQLSample.java
SuppliersTable.java
WebRowSetSample.java
txt
colombian-description.txt
xml
rss-coffee-industry-news.xml
rss-the-coffee-break-blog.xml
build.xml
Create a directory to contain all the files of the sample.创建一个目录以包含示例的所有文件。These steps refer to this directory as 这些步骤将此目录称为<JDBC tutorial directory>
.<JDBC tutorial directory>
。Unzip the contents of 请将
into JDBCTutorial.zip
<JDBC tutorial directory>
.
的内容解压缩到JDBCTutorial.zip
<JDBC tutorial directory>
。
The build.xml
file is the build file that Apache Ant uses to compile and execute the JDBC samples.build.xml
文件是Apache Ant用来编译和执行JDBC示例的构建文件。The files 文件properties/javadb-build-properties.xml
and properties/mysql-build-properties.xml
contain additional Apache Ant properties required for Java DB and MySQL, respectively.properties/javadb-build-properties.xml
和properties/mysql-build-properties.xml
分别包含Java DB和mysql所需的其他Apache Ant属性。The files 文件properties/javadb-sample-properties.xml
and properties/mysql-sample-properties.xml
contain properties required by the sample.properties/javadb-sample-properties.xml
和properties/mysql-sample-properties.xml
包含示例所需的属性。
Modify these XML files as follows:按如下方式修改这些XML文件:
In the 在build.xml
file, modify the property ANTPROPERTIES
to refer to either properties/javadb-build-properties.xml
or properties/mysql-build-properties.xml
, depending on your DBMS.build.xml
文件中,修改属性ANTPROPERTIES
以引用properties/javadb-build-properties.xml
或properties/mysql-build-properties.xml
,具体取决于您的DBMS。For example, if you are using Java DB, your 例如,如果您使用的是Java DB,则build.xml
file would contain this:build.xml
文件将包含以下内容:
<property name="ANTPROPERTIES" value="properties/javadb-build-properties.xml"/> <import file="${ANTPROPERTIES}"/>
Similarly, if you are using MySQL, your 类似地,如果您使用的是MySQL,build.xml
file would contain this:build.xml
文件将包含以下内容:
<property name="ANTPROPERTIES" value="properties/mysql-build-properties.xml"/> <import file="${ANTPROPERTIES}"/>
In the 在文件properties/javadb-build-properties.xml
or properties/mysql-build-properties.xml
file (depending on your DBMS), modify the following properties, as described in the following table:properties/javadb-build-properties.xml
或properties/mysql-build-properties.xml
中(取决于您的DBMS),修改以下属性,如下表所述:
JAVAC |
javac |
JAVA |
java |
PROPERTIESFILE |
properties/javadb-sample-properties.xml or properties/mysql-sample-properties.xml properties/javadb-sample-properties.xml 或properties/mysql-sample-properties.xml |
MYSQLDRIVER |
<Connector/J installation directory>/mysql-connector-java-version-number.jar .<Connector/J installation directory>/mysql-connector-java-version-number.jar 。 |
JAVADBDRIVER |
<Java DB installation directory>/lib/derby.jar .<Java DB installation directory>/lib/derby.jar 。 |
XALANDIRECTORY |
|
CLASSPATH |
|
XALAN |
xalan.jar .xalan.jar 的完整路径名。 |
DB.VENDOR |
derby or mysql depending on whether you are using Java DB or MySQL, respectively.derby 或mysql 的值取决于您分别使用的是Java DB还是mysql。 |
DB.DRIVER |
org.apache.derby.jdbc.EmbeddedDriver .org.apache.derby.jdbc.EmbeddedDriver 。com.mysql.cj.jdbc.Driver .com.MySQL.cj.jdbc.Driver 。 |
DB.HOST |
|
DB.PORT |
|
DB.SID |
|
DB.URL.NEWDATABASE |
|
DB.URL |
|
DB.USER |
|
DB.PASSWORD |
DB.USER .DB.USER 中指定的用户的密码。 |
DB.DELIMITER |
; ).; )。 |
The tutorial samples use the values in either the 教程示例使用properties/javadb-sample-properties.xml
file or properties/mysql-sample-properties.xml
file (depending on your DBMS) to connect to the DBMS and initialize databases and tables, as described in the following table:properties/javadb-sample-properties.xml
文件或properties/mysql-sample-properties.xml
文件(取决于您的DBMS)中的值连接到DBMS并初始化数据库和表,如下表所述:
Property | Description |
---|---|
dbms |
derby or mysql depending on whether you are using Java DB or MySQL, respectively.derby 或mysql 的值取决于您分别使用的是Java DB还是mysql。 |
jar_file |
|
driver |
org.apache.derby.jdbc.EmbeddedDriver .org.apache.derby.jdbc.EmbeddedDriver 。com.mysql.cj.jdbc.Driver .com.MySQL.cj.jdbc.Driver 。 |
database_name |
|
user_name |
|
password |
user_name .user_name 中指定的用户的密码。 |
server_name |
|
port_number |
Note注意: For simplicity in demonstrating the JDBC API, the JDBC tutorial sample code does not perform the password management techniques that a deployed system normally uses.:为了简化JDBC API的演示,JDBC教程示例代码不执行已部署系统通常使用的密码管理技术。In a production environment, you can follow the Oracle Database password management guidelines and disable any sample accounts.在生产环境中,您可以遵循Oracle数据库密码管理指南并禁用任何示例帐户。See the section Securing Passwords in Application Design in Oracle Database Security Guide for password management guidelines and other security recommendations.有关密码管理指南和其他安全建议,请参阅《Oracle数据库安全指南》中的“在应用程序设计中保护密码”一节。
At a command prompt, change the current directory to 在命令提示下,将当前目录更改为<JDBC tutorial directory>
.<JDBC tutorial directory>
。From this directory, run the following command to compile the samples and package them in a jar file:在此目录中,运行以下命令编译示例并将其打包到jar文件中:
ant jar
If you are using MySQL, then run the following command to create a database:如果您使用的是MySQL,请运行以下命令创建数据库:
ant create-mysql-database
Note注意: No corresponding Ant target exists in the :为Java DB创建数据库的build.xml
file that creates a database for Java DB.build.xml
文件中不存在相应的Ant目标。The database URL for Java DB, which is used to establish a database connection, includes the option to create the database (if it does not already exist).用于建立数据库连接的Java DB的数据库URL包括创建数据库的选项(如果数据库不存在)。See Establishing a Connection for more information.有关更多信息,请参阅建立连接。
If you are using either Java DB or MySQL, then from the same directory, run the following command to delete existing sample database tables, recreate the tables, and populate them.如果您使用的是JavaDB或MySQL,那么在同一目录中,运行以下命令删除现有的示例数据库表,重新创建表并填充它们。For Java DB, this command also creates the database if it does not already exist:对于Java DB,如果数据库不存在,此命令还会创建该数据库:
ant setup
Note注意: You should run the command :您应该在每次运行示例中的一个Java类之前运行命令ant setup。ant setup
every time before you run one of the Java classes in the sample.Many of these samples expect specific data in the contents of the sample's database tables.这些示例中的许多都希望在示例的数据库表的内容中包含特定的数据。
Each target in the build.xml
file corresponds to a Java class or SQL script in the JDBC samples.build.xml
文件中的每个目标对应于JDBC示例中的Java类或SQL脚本。The following table lists the targets in the 下表列出build.xml
file, which class or script each target executes, and other classes or files each target requires:build.xml
文件中的目标,每个目标执行哪个类或脚本,以及每个目标需要的其他类或文件:
Ant Target | ||
---|---|---|
javadb-create-procedure |
javadb/create-procedures.sql build.xml file to view other SQL statements that are runbuild.xml 文件以查看正在运行的其他SQL语句 |
|
mysql-create-procedure |
mysql/create-procedures.sql . |
|
run |
JDBCTutorialUtilities |
|
runct |
CoffeesTable |
JDBCTutorialUtilities |
runst |
SuppliersTable |
JDBCTutorialUtilities |
runjrs |
JdbcRowSetSample |
JDBCTutorialUtilities |
runcrs |
CachedRowSetSample , ExampleRowSetListener |
JDBCTutorialUtilities |
runjoin |
JoinSample |
JDBCTutorialUtilities |
runfrs |
FilteredRowSetSample |
JDBCTutorialUtilities , CityFilter , StateFilter |
runwrs |
WebRowSetSample |
JDBCTutorialUtilities |
runclob |
ClobSample |
JDBCTutorialUtilities , txt/colombian-description.txt |
runrss |
RSSFeedsTable |
JDBCTutorialUtilities xml directoryxml 目录中的XML文件 |
rundl |
DatalinkSample |
JDBCTutorialUtilities |
runspjavadb |
StoredProcedureJavaDBSample |
JDBCTutorialUtilities , SuppliersTable , CoffeesTable |
runspmysql |
StoredProcedureMySQLSample |
JDBCTutorialUtilities , SuppliersTable , CoffeesTable |
runframe |
CoffeesFrame |
JDBCTutorialUtilities , CoffeesTableModel |
For example, to run the class 例如,要运行CoffeesTable
, change the current directory to <JDBC tutorial directory>
, and from this directory, run the following command:CoffeesTable
类,请将当前目录更改为<JDBC tutorial directory>
,并在此目录中运行以下命令:
ant runct