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商业应用的例子
4星 · 超过85%的资源 需积分: 9 33 浏览量
2011-11-07
19:28:26
上传
评论
收藏 15KB RAR 举报
勾月禅心
- 粉丝: 685
- 资源: 6702
最新资源
- 老飞飞搭建基础通用数据库V19数据库.rar
- jquery.js
- 机械设计多工位ACF贴胶带&预压设备sw18可编辑非常好的设计图纸100%好用.zip
- 基于Pytorch复现Point-Transformer,用于ShapeNet数据集点云分割
- 【医学影像分析】2D超声图像的分割检测(Ultrasound Nerve Segmentation - Kaggle数据集)
- 嘎嘎香的五款神仙谷歌插件
- .arch书源导入教程.mp4
- 贪心算法介绍及代码示例讲解
- CR13SP35MSI64 Crystal 水晶报表运行组件最后版本64位
- 图像分类数据集:玉米叶是否感染分类数据集(2分类,包含训练集、验证集)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