Java Mysql 简明教程
Java & MySQL - Environment Setup
若要开始使用 JDBC 开发,您应该按照以下步骤设置 JDBC 环境。我们假设您正在 Windows 平台上工作。
To start developing with JDBC, you should setup your JDBC environment by following the steps shown below. We assume that you are working on a Windows platform.
Install Java
Java SE 可以免费下载。若要下载 click here ,请下载与您的操作系统兼容的版本。
Java SE is available for download for free. To download click here, please download a version compatible with your operating system.
按照说明下载 Java,并运行 .exe 在你的计算机上安装 Java。在计算机上安装 Java 后,你需要设置环境变量来指向正确的安装目录。
Follow the instructions to download Java, and run the .exe to install Java on your machine. Once you have installed Java on your machine, you would need to set environment variables to point to correct installation directories.
Setting Up the Path for Windows 2000/XP
假设你已将 Java 安装在 c:\Program Files\java\jdk 目录中 −
Assuming you have installed Java in c:\Program Files\java\jdk directory −
-
Right-click on 'My Computer' and select 'Properties'.
-
Click on the 'Environment variables' button under the 'Advanced' tab.
-
Now, edit the 'Path' variable and add the path to the Java executable directory at the end of it. For example, if the path is currently set to C:\Windows\System32, then edit it the following way
C:\Windows\System32;c:\Program Files\java\jdk\bin
Setting Up the Path for Windows 95/98/ME
假设你已将 Java 安装在 c:\Program Files\java\jdk 目录中 −
Assuming you have installed Java in c:\Program Files\java\jdk directory −
-
Edit the 'C:\autoexec.bat' file and add the following line at the end −
SET PATH = %PATH%;C:\Program Files\java\jdk\bin
Setting Up the Path for Linux, UNIX, Solaris, FreeBSD
环境变量 PATH 应设置为指向已安装 Java 二进制文件的位置。如果你在这方面遇到问题,请参阅 shell 文档。
Environment variable PATH should be set to point to where the Java binaries have been installed. Refer to your shell documentation if you have trouble doing this.
例如,如果你使用 bash 作为你的 shell,那么你将在 .bashrc 的末尾添加以下行 −
For example, if you use bash as your shell, then you would add the following line at the end of your .bashrc −
export PATH = /path/to/java:$PATH'
当您安装 J2SE 开发工具包时,您会自动获取 JDBC 软件包 java.sql 和 javax.sql, 。
You automatically get both JDBC packages java.sql and javax.sql, when you install J2SE Development Kit.
Install MySQL Database
当然,您将需要最重要的实际运行数据库,其中包含您可以查询和修改的表。
The most important thing you will need, of course is an actual running database with a table that you can query and modify.
-
MySQL DB − MySQL is an open source database. You can download it from MySQL Official Site. We recommend downloading the full Windows installation.
-
In addition, download and install MySQL Administrator as well as MySQL Query Browser. These are GUI based tools that will make your development much easier.
-
Finally, download and unzip MySQL Connector/J (the MySQL JDBC driver) in a convenient directory. For the purpose of this tutorial we will assume that you have installed the driver at C:\Program Files\MySQL\mysql-connector-java-5.1.8.
-
Accordingly, 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.
Set Database Credential
当我们安装 MySQL 数据库时,它的管理员 ID 设置为 root ,并提供设置密码的选项。
When we install MySQL database, its administrator ID is set to root and it gives provision to set a password of your choice.
使用 root ID 和密码,您可以创建另一个用户 ID 和密码,也可以为您的 JDBC 应用程序使用 root ID 和密码。
Using root ID and password you can either create another user ID and password, or you can use root ID and password for your JDBC application.
有各种数据库操作,例如数据库创建和删除,这需要管理员 ID 和密码。
There are various database operations like database creation and deletion, which would need administrator ID and password.
对于 JDBC 教程的其余部分,我们将使用 MySQL 数据库, guest 为 ID, guest123 为密码。
For rest of the JDBC tutorial, we would use MySQL Database with guest as ID and guest123 as password.
如果您没有足够权限创建新用户,那么您可以请求数据库管理员 (DBA) 为您创建用户 ID 和密码。
If you do not have sufficient privilege to create new users, then you can ask your Database Administrator (DBA) to create a user ID and password for you.
Create Database
要创建 TUTORIALSPOINT 数据库,请执行以下步骤 −
To create the TUTORIALSPOINT database, use the following steps −
Step 1
打开 Command Prompt 并更改到安装目录,如下所示 −
Open a Command Prompt and change to the installation directory as follows −
C:\>
C:\>cd Program Files\MySQL\bin
C:\Program Files\MySQL\bin>
Note − mysqld.exe 的路径可能因系统上 MySQL 的安装位置而异。您还可以查看有关如何启动和停止数据库服务器的文档。
Note − The path to mysqld.exe may vary depending on the install location of MySQL on your system. You can also check documentation on how to start and stop your database server.
Step 2
如果数据库服务器尚未运行,请执行以下命令以下启动数据库服务器。
Start the database server by executing the following command, if it is already not running.
C:\Program Files\MySQL\bin>mysqld
C:\Program Files\MySQL\bin>
Step 3
通过执行以下命令创建 TUTORIALSPOINT 数据库 −
Create the TUTORIALSPOINT database by executing the following command −
C:\Program Files\MySQL\bin> mysqladmin create TUTORIALSPOINT -u guest -p
Enter password: ********
C:\Program Files\MySQL\bin>
Create Table
要创建 Employees 表中 TUTORIALSPOINT 数据库,请执行以下步骤 −
To create the Employees table in TUTORIALSPOINT database, use the following steps −
Step 1
打开 Command Prompt 并更改到安装目录,如下所示 −
Open a Command Prompt and change to the installation directory as follows −
C:\>
C:\>cd Program Files\MySQL\bin
C:\Program Files\MySQL\bin>
Step 2
登录数据库,如下所示 −
Login to the database as follows −
C:\Program Files\MySQL\bin>mysql -u guest -p
Enter password: ********
mysql>
Step 3
创建表 Employees 如下所示 −
Create the table Employees as follows −
mysql> use TUTORIALSPOINT;
mysql> create table Employees
-> (
-> id int not null,
-> age int not null,
-> first varchar (255),
-> last varchar (255)
-> );
Query OK, 0 rows affected (0.08 sec)
mysql>
Create Data Records
最后,在 Employee 表中创建一些记录,如下所示 −
Finally you create few records in Employee table as follows −
mysql> INSERT INTO Employees VALUES (100, 18, 'Zara', 'Ali');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO Employees VALUES (101, 25, 'Mahnaz', 'Fatma');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO Employees VALUES (102, 30, 'Zaid', 'Khan');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO Employees VALUES (103, 28, 'Sumit', 'Mittal');
Query OK, 1 row affected (0.00 sec)
mysql>
要全面了解 MySQL 数据库,请学习 MySQL Tutorial 。
For a complete understanding on MySQL database, study the MySQL Tutorial.
现在您可以开始尝试 JDBC 了。下一章将为您提供有关 JDBC 编程的示例。
Now you are ready to start experimenting with JDBC. Next chapter gives you a sample example on JDBC Programming.