Documentation

The Java™ Tutorials
Hide TOC
Getting Started起步
Trail: JDBC Database Access
Lesson: JDBC Basics

Getting Started起步

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开发环境,您可以使用该环境编译和运行教程示例:

  1. Install the latest version of the Java SE SDK on your computer在您的计算机上安装最新版本的Java SE SDK
  2. Install your database management system (DBMS) if needed如果需要,请安装数据库管理系统(DBMS)
  3. Install a JDBC driver from the vendor of your database从数据库供应商处安装JDBC驱动程序
  4. Install安装Apache Ant
  5. Install安装Apache Xalan
  6. Download the sample code下载示例代码
  7. Modify the build.xml file修改build.xml文件
  8. Modify the tutorial properties file修改教程属性文件
  9. Compile and package the samples编译并打包样本
  10. Create databases, tables, and populate tables创建数据库、表和填充表
  11. Run the samples运行样本

Install the latest version of the Java SE SDK on your computer在您的计算机上安装最新版本的Java SE SDK

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 bin directory is in your PATH environment variable so that you can run the Java compiler and the Java application launcher from any directory.确保Java SE SDK bin目录的完整目录路径位于PATH环境变量中,以便可以从任何目录运行Java编译器和Java应用程序启动器。

Install your database management system (DBMS) if needed如果需要,请安装数据库管理系统(DBMS)

This tutorial has been tested for the following DBMS:本教程已针对以下DBMS进行了测试:

Note that if you are using another DBMS, you might have to alter the code of the tutorial samples.请注意,如果您正在使用另一个DBMS,则可能必须更改教程示例的代码。

Install a JDBC driver from the vendor of your database从数据库供应商处安装JDBC驱动程序

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:这些实现分类如下:

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.通常不需要其他特殊配置。

Install安装Apache Ant

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:

https://ant.apache.org/

Ensure that the Apache Ant executable file is in your PATH environment variable so that you can run it from any directory.确保Apache Ant可执行文件位于PATH环境变量中,以便可以从任何目录运行它。

Install安装Apache Xalan

The sample RSSFeedsTable.java, which is described in Using SQLXML Objects, requires Apache Xalan if your DBMS is Java DB.如果您的DBMS是java DB,则使用SQLXML对象中描述的示例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/

Download the sample code下载示例代码

The sample code, JDBCTutorial.zip, consists of the following files:示例代码JDBCTutorial.zip由以下文件组成:

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 JDBCTutorial.zip into <JDBC tutorial directory>.请将JDBCTutorial.zip的内容解压缩到<JDBC tutorial directory>

Modify the build.xml file修改build.xml文件

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.xmlproperties/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.xmlproperties/mysql-sample-properties.xml包含示例所需的属性。

Modify these XML files as follows:按如下方式修改这些XML文件:

Modify修改build.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.xmlproperties/mysql-build-properties.xml,具体取决于您的DBMS。For example, if you are using Java DB, your build.xml file would contain this:例如,如果您使用的是Java DB,则build.xml文件将包含以下内容:

<property
  name="ANTPROPERTIES"
  value="properties/javadb-build-properties.xml"/>

  <import file="${ANTPROPERTIES}"/>

Similarly, if you are using MySQL, your build.xml file would contain this:类似地,如果您使用的是MySQL,build.xml文件将包含以下内容:

<property
  name="ANTPROPERTIES"
  value="properties/mysql-build-properties.xml"/>

  <import file="${ANTPROPERTIES}"/>

Modify database-specific properties file修改特定于数据库的属性文件

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.xmlproperties/mysql-build-properties.xml中(取决于您的DBMS),修改以下属性,如下表所述:

