Chinaunix首页 | 论坛 | 博客
  • 博客访问: 591061
  • 博文数量: 718
  • 博客积分: 4000
  • 博客等级: 上校
  • 技术积分: 4960
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-17 13:24
文章分类

全部博文(718)

文章存档

2011年(1)

2008年(717)

我的朋友

分类:

2008-10-17 13:28:01

  JdbcRowSet功能与ResultSet类似,与CachedRowSet不同,JdbcRowSet在操作时保持与数据库的连接。可以将与数据库连接的URL,用户名,密码以及执行的SQL语句通过setXXX形式绑定。另外,JdbcRowSet返回的结果默认是可以上下滚动和可更新的,当然这需要数据库厂商提供的JDBC Driver支持。下面的代码演示了如何通过set方法设定数据库连接参数,以及如何操作JdbcRowSet对象。



public static void testJdbcRowSet() {
	JdbcRowSetImpl jrs = new JdbcRowSetImpl();
	try {
		// 设置连接数据库的URL
		jrs.setUrl(DB2URL);
		// 设置连接数据库的用户名
		jrs.setUsername(DB2USER);
		// 设置连接数据库的密码
		jrs.setPassword(DB2PASSWORD);
		// 设置执行数据库的SQL语句
		jrs.setCommand("select * from student");
		// 执行操作
		jrs.execute();
		// 对获得的JdbcRowSet进行操作
		operateOnRowSet(jrs);
		// 关闭JdbcRowset
		jrs.close();
	} catch (SQLException e) {
		System.out.println("Andrew: SQLException!");//$NON-NLS-1$
		e.printStackTrace();
	}
}

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$
		}
	} catch (SQLException e) {
		System.out.println("Andrew: SQLException!");//$NON-NLS-1$
		e.printStackTrace();
	}
}
	

  其运行结果如下:



cursor moved
ID=0011,NAME=zhou
cursor moved
ID=0021,NAME=zhang
cursor moved



  

  FilteredRowSet接口中规定了可以设定过滤器,其过滤接口为Predicate接口,必须实现Predicate接口中的evaluate方法。具体的代码如下:



public static void testFilteredRowSet() {
	try {
		// 获得数据库连接
		Connection conn = DriverManager.getConnection(DB2URL, DB2USER,
			DB2PASSWORD);
		Statement stmt = conn.createStatement();
		// 查询数据库,获得表数据
		ResultSet rs = stmt.executeQuery("select * from student");//$NON-NLS-1$
		FilteredRowSet frs = new FilteredRowSetImpl();
		frs.populate(rs);
		// 设置过滤器
		MyDBFilter filter = new MyDBFilter(11, 100);
		frs.setFilter(filter);
		operateOnRowSet(frs);
		// 关闭FilteredRowSet
		frs.close();
		// 关闭与数据库的连接
		conn.close();
	} catch (SQLException e) {
		System.out.println("Andrew: SQLException!");//$NON-NLS-1$
		e.printStackTrace();
	}
}

public static void operateOnRowSet(RowSet rs) {
	// 为RowSet注册监听器
	System.out.println("operateOnRowSet!");//$NON-NLS-1$
	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$
		}
	} catch (SQLException e) {
		System.out.println("Andrew: SQLException!");//$NON-NLS-1$
		e.printStackTrace();
	}
}

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$
	}
	testFilteredRowSet();
}

  其中MyDBFilter实现了Predicate接口,其实现代码如下:



class MyDBFilter implements Predicate {
	private int low;
	private int high;
	public MyDBFilter(int low, int high) {
		this.low = low;
		this.high = high;
	}
	public boolean evaluate(RowSet rs) {
		CachedRowSet crs=(CachedRowSet)rs;
		//如果id在low和high之间返回真
		try {
			String id = (String) crs.getString("id");
			int idValue = Integer.parseInt(id);
			if (low < idValue && idValue < high) {
				return true;
			}
		} catch (SQLException e) {
			
		}
		return false;
	}
	public boolean evaluate(Object arg0, int arg1) throws SQLException {
		return false;
	}

	public boolean evaluate(Object arg0, String arg1) throws SQLException {
		return false;
	}
}

  其运行结果如下:



cursor moved
ID=0021,NAME=zhang
cursor moved



  

  JoinRowSet可以将多个RowSet对象进行join合并,Join的列可以通过每个RowSet通过调用setMatchColumn方法来设置。setMatchColumn方式是Joinable接口定义的方法,五种类型的RowSet规定都需要实现该接口。下面的代码演示将student表和intern表中id相同的数据进行join操作。JoinRowSet不需要保持与数据库的连接。

 

   [2]  

【责编:Kittoy】

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

public static void testFilteredRowSet() {
	try {
		// 获得数据库连接
		Connection conn = DriverManager.getConnection(DB2URL, DB2USER,
			DB2PASSWORD);
		Statement stmt = conn.createStatement();
		// 查询数据库,获得表数据
		ResultSet rs = stmt.executeQuery("select * from student");//$NON-NLS-1$
		FilteredRowSet frs = new FilteredRowSetImpl();
		frs.populate(rs);
		// 设置过滤器
		MyDBFilter filter = new MyDBFilter(11, 100);
		frs.setFilter(filter);
		operateOnRowSet(frs);
		// 关闭FilteredRowSet
		frs.close();
		// 关闭与数据库的连接
		conn.close();
	} catch (SQLException e) {
		System.out.println("Andrew: SQLException!");//$NON-NLS-1$
		e.printStackTrace();
	}
}

public static void operateOnRowSet(RowSet rs) {
	// 为RowSet注册监听器
	System.out.println("operateOnRowSet!");//$NON-NLS-1$
	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$
		}
	} catch (SQLException e) {
		System.out.println("Andrew: SQLException!");//$NON-NLS-1$
		e.printStackTrace();
	}
}

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$
	}
	testFilteredRowSet();
}

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

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