今天看到JDBC。接下来记录一下今天的环境配置工作。
1.确保安装了:
	MySQL Server;
	MySQL Client;
	MySQL Navigator;(可选)一款用于可视化数据库的软件,类似的也有很多;
NetBeans 6.9.1;
	glassfish-3.0.1;
	mysql-connector-java-5.1.15-bin.jar;这是(MySQL Connector/J JDBC 驱动程序)
2.Navigator下配置
由于本人以前没有用过MySQL,(一直在WIN上,MSSQL),所以对MySQL的命令行下的配置相关的命令并不熟悉,所以Navigator帮了很大的忙。
	首先在Navigator下创建一个新的数据库连接:
	服务器设为:Localhost ,用户名,和密码自己设定好。设置成功后你可以在相应的项上新建数据库,或表等等。一系列操作。
3.开启MySQL服务器
	一旦你安装了MySQL,则他在系统上是默认开启的,大家从命令行上看看是否有MySQL进程;
	 
	www@www-laptop:~$ ps -A
	....
	1200       00:00:09 mysqld
	.......
	 
	大家可以进入MySQL查看。
	www@www-laptop:~$ mysql --user=root --password=catherine
	Welcome to the MySQL monitor.  Commands end with ; or g.
	Your MySQL connection id is 54
	Server version: 5.1.41-3Ubuntu12.10 (Ubuntu)
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql>
	可以查看所有的数据库:来验证大家刚刚在Navigator里面添加的数据库或者表是否能成功显示出来。
	mysql> show databases;
	+--------------------+
	| Database           |
	+--------------------+
	| information_schema |
	| JDBCTest           |
	| mysql              |
	| wolf               |
	+--------------------+
	4 rows in set (0.00 sec)
mysql>
命令行下还有很多功能强大的操作,以后有机会再接触把。
4.在NetBeans上面设置与MySQL数据库的连接
	打开NetBeans IDE ,窗口>服务>点击数据库,右键“”“”“,选择“连接”,第一次可能会提示输入密码。
在选中表上右键选择“执行命令” 会出现SQL命令 *标签页。。这里就可以像MSSQL那样,使用SQL语句写相关代码了。
	一旦添加了表,则在服务窗口里面的表上右键刷新一下。。这里操作跟MSSQL都一样,不多说了。
5.写代码测试连接
import java.sql.*; 
	import java.io.*; 
	import java.util.*; 
	public class Main { 
	public static void main(String[] args) { 
	try{ 
	runTest(); 
	} 
	catch(SQLException e){ 
	for(Throwable t:e) 
	t.printStackTrace(); 
	} 
	catch(IOException ex){ 
	ex.printStackTrace(); 
	} 
	} 
	public static void runTest()throws SQLException,IOException{ 
	Connection con=getConnection(); 
	try{ 
	Statement stat=con.createStatement(); 
	stat.executeUpdate("create table Greetings(Message CHAR(20))"); 
	stat.executeUpdate("insert into Greetings values('Hello,world')"); 
	ResultSet result=stat.executeQuery("select * from Greetings"); 
	if(result.next()) 
	System.out.println(result.getString(1)); 
	result.close(); 
	// stat.executeUpdate("drop table Greetings"); 
	} 
	finally{ 
	con.close(); 
	} 
	} 
	private static Connection getConnection() throws SQLException,IOException { 
	Properties props=new Properties(); 
	props.put("user","root"); 
	props.put("password","catherine"); 
	return DriverManager.getConnection("jdbc:mysql://localhost:3306/JDBCTest","root","catherine"); 
	} 
	}
6.驱动程序配置
一旦JDBC驱动程序没有加入到jre中去会导致报错,无法找到相关驱动driver
大家将下载的mysql-connector-java-5.1.15-bin.jar
复制到java_home目录接下来,
打开glassfish-3.0.1>glassfish>domains>domain1>lib,一旦此目录下没有MySQL的驱动器则把jar包再复制一份贴到这里来。
再编译刚刚的源程序,成功了。
并且成功创建了Greetings表,里面有一条记录Hello,world