Chinaunix首页 | 论坛 | 博客
  • 博客访问: 656809
  • 博文数量: 845
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 5015
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-15 16:22
文章分类

全部博文(845)

文章存档

2011年(1)

2008年(844)

我的朋友

分类:

2008-10-15 16:31:56

  

   5在 Database Connectivity (JDBC)方面加强了支持,其中加入了新的包javax.sql.rowset,javax.sql.rowset.serial,javax.sql.rowset.spi。从RowSet接口继承规定了五个新的接口:

  1. CachedRowSet: CachedRowset可以不用与数据源建立长期的连接,只有当从数据库读取数据或是往数据库写入数据的时候才会与数据库建立连接,它提供了一种轻量级的访问数据库的方式,其数据均存在内存中。

  2. JdbcRowSet:对ResultSet的对象进行包装,使得可以将ResultSet对象做为一个JavaBeans ™ 组件。

  3. FilteredRowSet:继承自CachedRowSet,可以根据设置条件得到数据的子集。

  4. JoinRowSet:继承自CachedRowSet,可以将多个RowSet对象进行SQL Join语句的合并。

  5. WebRowSet:继承自CachedRowSet,可以将WebRowSet对象输出成XML格式。

  下面分别演示如何使用这五个新接口。



  

  IBM DB2 Universal 8.1
  数据库名:DemoDB
  数据库用户名:db2admin
  数据库密码:password



  

  CachedRowSet可以通过调用populate(ResuletSet rs)来生成数据,一旦获得数据,CachedRowSet就可以断开与数据库的连接,直到往数据库写入数据的时候才需建立连接。

  可以使用自己扩展的或是使用Reference Implement的实现类进行访问数据库。下面的代码演示了如何根据ResultSet建立一个CachedRowSet对象,在中断与数据库连接的情况下,读取数据,并做更新,最后再获取数据库连接,将更新落实到数据库中。



public static void testCachedRowSet(){
	Connection conn = null;
	try {
		// 获得数据库连接
	    conn= DriverManager.getConnection(DB2URL, DB2USER, DB2PASSWORD);
	    Statement stmt = conn.createStatement();
	    // 查询数据库,获得表数据
	    ResultSet rs = stmt.executeQuery("select * from student");//$NON-NLS-1$
	    // 根据ResultSet对象生成CachedRowSet类型的对象
	    CachedRowSetImpl crs = new CachedRowSetImpl();
	    crs.populate(rs);
	    // 关闭ResultSet
		rs.close();
	    // 关闭数据库的连接
	    conn.close();
	    // 在中断与数据库连接的情况下,对CachedRowSet进行操作
	    operateOnRowSet(crs);
	    // 重新获取与数据库的连接
	    conn= DriverManager.getConnection(DB2URL, DB2USER, DB2PASSWORD);
	    // 将CachedRowSet的内容更新到数据库
	    crs.acceptChanges(conn);
	    // 关闭CachedRowSet
	    crs.close();
	    // 关闭数据库连接
	    conn.close();
	} catch (InstantiationException e) {
	    System.out.println("Andrew: InstantiationException!");//$NON-NLS-1$
	} catch (IllegalAccessException e) {
	    System.out.println("Andrew: IllegalAccessException!");//$NON-NLS-1$
	} catch (ClassNotFoundException e) {
	    System.out.println("Andrew: ClassNotFoundException!");//$NON-NLS-1$
	}catch (SQLException e) {
	  	System.out.println("Andrew: SQLException!");//$NON-NLS-1$
		e.printStackTrace();
	}	
}

  其中operateOnRowSet方法遍历读取RowSet中的元素,并将id值加1。RowSet允许注册监听器,可以在光标移动,RowSet发生改变时触发。其具体代码如下:



public static void operateOnRowSet(RowSet rs){
	// 为RowSet注册监听器
	MyRowsetListener myListener = new MyRowsetListener();
    rs.addRowSetListener(myListener);
    // 操作RowSet数据
	try{
		// 遍历读取数据
		while (rs.next()) {
			String id = rs.getString("ID");//$NON-NLS-1$
	        String name = rs.getString("NAME");//$NON-NLS-1$
	        System.out.println("ID="+id+",NAME="+name);//$NON-NLS-1$
	        //在id最末位连接"1"
	        rs.updateString(1, id+"1");
	    }
	}catch (SQLException e) {
	    System.out.println("Andrew: SQLException!");//$NON-NLS-1$
		e.printStackTrace();
	}
}
class MyRowsetListener implements RowSetListener{
	// 光标发生移动
	public void cursorMoved(RowSetEvent event) {
		System.out.println("cursor moved");
	}
	// row发生改变
	public void rowChanged(RowSetEvent event) {
		System.out.println("row changed");
	}
	// RowSet发生改变
	public void rowSetChanged(RowSetEvent event) {
		System.out.println("row set changed");
	}
}
public static void main(String[] args) {
	try {
		Class.forName(DB2DRIVER).newInstance();
	} catch (InstantiationException e) {
		System.out.println("Andrew: InstantiationException!");//$NON-NLS-1$
	} catch (IllegalAccessException e) {
		System.out.println("Andrew: IllegalAccessException!");//$NON-NLS-1$
	} catch (ClassNotFoundException e) {
		System.out.println("Andrew: ClassNotFoundException!");//$NON-NLS-1$
	}
	testCachedRowSet();
}

  上面的程序的运行结果如下:



cursor moved
ID=001,NAME=zhou
cursor moved
ID=002,NAME=zhang
cursor moved
cursor moved
cursor moved
cursor moved
cursor moved
cursor moved
row set changed

  并且数据库中的id更新为0011,0021。



[1]   

【责编:Kittoy】

--------------------next---------------------

public static void operateOnRowSet(RowSet rs){
	// 为RowSet注册监听器
	MyRowsetListener myListener = new MyRowsetListener();
    rs.addRowSetListener(myListener);
    // 操作RowSet数据
	try{
		// 遍历读取数据
		while (rs.next()) {
			String id = rs.getString("ID");//$NON-NLS-1$
	        String name = rs.getString("NAME");//$NON-NLS-1$
	        System.out.println("ID="+id+",NAME="+name);//$NON-NLS-1$
	        //在id最末位连接"1"
	        rs.updateString(1, id+"1");
	    }
	}catch (SQLException e) {
	    System.out.println("Andrew: SQLException!");//$NON-NLS-1$
		e.printStackTrace();
	}
}
class MyRowsetListener implements RowSetListener{
	// 光标发生移动
	public void cursorMoved(RowSetEvent event) {
		System.out.println("cursor moved");
	}
	// row发生改变
	public void rowChanged(RowSetEvent event) {
		System.out.println("row changed");
	}
	// RowSet发生改变
	public void rowSetChanged(RowSetEvent event) {
		System.out.println("row set changed");
	}
}
public static void main(String[] args) {
	try {
		Class.forName(DB2DRIVER).newInstance();
	} catch (InstantiationException e) {
		System.out.println("Andrew: InstantiationException!");//$NON-NLS-1$
	} catch (IllegalAccessException e) {
		System.out.println("Andrew: IllegalAccessException!");//$NON-NLS-1$
	} catch (ClassNotFoundException e) {
		System.out.println("Andrew: ClassNotFoundException!");//$NON-NLS-1$
	}
	testCachedRowSet();
}

--------------------next---------------------

阅读(385) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~