package order.ejb;
import order.common.*;
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
public class OrderEJB implements EntityBean{
public String order_id; // 定购编号
public String user_id; // 用户帐号
public int totalPrice; // 订单价格
public ArrayList orderItems; // 订单项目的数组对象
private Connection con;
private String dbJndi="java:comp/env/jdbc/ExampleDB";
// 连接数据库的id,password
private String dbId="guest";
private String dbPassword="guest123";
private EntityContext context;
// 取得数据库连接
public void setEntityContext(EntityContext context){
this.context=context;
try{
getConnection();
}
catch(Exception ex){
throw new EJBException("Connect to DB failed:"+ex.getMessage());
}
}
// 释放数据库连接
public void unsetEntityContext(){
try{
con.close();
}
catch(SQLException ex){
throw new EJBException("Close DB Connection failed:"+ex.getMessage());
}
}
// 添加订单到ordertbl表,添加订单细目到itemtbl表
public String ejbCreate(String user_id, int totalPrice, ArrayList orderItems)
throws CreateException{
if(orderItems==null||orderItems.size()==0)
throw new CreateException("ejbCreate exception : no any item");
try{// 取得订单编号
this.order_id=genOrder_id();
// 添加一笔订单到ordertbl表格
insertOrder(this.order_id, user_id, totalPrice);
// 添加订单细目到itemtbl表格
insertItems(orderItems);
}
catch(Exception ex){
throw new EJBException("ejbCreate exception: "+ex.getMessage());
}
this.user_id=user_id;
this.totalPrice=totalPrice;
this.orderItems=orderItems;
// 返回订单编号
return order_id;
}
public void ejbPostCreate(String user_id,
int totalPrice, ArrayList orderItems){
}
// 通过一个主键查找订单
public String ejbFindByPrimaryKey(String orderId)throws FinderException{
boolean result;
try{
result=selectByPrimaryKey(orderId);
}
catch(Exception ex){
throw new EJBException("ejbFindByPrimaryKey exception:"+ex.getMessage());
}
if(result)
return orderId;
else
throw new ObjectNotFoundException("Row for id "+orderId+" not found.");
}
// 根据订购的账号查找一个订购者的所有订单
public Collection ejbFindByUserId(String userId)
throws FinderException {
Collection result;
try{
result=selectByUserId(userId);
}
catch(Exception ex){
throw new EJBException(ex.getMessage());
}
if(result.isEmpty()){
throw new ObjectNotFoundException("No rows found.");
}
else{
return result;
}
}
// 查找所有订单
public Collection ejbFindAll()throws FinderException {
Collection result;
try{
result=selectAll();
}
catch(Exception ex){
throw new EJBException(ex.getMessage());
}
if(result.isEmpty()){
throw new ObjectNotFoundException("No rows found.");
}
else{
return result;
}
}
// 读取数据库订单内容和订单细目到内存字段变量
public void ejbLoad(){
try{
loadOrder();
loadItems();
}
catch(Exception ex){
throw new EJBException("ejbLoad exception:"+ex.getMessage()+".....");
}
}
// 写入内存字段变量到数据库表
public void ejbStore(){
try{
storeOrder();
deleteItems();
insertItems(orderItems);
}
catch(Exception ex)
{
throw new EJBException("ejbLoad exception:"+ex.getMessage());
}
}
// 获得订单细目
public ArrayList getOrderItems(){
return orderItems;
}
// 取得用户id
public String getUser_id(){
return user_id;
}
// 获得订单总价
public int getTotalPrice(){
return totalPrice;
}
// 设定订单总价
public void setTotalPrice(int totalPrice){
this.totalPrice=totalPrice;
}
// 设定订单编号
public String getOrder_id(){
return order_id;
}
// 设定订单细目
public void setOrderItems(ArrayList orderItems){
if(orderItems==null||orderItems.size()==0)
throw new EJBException("setOrderItems exception : no any item");
this.orderItems=orderItems;
}
// 在激活执行前,先去的主键
public void ejbActivate(){
order_id=(String)context.getPrimaryKey();
}
// 钝化,将Bean实例放回Bean池
public void ejbPassivate(){
order_id=null;
}
// 删除订单和订单细目
public void ejbRemove(){
try{
deleteOrder();
deleteItems();
}
catch(Exception ex){
throw new EJBException("ejbRemove exception:"+ex.getMessage());
}
}
// 取得数据库连结
private void getConnection() throws NamingException, SQLException{
InitialContext ic=new InitialContext();
DataSource ds=(DataSource) ic.lookup(dbJndi);
con= ds.getConnection(dbId, dbPassword);
}
// 取得订单编号
private String genOrder_id() throws SQLException{
ResultSet rs;
String order_id;
String selectStatement ="select order_id "+"from OrderTBL where order_id=? ";
PreparedStatement ps = con.prepareStatement(selectStatement);
try{
do{
order_id=OrderId.genOrderId();
ps.setString(1, order_id);
rs=ps.executeQuery();
}while(rs.next());
return order_id;
}
finally{
ps.close();
}
}
// 插入订单到ordertbl
private void insertOrder(String order_id, String user_id, int totalPrice)
throws SQLException{
String insertStatement ="insert into OrderTBL values(? ,? ,?)";
PreparedStatement ps= con.prepareStatement(insertStatement);
try{
ps.setString(1, order_id);
ps.setString(2, user_id);
ps.setInt(3, totalPrice);
ps.executeUpdate();
}
finally{
ps.close();
}
}
// 插入订单细目到itemtbl
private void insertItems(ArrayList orderItems) throws SQLException{
String insertStatement ="insert into ItemTBL values(? ,? ,?)";
PreparedStatement ps=con.prepareStatement(insertStatement);
try{
// 逐笔将订单细目新增至ITEMTBL表格
ps.setString(1, this.order_id);
for(int i=0; i<orderItems.size(); i++){
OrderItem item=(OrderItem)orderItems.get(i);
ps.setString(2, item.getProduct_id());
ps.setInt(3, item.getQuantity());
ps.executeUpdate();
}
}
finally{
ps.close();
}
}
private boolean selectByPrimaryKey(String primaryKey) throws SQLException{
String selectStatement ="select order_id "+"from OrderTBL where order_id=?";
PreparedStatement ps =con.prepareStatement(selectStatement);
try{
ps.setString(1, primaryKey);
ResultSet rs=ps.executeQuery();
return rs.next();
}
finally{
ps.close();
}
}
private Collection selectAll() throws SQLException{
String sqlStatement="select order_id from OrderTBL";
PreparedStatement ps=con.prepareStatement(sqlStatement);
try{
ResultSet rs=ps.executeQuery();
ArrayList al=new ArrayList();
while(rs.next()){
String id=rs.getString(1);
al.add(id);
}
return al;
}finally{
ps.close();
}
}
private Collection selectByUserId(String userId) throws SQLException{
String sqlStatement="select order_id from OrderTBL "+"where user_id=?";
PreparedStatement ps=con.prepareStatement(sqlStatement);
try{
ps.setString(1, userId);
ResultSet rs=ps.executeQuery();
ArrayList al=new ArrayList();
while(rs.next()){
String id=rs.getString(1);
al.add(id);
}
return al;
}
finally{
ps.close();
}
}
// 删除一笔订单细目
private void deleteItems() throws SQLException{
String deleteStatement = "delete from ItemTBL "+"where order_id=?";
PreparedStatement ps =con.prepareStatement(deleteStatement);
try{
ps.setString(1, order_id);
ps.executeUpdate();
没有合适的资源?快使用搜索试试~ 我知道了~
一个较初级的EJB商业应用的例子
共18个文件
java:11个
jsp:6个
html:1个
4星 · 超过85%的资源 需积分: 9 7 下载量 35 浏览量
2011-11-07
19:28:26
上传
评论
收藏 15KB RAR 举报
温馨提示
一个较初级的EJB商业应用的例子,包括完整的Bean源代码,好像是一个网络订购及会员登录的模块代码,想学EJB的新手参考一下。
资源推荐
资源详情
资源评论
收起资源包目录
ejbdemo.rar (18个子文件)
codefans.net
EJB的商业应用实例
CancelOrder.jsp 739B
Order.java 683B
Quotation.jsp 2KB
index.html 707B
ProductEJB.java 2KB
Product.java 641B
QueryOrder.jsp 2KB
UpdateForm.jsp 3KB
ProductItem.java 563B
OrderForm.java 4KB
QueryOrder.java 2KB
OrderEJB.java 10KB
OrderID.java 667B
orderErr.jsp 465B
ProductHome.java 774B
OrderItem.java 1KB
OrderHome.java 729B
UpdateOrder.jsp 2KB
共 18 条
- 1
资源评论
- mowemNO12012-10-23没说明,不过还是很有帮助的
勾月禅心
- 粉丝: 685
- 资源: 6702
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功