Documentation

The Java™ Tutorials
Hide TOC
Establishing a Connection建立联接
Trail: JDBC Database Access
Lesson: JDBC Basics

Establishing a Connection建立联系

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应用程序使用以下两个类之一连接到目标数据源:

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:本页涵盖以下主题:

Using the DriverManager Class使用DriverManager类

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示例:

  1. MySQL: jdbc:mysql://localhost:3306/, where localhost is the name of the server hosting your database, and 3306 is the port numberMySQL:jdbc:mysql://localhost:3306/,其中localhost是承载数据库的服务器的名称,3306是端口号

  2. Java DB: 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.javadb: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:

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.通过此对象查询数据库。

Specifying Database Connection URLs指定数据库连接URL

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指定。

Java DB Database Connection URLsJavaDB数据库连接URL

The following is the database connection URL syntax for Java DB:以下是Java DB的数据库连接URL语法:

jdbc:derby:[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参考手册

MySQL Connector/J Database URLMySQL连接器/J数据库URL

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]...

See MySQL Reference Manual for more information.有关更多信息,请参阅《MySQL参考手册》。


Previous page: Processing SQL Statements with JDBC
Next page: Connecting with DataSource Objects