假设你的Java开发环境已经搭建好了。
一、准备工作:
下载MySQL:mysql-5.0.27-win32.zip
下载MySQL的JDBC jar包:mysql-connector-java-5.0.3-bin.jar
二、安装和测试MySQL
MySQL在Windows下的安装很简单,注意在选择配置的有一项是将MySQL的配置导入系统环境变量中,
为了后面的方便这个选项要选项。
安装完后,下面测试下:
1. 运行MySQL Server
M:/Documents and Settings/apple>mysqld-nt --help
mysqld-nt Ver 5.0.22-community-nt for Win32 on ia32 (MySQL Community Edition (G
PL))
Copyright (C) 2000 MySQL AB, by Monty and others
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license
Starts the MySQL database server
Usage: mysqld-nt [OPTIONS]
For more help options (several pages), use mysqld --verbose --help
这里可以看到mysqld-nt.exe是MySQL database server
我们再运行客户端进行连接:
如果这里说系统没有这个mysqld-nt命令,那么很可能是MySQL的配置信息没有导入系统环境变量中。这个时候可以将MySQL的安装目录下bin目录导入到系统PATH环境变量中,例如:
M:/WINDOWS/system32;M:/WINDOWS;M:/WINDOWS/System32/Wbem;M:/Program Files/MySQL/MySQL Server 5.0/bin
再运行 mysqld-nt 应该就没有问题了。
M:/Documents and Settings/apple>mysqld-nt
2.连接MYSQL。
格式: mysql -h主机地址 -u用户名 -p用户密码
例1:连接到本机上的MYSQL。
首先在打开DOS窗口,然后进入目录 mysqlbin,再键入命令mysql -uroot -p,回车后提示你输密码,如果刚安装好MYSQL,超级用户root是没有密码的,故直接回车即可进入到MYSQL中了,MYSQL的提示符是:mysql>
M:/Documents and Settings/apple>mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 1 to server version: 5.0.22-community-nt
Type 'help;' or '/h' for help. Type '/c' to clear the buffer.
mysql>
例2:连接到远程主机上的MYSQL。假设远程主机的IP为:110.110.110.110,用户名为root,密码为abcd123。则键入以下命令:
mysql -h110.110.110.110 -uroot -pabcd123
(注:u与root可以不用加空格,其它也一样)
3、退出MYSQL命令: exit (回车)
mysql> exit
Bye
M:/Documents and Settings/apple>
三、在Java中使用MySQL数据库
1. 准数据库文件
创建一个LearnJava数据库:
M:/Documents and Settings/apple>mysql -uroot -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 6 to server version: 5.0.22-community-nt
Type 'help;' or '/h' for help. Type '/c' to clear the buffer.
mysql> create database LearnJava;
Query OK, 1 row affected (0.02 sec)
创建UserInfo表格:
mysql> use LearnJava;
Database changed
mysql> create table UserInfo(
-> UserName varchar (20) not null,
-> UserPwd varchar (20) not null
-> );
Query OK, 0 rows affected (0.06 sec)
mysql>
插入一条数据:
mysql> insert into UserInfo values ('zieckey','123456');
Query OK, 1 row affected (0.03 sec)
mysql>
2. 将MySQL的JDBC jar包导入系统环境中
这里我是将mysql-connector-java-5.0.3-bin.jar放在java的安装目录下,
然后导入到CLASSPATH环境变量中,例如:
.;%JAVA_HOME%/lib/tools.jar;%JAVA_HOME%/lib/mysql-connector-java-5.0.3-bin.jar;%JAVA_HOME%/lib/dt.jar;
3. 新建 Java 程序 QueryJDBC.java
/** *该程序用来测试JDBC和MySQL连接 */ package test;
import java.sql.*;
public class QueryJDBC { static { try { //驱动和odbc不同
Class.forName ( "org.gjt.mm.mysql.Driver" ); System.out.println ( "success loading mysql Driver...." ); } catch ( Exception e ) { System.out.println ( "Error loading mysql Driver....." ); e.printStackTrace ( ); } }
/** * @param args */ public static void main ( String agrs[] ) { try { //连接参数与Access不同
String url = "jdbc:mysql://localhost/LearnJSP"; //建立连接
Connection con = DriverManager.getConnection ( url, "root", "011124" ); //建立发送SQL命令的Statement对象
Statement stmt = con.createStatement ( ); //返回查询结果
ResultSet rs = stmt.executeQuery ( "select * from userinfo" );
ResultSetMetaData md = rs.getMetaData ( );
int col = md.getColumnCount ( ); for ( int i = 1; i <= col; i++ ) { System.out.println ( md.getColumnName ( i ) + "/t" ); } while ( rs.next ( ) ) { String strData = ""; for ( int i = 1; i <= col; i++ ) { strData = strData + rs.getString ( i ) + "/t"; } System.out.println ( strData ); } //断开Connection连接
rs.close(); stmt.close(); con.close ( ); } catch ( SQLException e ) { e.printStackTrace ( ); } }
}
|
E:/JavaLesson/MySQLTest/test>javac QueryJDBC.java -d .
E:/JavaLesson/MySQLTest/test>java test.QueryJDBC
success loading mysql Driver....
UserName
UserPwd
zieckey 123456
这里要特别提醒一下,如果在Eclipse下建立工程的,
该程序很可能运行出现异常,提示找不到org.gjt.mm.mysql.Driver,
这个可以通过 Project->Properties->Java Build Path->Libraries 选择 Add Extenal JARs,
然后就可以选择第三方jar包:mysql-connector-java-5.0.3-bin.jar
这样程序就可以正常运行了。