Java Mysql 简明教程

Java & MySQL - Connections

在您安装了相应的驱动程序后,就可以使用 JDBC 来建立一个数据库连接了。

After you’ve installed the appropriate driver, it is time to establish a database connection using JDBC.

建立 JDBC 连接的编程过程相当简单。下面是这三个简单步骤:

The programming involved to establish a JDBC connection is fairly simple. Here are these simple three steps −

  1. Import JDBC Packages − Add import statements to your Java program to import required classes in your Java code.

  2. Database URL Formulation − This is to create a properly formatted address that points to the database to which you wish to connect.

  3. Create Connection Object − Finally, code a call to the DriverManager object’s getConnection( ) method to establish actual database connection.

Import JDBC Packages

Import 语句告诉 Java 编译器在你的代码中引用哪些类,并且放在源代码的最开始位置。

The Import statements tell the Java compiler where to find the classes you reference in your code and are placed at the very beginning of your source code.

要使用标准 JDBC 软件包(它允许你选择、插入、更新和删除 SQL 表中的数据),将以下导入项添加到你的源代码:

To use the standard JDBC package, which allows you to select, insert, update, and delete data in SQL tables, add the following imports to your source code −

import java.sql.* ;  // for standard JDBC programs
import java.math.* ; // for BigDecimal and BigInteger support

Register JDBC Driver

类路径中必须有必需的 JDBC 驱动程序。在当前情况下,将 CLASSPATH 变量设置为 C:\Program Files\MySQL\mysql-connector-java-5.1.8\mysql-connector-java-5.1.8-bin.jar。你的驱动程序版本可能根据安装而有所不同。

You must have the required JDBC driver in the classpath. In current case, you set CLASSPATH variable to C:\Program Files\MySQL\mysql-connector-java-5.1.8\mysql-connector-java-5.1.8-bin.jar. Your driver version may vary based on your installation.

Database URL Formulation

在加载驱动程序后,可以使用 DriverManager.getConnection() 方法建立连接。为了便于参考,我列出三个重载的 DriverManager.getConnection() 方法:

After you’ve loaded the driver, you can establish a connection using the DriverManager.getConnection() method. For easy reference, let me list the three overloaded DriverManager.getConnection() methods −

  1. getConnection(String url)

  2. getConnection(String url, Properties prop)

  3. getConnection(String url, String user, String password)

此处每个表单都需要一个数据库 URL 。数据库 URL 是指向你的数据库的地址。

Here each form requires a database URL. A database URL is an address that points to your database.

形成数据库 URL 是与建立连接相关的大多数问题发生的地方。

Formulating a database URL is where most of the problems associated with establishing a connection occurs.

下表列出了 MySQL JDBC 驱动程序名称和数据库 URL。

Following table lists down the MySQL JDBC driver name and database URL.

RDBMS

JDBC driver name

URL format

MySQL

com.mysql.jdbc.Driver

*jdbc:mysql://*hostname/ databaseName

URL 格式中的所有高亮部分都是静态的,你只需要根据数据库设置更改其余部分。

All the highlighted part in URL format is static and you need to change only the remaining part as per your database setup.

Create Connection Object

我们列出了三种 DriverManager.getConnection() 方法来创建连接对象。

We have listed down three forms of DriverManager.getConnection() method to create a connection object.

Using a Database URL with a username and password

getConnection() 最常用的形式需要你传递一个数据库 URL、一个用户名和一个密码:

The most commonly used form of getConnection() requires you to pass a database URL, a username, and a password −

由于你正在使用 MySQL 驱动程序,因此你将为 URL 的数据库部分指定 host:port:databaseName 值。

As you are using MySQL driver, you’ll specify a host:port:databaseName value for the database portion of the URL.

如果你有一个 TCP/IP 地址为 192.0.0.1、主机名为 localhost 的主机,并且你的 MySQL 侦听器配置为默认情况下在端口 3306 上侦听,并且你的数据库名为 TUTORIALSPOINT,那么完整的数据库 URL 如下:

If you have a host at TCP/IP address 192.0.0.1 with a host name of localhost, and your MySQL listener is configured to listen on port 3306 as default, and your database name is TUTORIALSPOINT, then complete database URL would be −

jdbc:mysql://localhost/TUTORIALSPOINT

现在,你必须使用合适的用户名和密码调用 getConnection() 方法以获得 Connection 对象,如下所示:

Now you have to call getConnection() method with appropriate username and password to get a Connection object as follows −

String URL = "jdbc:mysql://localhost/TUTORIALSPOINT";
String USER = "guest";
String PASS = "password"
Connection conn = DriverManager.getConnection(URL, USER, PASS);

Using a Database URL and a Properties Object

DriverManager.getConnection( ) 方法的第三种形式需要一个数据库 URL 和一个 Properties 对象:

A third form of the DriverManager.getConnection( ) method requires a database URL and a Properties object −

DriverManager.getConnection(String url, Properties info);

一个 Properties 对象保存一组关键字-值对。它用于在调用 getConnection() 方法期间将驱动程序的属性传递给驱动程序。

A Properties object holds a set of keyword-value pairs. It is used to pass driver properties to the driver during a call to the getConnection() method.

如需建立与前一个示例相同的连接,请使用以下代码:

To make the same connection made by the previous examples, use the following code −

import java.util.*;

String URL = "jdbc:mysql://localhost/TUTORIALSPOINT";
Properties info = new Properties( );
info.put( "user", "guest" );
info.put( "password", "guest123" );

Connection conn = DriverManager.getConnection(URL, info);

为了更好地理解,我们建议您研究我们的 Java & MySQL − Sample Code tutorial

For a better understanding, we suggest you to study our Java & MySQL − Sample Code tutorial.

现在让我们如下编译上述示例:

Now let us compile the above example as follows −

C:\>javac FirstExample.java
C:\>

当您运行 FirstExample 时,它会产生以下结果:

When you run FirstExample, it produces the following result −

C:\>java FirstExample
ID: 100, Age: 18, First: Zara, Last: Ali
ID: 101, Age: 25, First: Mahnaz, Last: Fatma
ID: 102, Age: 30, First: Zaid, Last: Khan
ID: 103, Age: 28, First: Sumit, Last: Mittal
C:\>