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发行说明。
First, you need to establish a connection with the data source you want to use.首先,需要与要使用的数据源建立连接。A data source can be a DBMS, a legacy file system, or some other source of data with a corresponding JDBC driver.数据源可以是DBMS、遗留文件系统,也可以是具有相应JDBC驱动程序的其他数据源。Typically, a JDBC application connects to a target data source using one of two classes:通常,JDBC应用程序使用以下两个类之一连接到目标数据源:
DriverManager
: This fully implemented class connects an application to a data source, which is specified by a database URL.:此完全实现的类将应用程序连接到由数据库URL指定的数据源。When this class first attempts to establish a connection, it automatically loads any JDBC 4.0 drivers found within the class path.当该类首次尝试建立连接时,它会自动加载在类路径中找到的任何JDBC 4.0驱动程序。Note that your application must manually load any JDBC drivers prior to version 4.0.请注意,应用程序必须手动加载4.0版之前的任何JDBC驱动程序。
DataSource
: This interface is preferred over :此接口优于DriverManager
because it allows details about the underlying data source to be transparent to your application.DriverManager
,因为它允许底层数据源的详细信息对应用程序透明。A DataSource
object's properties are set so that it represents a particular data source.DataSource
对象的属性被设置为表示特定的数据源。See Connecting with DataSource Objects for more information.有关详细信息,请参阅连接数据源对象。For more information about developing applications with the 有关使用DataSource
class, see the latest The Java EE Tutorial.DataSource
类开发应用程序的更多信息,请参阅最新的Java EE教程。
Note注意: The samples in this tutorial use the :本教程中的示例使用DriverManager
class instead of the DataSource
class because it is easier to use and the samples do not require the features of the DataSource
class.DriverManager
类而不是DataSource
类,因为它更易于使用,并且示例不需要DataSource
类的功能。
This page covers the following topics:本页涵盖以下主题:
Connecting to your DBMS with the 使用DriverManager
class involves calling the method DriverManager.getConnection
.DriverManager
类连接到DBMS需要调用DriverManager.getConnection
方法。The following method, 以下方法JDBCTutorialUtilities.getConnection
, establishes a database connection:JDBCTutorialUtilities.getConnection
用于建立数据库连接:
public Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", this.userName); connectionProps.put("password", this.password); if (this.dbms.equals("mysql")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps); } else if (this.dbms.equals("derby")) { conn = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps); } System.out.println("Connected to database"); return conn; }
The method 方法DriverManager.getConnection
establishes a database connection.DriverManager.getConnection
建立数据库连接。This method requires a database URL, which varies depending on your DBMS.此方法需要一个数据库URL,该URL因DBMS而异。The following are some examples of database URLs:以下是一些数据库URL示例:
MySQL: MySQL:jdbc:mysql://localhost:3306/
, where localhost
is the name of the server hosting your database, and 3306
is the port numberjdbc:mysql://localhost:3306/
,其中localhost
是承载数据库的服务器的名称,3306
是端口号
Java DB: javadb:jdbc:derby:testdb;create=true
, where testdb
is the name of the database to connect to, and create=true
instructs the DBMS to create the database.jdbc:derby:testdb;create=true
,其中testdb
是要连接到的数据库的名称,create=true
指示DBMS创建数据库。
Note: This URL establishes a database connection with the Java DB Embedded Driver.:此URL建立与Java DB嵌入式驱动程序的数据库连接。Java DB also includes a Network Client Driver, which uses a different URL.JavaDB还包括一个网络客户端驱动程序,它使用不同的URL。
This method specifies the user name and password required to access the DBMS with a 此方法指定使用Properties
object.Properties
对象访问DBMS所需的用户名和密码。
Note:
Typically, in the database URL, you also specify the name of an existing database to which you want to connect.通常,在数据库URL中,还可以指定要连接到的现有数据库的名称。For example, the URL 例如,URL jdbc:mysql://localhost:3306/mysql
represents the database URL for the MySQL database named mysql
.jdbc:mysql://localhost:3306/mysql
表示名为MySQL的MySQL数据库的数据库URL。The samples in this tutorial use a URL that does not specify a specific database because the samples create a new database.本教程中的示例使用的URL不指定特定数据库,因为示例创建了一个新数据库。
In previous versions of JDBC, to obtain a connection, you first had to initialize your JDBC driver by calling the method 在以前版本的JDBC中,要获得连接,首先必须通过调用Class.forName
.Class.forName
方法初始化JDBC驱动程序。This methods required an object of type 此方法需要java.sql.Driver
.java.sql.Driver
类型的对象。Each JDBC driver contains one or more classes that implements the interface 每个JDBC驱动程序都包含一个或多个实现接口java.sql.Driver
.java.sql.driver
的类。The drivers for Java DB are Java DB的驱动程序是org.apache.derby.jdbc.EmbeddedDriver
and org.apache.derby.jdbc.ClientDriver
, and the one for MySQL Connector/J is com.mysql.cj.jdbc.Driver
.org.apache.derby.jdbc.EmbeddedDriver
和org.apache.derby.jdbc.ClientDriver
,MySQL Connector/J的驱动程序是com.MySQL.cj.jdbc.Driver
。See the documentation of your DBMS driver to obtain the name of the class that implements the interface 请参阅DBMS驱动程序的文档,以获取实现接口java.sql.Driver
.java.sql.driver
的类的名称。
Any JDBC 4.0 drivers that are found in your class path are automatically loaded.在类路径中找到的任何JDBC4.0驱动程序都会自动加载。(However, you must manually load any drivers prior to JDBC 4.0 with the method (但是,您必须使用方法Class.forName
.)Class.forName
手动加载JDBC 4.0之前的任何驱动程序。)
The method returns a 该方法返回一个Connection
object, which represents a connection with the DBMS or a specific database.Connection
对象,该对象表示与DBMS或特定数据库的连接。Query the database through this object.通过此对象查询数据库。
A database connection URL is a string that your DBMS JDBC driver uses to connect to a database.数据库连接URL是DBMS JDBC驱动程序用于连接数据库的字符串。It can contain information such as where to search for the database, the name of the database to connect to, and configuration properties.它可以包含诸如在何处搜索数据库、要连接到的数据库的名称以及配置属性等信息。The exact syntax of a database connection URL is specified by your DBMS.数据库连接URL的确切语法由DBMS指定。
The following is the database connection URL syntax for Java DB:以下是Java DB的数据库连接URL语法:
jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*
subsubprotocol
databaseName
attribute=value
See Java DB Developer's Guide and Java DB Reference Manual from Java DB Technical Documentation for more information.有关更多信息,请参阅《Java DB技术文档》中的Java DB开发者指南和Java DB参考手册。
The following is the database connection URL syntax for MySQL Connector/J:以下是MySQL Connector/J的数据库连接URL语法:
jdbc:mysql://[host][,failoverhost...] [:port]/[database] [?propertyName1][=propertyValue1] [&propertyName2][=propertyValue2]...
host:port
host
and port
are 127.0.0.1 and 3306, respectively.127.0.0.1
和3306
。database
failover
propertyName=propertyValue
See MySQL Reference Manual for more information.有关更多信息,请参阅《MySQL参考手册》。