Property属性 Description描述
JAVAC The full path name of your Java compiler,Java编译器的完整路径名,javac
JAVA The full path name of your Java runtime executable,Java运行时可执行文件的完整路径名,java
PROPERTIESFILE The name of the properties file, either properties/javadb-sample-properties.xml or properties/mysql-sample-properties.xml属性文件的名称,properties/javadb-sample-properties.xmlproperties/mysql-sample-properties.xml
MYSQLDRIVER The full path name of your MySQL driver.MySQL驱动程序的完整路径名。For Connector/J, this is typically <Connector/J installation directory>/mysql-connector-java-version-number.jar.对于Connector/J,这通常是<Connector/J installation directory>/mysql-connector-java-version-number.jar
JAVADBDRIVER The full path name of your Java DB driver.Java DB驱动程序的完整路径名。This is typically <Java DB installation directory>/lib/derby.jar.这通常是<Java DB installation directory>/lib/derby.jar
XALANDIRECTORY The full path name of the directory that contains Apache Xalan.包含Apache Xalan的目录的完整路径名。
CLASSPATH The class path that the JDBC tutorial uses.JDBC教程使用的类路径。You do not need to change this value.您不需要更改此值。
XALAN The full path name of the file xalan.jar.文件xalan.jar的完整路径名。
DB.VENDOR A value of either derby or mysql depending on whether you are using Java DB or MySQL, respectively.derbymysql的值取决于您分别使用的是Java DB还是mysql。The tutorial uses this value to construct the URL required to connect to the DBMS and identify DBMS-specific code and SQL statements.本教程使用此值构造连接到DBMS所需的URL,并标识DBMS特定的代码和SQL语句。
DB.DRIVER The fully qualified class name of the JDBC driver.JDBC驱动程序的完全限定类名。For Java DB, this is org.apache.derby.jdbc.EmbeddedDriver.对于Java DB,这是org.apache.derby.jdbc.EmbeddedDriverFor MySQL, this is com.mysql.cj.jdbc.Driver.对于MySQL,这是com.MySQL.cj.jdbc.Driver
DB.HOST The host name of the computer hosting your DBMS.托管DBMS的计算机的主机名。
DB.PORT The port number of the computer hosting your DBMS.托管DBMS的计算机的端口号。
DB.SID The name of the database the tutorial creates and uses.教程创建和使用的数据库的名称。
DB.URL.NEWDATABASE The connection URL used to connect to your DBMS when creating a new database.创建新数据库时用于连接到DBMS的连接URL。You do not need to change this value.您不需要更改此值。
DB.URL The connection URL used to connect to your DBMS.用于连接到DBMS的连接URL。You do not need to change this value.您不需要更改此值。
DB.USER The name of the user that has access to create databases in the DBMS.有权在DBMS中创建数据库的用户的名称。
DB.PASSWORD The password of the user specified in DB.USER.DB.USER中指定的用户的密码。
DB.DELIMITER The character used to separate SQL statements.用于分隔SQL语句的字符。Do not change this value.不要更改此值。 It should be the semicolon character (;).它应该是分号字符(;)。

Modify the tutorial properties file修改教程属性文件

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 A value of either derby or mysql depending on whether you are using Java DB or MySQL, respectively.derbymysql的值取决于您分别使用的是Java DB还是mysql。The tutorial uses this value to construct the URL required to connect to the DBMS and identify DBMS-specific code and SQL statements.本教程使用此值构造连接到DBMS所需的URL,并标识DBMS特定的代码和SQL语句。
jar_file The full path name of the JAR file that contains all the class files of this tutorial.包含本教程所有类文件的JAR文件的完整路径名。
driver The fully qualified class name of the JDBC driver.JDBC驱动程序的完全限定类名。For Java DB, this is org.apache.derby.jdbc.EmbeddedDriver.对于Java DB,这是org.apache.derby.jdbc.EmbeddedDriverFor MySQL, this is com.mysql.cj.jdbc.Driver.对于MySQL,这是com.MySQL.cj.jdbc.Driver
database_name The name of the database the tutorial creates and uses.教程创建和使用的数据库的名称。
user_name The name of the user that has access to create databases in the DBMS.有权在DBMS中创建数据库的用户的名称。
password The password of the user specified in user_name.user_name中指定的用户的密码。
server_name The host name of the computer hosting your DBMS.托管DBMS的计算机的主机名。
port_number The port number of the computer hosting your DBMS.托管DBMS的计算机的端口号。

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数据库安全指南》中的“在应用程序设计中保护密码”一节。

Compile and package the samples编译并打包样本

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

Create databases, tables, and populate tables创建数据库、表和填充表

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 build.xml file that creates a database for Java DB.:为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 ant setup every time before you run one of the Java classes in the sample.:您应该在每次运行示例中的一个Java类之前运行命令ant setup。Many of these samples expect specific data in the contents of the sample's database tables.这些示例中的许多都希望在示例的数据库表的内容中包含特定的数据。

Run the samples运行样本

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 Class or SQL Script类或SQL脚本 Other Required Classes or Files其他必需的类或文件
javadb-create-procedure javadb/create-procedures.sql; see the build.xml file to view other SQL statements that are run; 请参阅build.xml文件以查看正在运行的其他SQL语句 No other required files没有其他需要的文件
mysql-create-procedure mysql/create-procedures.sql. No other required files没有其他需要的文件
run JDBCTutorialUtilities No other required classes没有其他需要的类
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, the XML files contained in the xml directory,包含在xml目录中的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

Previous page: JDBC Basics
Next page: Processing SQL Statements with JDBC