在Java中使用MySQL的步骤
By zieckey(http://blog.csdn.net/zieckey)
All Rights Reserved!
下载MySQL的JDBC jar包:mysql-connector-java-5.0.3-bin.jar
为了后面的方便这个选项要选项。
安装完后,下面测试下:
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
我们再运行客户端进行连接:
M:/WINDOWS/system32;M:/WINDOWS;M:/WINDOWS/System32/Wbem;M:/Program Files/MySQL/MySQL Server 5.0/bin
再运行 mysqld-nt 应该就没有问题了。
格式: mysql -h主机地址 -u用户名 -p用户密码
例1:连接到本机上的MYSQL。
首先在打开DOS窗口,然后进入目录 mysqlbin,再键入命令mysql -uroot -p,回车后提示你输密码,如果刚安装好MYSQL,超级用户root是没有密码的,故直接回车即可进入到MYSQL中了,MYSQL的提示符是:mysql>
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
mysql -h110.110.110.110 -uroot -pabcd123
(注:u与root可以不用加空格,其它也一样)
mysql> exit
Bye
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
Query OK, 1 row affected (0.02 sec)
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> insert into UserInfo values ('zieckey','123456');
Query OK, 1 row affected (0.03 sec)
这里我是将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;
|
E:/JavaLesson/MySQLTest/test>javac QueryJDBC.java -d .
success loading mysql Driver....
UserName
UserPwd
zieckey 123456
该程序很可能运行出现异常,提示找不到org.gjt.mm.mysql.Driver,
这个可以通过 Project->Properties->Java Build Path->Libraries 选择 Add Extenal JARs,
然后就可以选择第三方jar包:mysql-connector-java-5.0.3-bin.jar
这样程序就可以正常运行了。
本文地址:http://www.45fan.com/a/question/69725.html