Servlet调用的Session Bean的构造
来源:互联网 发布:java encode utf8 编辑:程序博客网 时间:2024/06/08 02:03
XML树是从数据库中得到相应结果集而构造的,这个结果集如果不采用EJB的方式可以用ResultSet来完成,但是如果考虑采用EJB来做,那么就需要放弃使用ResultSet,因为它是不可序列化,不能直接为EJB所用,使用RowSet或者CachedRowSet这些可序列化的结果集对象来实现.
这里构造的SessionBean是有状态的,需要构造的有远程接口,home接口和bean类.
远程接口, ResultEdit.java
home接口, ResultEditHome.java
bean类, ResultEditBean.java
对如何实现bean类进行详细描述,
public class ResultEditBean implements SessionBean {
/**Session容器定义*/
private SessionContext sessionContext;
public int id;
public String name;
/**结果集定义,定义结果集类型为sun.jdbc.rowset.CachedRowSet*/
private CachedRowSet crset=null;
/**容器定义*/
private Context ctx = null;
/**数据源定义*/
private DataSource ds = null;
/**构造函数*/
public ResultEditBean() {
}
/**EJB创建,该方法是每个EJB定义都必须有,方法里面包含EJB的创建过程*/
public void ejbCreate()
try {
/**容器初始化*/
ctx = new InitialContext();
/**从容器获得DataSource名称为imis的实例*/
ds = (DataSource)ctx.lookup("java:comp/env/imis");
}
catch (Exception e) {
System.out.println("ejbCreate failed:"+e.getMessage());
e.printStackTrace();
}
}
/**以下几个ejb开头的方法是构造EJB时必须的,分别处理着相应的事件*/
public void ejbRemove() throws RemoteException {
}
public void ejbActivate() throws RemoteException {
}
public void ejbPassivate() throws RemoteException {
}
public void setSessionContext(SessionContext sessionContext) throws RemoteException {
this.sessionContext = sessionContext;
}
/**getRowSet()返回私有成员crset,该方法在远程接口中有定义*/
public RowSet getRowSet() throws SQLException {
return crset;
}
/**setRowSet(String sqlExp)通过定义好的数据源连接数据库,并用查询语句获得结果集*/
public RowSet setRowSet(String sqlExp) throws SQLException {
Connection con = null;
try {
con = ds.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sqlExp);
CachedRowSet t_crset = new CachedRowSet();
t_crset.populate(rs);
rs.close();
stmt.close();
crset = t_crset;
return t_crset;
}
finally {
if (con != null) con.close();
}
}
SessionBean在附录中有完整的代码和EJB发布的完整过程.
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!");
} catch (IllegalAccessException e) {
System.out.println("Andrew: IllegalAccessException!");
} catch (ClassNotFoundException e) {
System.out.println("Andrew: ClassNotFoundException!");
}
testCachedRowSet();
}
- Servlet调用的Session Bean的构造
- Servlet中调用Spring管理的Bean
- servlet中调用spring的bean
- Session Bean 的生命周期
- Tomcat6远程调用GlassFish V2的session bean
- 解决servlet调用spring中bean的问题
- 解决servlet调用spring中bean的问题
- 在Servlet中调用spring中定义的Bean
- Session Bean 与 Entity Bean 的区别?Stateful Session Bean 与 Stateless Session Bean 的区别?
- ejb3(session bean)会话bean的使用!!!
- servlet的session管理
- servlet的session管理
- Servlet的session问题
- servlet session的使用
- servlet session的学习
- servlet 的session 管理
- servlet的session管理
- servlet session 的作用
- 创业失败主要原因!
- 最袖珍的垃圾回收器
- 程序员该做的事
- 人总是会变的
- philips 968 相关软件下载链接
- Servlet调用的Session Bean的构造
- C# 基本语法
- IntToHex和HexToInt函数
- 可疑进程描述与解密
- C++中巧用#define访问类的私有成员
- AJAX趋势谈
- 怎样可以让牙齿变白
- 可惜我是左脚
- IT随感