用Java实现可保存状态的数据库生成XML树(7)
发表于:2007-07-01来源:作者:点击数:
标签:
6.3.源码 6.3.1.ResultEdit SessionBean源码和相关文件 1.远程接口文件,ResultEdit. java package com.sitechasia.ejb; import java.rmi.*; import javax.ejb.*; import java.io.*; import javax. sql .RowSet; import java.sql. SQL Exception; public inter
6.3.源码
6.3.1.ResultEdit SessionBean源码和相关文件
1.远程接口文件,ResultEdit.
java
package com.sitechasia.ejb;
import java.rmi.*;
import javax.ejb.*;
import java.io.*;
import javax.
sql.RowSet;
import java.sql.
SQLException;
public interface ResultEdit extends EJBObject {
public RowSet getRowSet() throws RemoteException , SQLException ;
public RowSet setRowSet(String sqlExp) throws RemoteException , SQLException ;
}
2.Home接口文件 ResultEditHome.java
package com.sitechasia.ejb;
import java.rmi.*;
import javax.ejb.*;
import java.io.*;
import java.util.HashMap;
public interface ResultEditHome extends EJBHome {
public ResultEdit create() throws RemoteException, CreateException;
public ResultEdit create(HashMap startingCart) throws RemoteException, CreateException;
}
3.Bean类 ResultEditBean.java
package com.sitechasia.ejb;
import java.rmi.*;
import javax.ejb.*;
import java.io.*;
import java.sql.*;
import javax.sql.*;
import sun.jdbc.rowset.*;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.HashMap;
public class ResultEditBean implements SessionBean {
private SessionContext sessionContext;
public int id;
public String name;
private CachedRowSet crset=null;
private HashMap cart;
private Context ctx = null;
private DataSource ds = null;
public ResultEditBean() {
cart = new HashMap();
}
public void ejbCreate() {
try {
cart = new HashMap();
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/imis");
}
catch (Exception e) {
System.out.println("ejbCreate failed:"+e.getMessage());
e.printStackTrace();
}
}
public void ejbCreate(HashMap starting) {
try {
cart = (HashMap) starting.clone();
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/imis");
}
catch (Exception e) {
System.out.println("ejbCreate failed:"+e.getMessage());
e.printStackTrace();
}
}
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;
}
public RowSet getRowSet() throws SQLException {
return crset;
}
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();
}
}
}
原文转自:http://www.ltesting.net